Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include<iomanip>
- using namespace std;
- struct node
- {
- int data;
- node* next;
- };
- void create_list (node* &top,int n)
- {
- node* p;
- top = new node;
- top = NULL;
- for (int i=0;i<n;i++)
- {
- p=new node;
- cin>>p->data;
- p->next=top;
- top=p;
- }
- }
- void show_list (node* &top)
- {
- node* p;
- p=top;
- while(p)
- {
- cout<<p->data;
- p=p->next;
- }
- cout<<endl;
- }
- void push (node* &top,int data)
- {
- node* p;
- p = new node;
- p->data = data;
- p->next = top;
- top=p;
- }
- node* previous (node* top,node* p)
- {
- node* pr,*q;
- q=top;
- pr = NULL;
- while (q!=p&&q)
- {
- pr=q;
- q=q->next;
- }
- if (q==p)
- {
- return pr;
- }
- return NULL;
- }
- node* find_adress (node* top,int x)
- {
- node* p = top;
- while (p!=NULL)
- {
- if (p->data == x) {
- return p;
- }
- p= p->next;
- }
- return NULL;
- }
- void insert_list_after(node* top,node* &q,int x)
- {
- node *p;
- p = new node;
- p->data = x;
- p->next= q->next;
- q->next=p;
- }
- void insert_list_before (node* top,node* &q,int x)
- {
- node* p,*r ;
- p = new node;
- r=previous(top,q);
- if (q==top) push(top,x);
- else {
- p->data=x;
- p->next=r->next;
- r->next=p;
- }
- }
- int main ()
- {
- int n;
- int d1;
- int d2;
- int d3;
- node* list;
- node* pt = NULL;
- cout<<"Enter n: ";
- cin>>n;
- create_list(list, n);
- show_list(list);
- cout<<"Enter a num to find: ";
- cin>>d1;
- pt = find_adress(list, d1);
- if (pt)
- cout<<"Data: "<<pt->data<<endl;
- cout<<"Enter a num to insert after: ";
- cin>>d2;
- insert_list_after(list, pt, d2);
- cout<<"Enter a num to insert before: ";
- cin>>d3;
- insert_list_before(list, pt, d3);
- show_list(list);
- }
Add Comment
Please, Sign In to add comment