Advertisement
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,*p,*store;
- void insert_position(int v)
- {
- struct node *newnode;
- int i;
- newnode=(struct node*) malloc (sizeof(struct node));
- cout<<"Enter information for the newnode: "<<endl;
- cin>>newnode->data;
- newnode->next=NULL;
- struct node *cur,*pre;
- cur=head;
- if(v==1)
- {
- newnode->next=head;
- head=newnode;
- }
- else
- {
- for(i=1; i<v-1; i++)
- {
- pre=cur;
- cur=cur->next;
- }
- newnode->next=cur->next;
- cur->next=newnode;
- }
- }
- void display()
- {
- temp=head;
- cout<<endl;
- int h=0;
- while(temp!=NULL)
- {
- cout<<temp->data<<endl;
- temp=temp->next;
- h++;
- }
- cout<<"Total nodes "<<h<<endl;
- }
- int main()
- {
- int n,i,x;
- cout<<"Enter how many link list u want:"<<endl;
- cin>>n;
- cout<<"Where u want to insert:"<<endl<<endl;
- cout<<"For inserting first press 1:"<<endl;
- cout<<"For inserting in last press 2:"<<endl;
- cin>>x;
- for(i=1; i<=n; i++)
- {
- struct node *newnode,*s;
- newnode=(struct node*) malloc (sizeof(struct node));
- cout<<"Enter information: "<<endl;
- 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<<"Enter a position where u want to insert: "<<endl;
- int v;
- cin>>v;
- insert_position(v);
- cout<<"Enter a second position where u want to insert"<<endl;
- cin>>v;
- insert_position(v);
- display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement