Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class node
- {
- public:
- int info;
- node *link;
- };
- node *head = NULL, *tmp = NULL;
- void creation()
- {
- node *ptr = new node();
- int item;
- cin>>item;
- ptr->info = item;
- ptr->link = NULL;
- if(head == NULL)
- {
- head = ptr;
- tmp = ptr;
- }
- else
- {
- tmp->link = ptr;
- tmp = ptr;
- }
- }
- void traverse()
- {
- cout<<"The items in the list : ";
- node *srt;
- for(srt = head; ; )
- {
- if(srt->link != NULL)
- {
- cout<<srt->info<<" ";
- srt = srt->link;
- }
- else
- {
- cout<<srt->info;
- break;
- }
- }
- cout<<endl<<endl;
- }
- void first_insert()
- {
- cout<<"Enter the item you want to inesrt : ";
- int item;
- cin>>item;
- node *ptr = new node();
- ptr->info = item;
- ptr->link = head;
- head = ptr;
- }
- void last_insert()
- {
- cout<<"Enter the item you want to inesrt : ";
- int item;
- cin>>item;
- node *ptr = new node();
- ptr->info = item;
- ptr->link = NULL;
- node *srt;
- for(srt = head; ; )
- {
- if(srt->link == NULL)
- {
- srt->link = ptr;
- break;
- }
- else
- {
- srt = srt->link;
- }
- }
- }
- void before_insert()
- {
- cout<<"Enter the 'before' value : ";
- int s_item;
- cin>>s_item;
- node *srt, *tmp;
- for(srt = head; ; )
- {
- if(srt->info == s_item)
- {
- if(srt == head)
- {
- first_insert();
- break;
- }
- else
- {
- cout<<"Enter the item you want to inesrt : ";
- int item;
- cin>>item;
- node *ptr = new node();
- ptr->info = item;
- ptr->link = NULL;
- tmp->link = ptr;
- ptr->link = srt;
- break;
- }
- }
- else
- {
- tmp = srt;
- srt = srt->link;
- }
- if(srt == NULL)
- {
- cout<<"'before' value not found."<<endl;
- break;
- }
- }
- }
- void after_insert()
- {
- cout<<"Enter the 'after' value : ";
- int s_item;
- cin>>s_item;
- node *srt, *tmp;
- for(srt = head; ; )
- {
- if(srt->info == s_item)
- {
- if(srt->link == NULL)
- {
- last_insert();
- break;
- }
- else
- {
- cout<<"Enter the item you want to inesrt : ";
- int item;
- cin>>item;
- node *ptr = new node();
- ptr->info = item;
- ptr->link = srt->link;
- srt->link = ptr;
- break;
- }
- }
- else
- {
- tmp = srt;
- srt = srt->link;
- }
- if(srt == NULL)
- {
- cout<<"'before' value not found."<<endl;
- break;
- }
- }
- }
- int main()
- {
- int i, n;
- cout<<"How many items you want to enter for creation : ";
- cin>>n;
- cout<<"Enter the items : ";
- for(i = 0; i < n; i++)
- {
- creation();
- }
- traverse();
- while(1)
- {
- int x;
- cout<<"Enter 1 for first insert \n Enter 2 for last insert \n Enter 3 for before insert \n Enter 4 for after insert : ";
- cin>>x;
- cout<<endl;
- if(x == 1)
- {
- first_insert();
- traverse();
- }
- else if(x == 2)
- {
- last_insert();
- traverse();
- }
- else if(x == 3)
- {
- before_insert();
- traverse();
- }
- else if(x == 4)
- {
- after_insert();
- traverse();
- }
- else
- {
- cout<<"Please enter a number between 1 to 4 "<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement