Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *head=NULL,*temp;
- void insert_position(int v)
- {
- struct node *newnode;
- int i=2;
- newnode=(struct node*) malloc (sizeof(struct node));
- cout<<"Enter information for the newnode: "<<endl;
- cin>>newnode->data;
- newnode->next=NULL;
- struct node *cur;
- cur=head;
- if(v==1)
- {
- newnode->next=head;
- head=newnode;
- }
- else
- {
- while(i!=v && cur->next!=NULL)
- {
- cur=cur->next;
- i++;
- }
- if(cur->next==NULL && i!=v)
- {
- cout<<"Opps!! position was not found"<<endl;
- }
- else if(cur->next==NULL && i==v)
- {
- cur->next=newnode;
- newnode->next=NULL;
- }
- else
- {
- newnode->next=cur->next;
- cur->next=newnode;
- }
- }
- }
- void insert_data(int v)
- {
- struct node *newnode,*pre,*cur;
- newnode=(struct node*)malloc(sizeof(struct node));
- cout<<"Give information for newnode"<<endl;
- cin>>newnode->data;
- newnode->next=NULL;
- pre=head;
- cur=head->next;
- if(pre->data==v)
- {
- newnode->next=pre;
- head=newnode;
- }
- else
- {
- while(cur->data!=v && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- }
- if(cur->data==v)
- {
- newnode->next=cur;
- pre->next=newnode;
- }
- if(cur->data!=v && cur->next==NULL)
- {
- cout<<"Data was not found"<<endl;
- }
- }
- }
- void search(int d)
- {
- temp=head;
- int count=1;
- while(temp->data!=d && temp->next!=NULL)
- {
- count++;
- temp=temp->next;
- }
- if(temp->data==d)
- {
- cout<<endl<<"**Data found at position: "<<count <<endl;
- }
- else
- {
- cout<<endl<<"**Data was not found: "<<endl;
- }
- }
- void delete_position(int z)
- {
- struct node *pre,*cur;
- int i=2;
- pre=head;
- cur=head->next;
- if(z==1)
- {
- head=cur;
- delete(pre);
- }
- else
- {
- while(i!=z && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- i++;
- }
- if(i!=z && cur->next==NULL)
- {
- cout<<"Position was not found in the list"<<endl;
- }
- else
- {
- pre->next=cur->next;
- delete(cur);
- }
- }
- }
- void delete_data(int g)
- {
- int i;
- i=g;
- struct node *pre,*cur;
- pre=head;
- cur=head->next;
- if(pre->data==g)
- {
- head=cur;
- delete(pre);
- }
- else
- {
- while(cur->data!=g && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- }
- if(cur->next==NULL && cur->data==g)
- {
- pre->next=NULL;
- delete(cur);
- }
- else if(cur->data==g)
- {
- pre->next=cur->next;
- delete(cur);
- }
- else
- {
- cout<<"Data was not found"<<endl;
- }
- }
- }
- void display()
- {
- temp=head;
- cout<<endl;
- while(temp!=NULL)
- {
- cout<<temp->data<<endl;
- temp=temp->next;
- }
- }
- int main()
- {
- int n,i,x;
- struct node *s;
- s=(struct node*)malloc(sizeof(struct node));
- cout<<"Enter how many link list u want:"<<endl;
- cin>>n;
- cout<<"For inserting first press 1:"<<endl<<endl;;
- cout<<"For inserting in last press 2:"<<endl;
- cin>>x;
- cout<<endl;
- cout<<"Enter information for newnode"<<endl;
- for(i=1; i<=n; i++)
- {
- struct node *newnode;
- newnode=(struct node*) malloc (sizeof(struct node));
- cin>>newnode->data;
- newnode->next=NULL;
- if(x==1)
- {
- if(head==NULL)
- {
- head=newnode;
- }
- else
- {
- newnode->next=head;
- head=newnode;
- }
- }
- if(x==2)
- {
- if(head==NULL)
- {
- head=newnode;
- s=head;
- }
- else
- {
- s->next=newnode;
- s=newnode;
- }
- }
- }
- cout<<endl;
- cout<<"Enter which data your searching for :"<<endl;
- int d;
- cin>>d;
- search(d);
- display();
- cout<<"Enter a position of a note which u want to delete:"<<endl;
- int z;
- cin>>z;
- delete_position(z);
- display();
- cout<<"Enter a data of a note which u want to delete:"<<endl;
- int g;
- cin>>g;
- delete_data(g);
- display();
- cout<<"Enter a position where u want to insert: "<<endl;
- int v;
- cin>>v;
- insert_position(v);
- display();
- cout<<"Enter a data where u want to insert: "<<endl;
- cin>>v;
- insert_data(v);
- display();
- return 0;
- }
Add Comment
Please, Sign In to add comment