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 *pre,*next;
- };
- struct node *head=NULL,*temp,*cur,*pre,*last;
- void insert(int v)
- {
- pre=head;
- cur=head->next;
- struct node *newnode;
- newnode= (struct node*)malloc(sizeof(struct node));
- cout<<"Enter information for newnode"<<endl;
- cin>>newnode->data;
- newnode->next=NULL;
- newnode->pre=NULL;
- if(pre->data==v)
- {
- newnode->next=pre;
- pre->pre=newnode;
- head=newnode;
- }
- else
- {
- while(cur->data!=v && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- }
- if(cur->data==v)
- {
- newnode->next=cur;
- cur->pre=newnode;
- pre->next=newnode;
- newnode->pre=pre;
- }
- if(cur->data!=v && cur->next==NULL)
- {
- cout<<"Sorry!! Not found"<<endl;
- }
- }
- }
- void deletedata(int v)
- {
- pre=head;
- cur=head->next;
- if(pre->data==v)
- {
- cur->pre=NULL;
- head=cur;
- delete(pre);
- }
- else
- {
- while(cur->data!=v && cur->next!=NULL)
- {
- pre=cur;
- cur=cur->next;
- }
- if(cur->data==v && cur->next!=NULL)
- {
- cur->next->pre=pre;
- pre->next=cur->next;
- delete(cur);
- }
- if(cur->data==v && cur->next==NULL)
- {
- pre->next=NULL;
- delete(cur);
- }
- else
- {
- cout<<"OPPS!! Not found"<<endl;
- }
- }
- }
- void display()
- {
- temp=head;
- while(temp!=NULL)
- {
- cout<<temp->data<<endl;
- temp=temp->next;
- }
- }
- void revdisplay()
- {
- temp=last;
- while(temp!=NULL)
- {
- cout<<temp->data<<endl;
- temp=temp->pre;
- }
- }
- int main()
- {
- cout<<"Enter how many node you want"<<endl;
- int n;
- cin>>n;
- struct node *s;
- s= (struct node*)malloc(sizeof(struct node));
- for(int i=1; i<=n; i++)
- {
- struct node *newnode;
- newnode= (struct node*)malloc(sizeof(struct node));
- cin>>newnode->data;
- newnode->next=NULL;
- newnode->pre=NULL;
- if(head==NULL)
- {
- head=newnode;
- s=head;
- last=head;
- }
- else
- {
- s->next=newnode;
- newnode->pre=s;
- newnode->next=NULL;
- s=newnode;
- last=newnode;
- }
- }
- cout<<endl;
- display();
- cout<<endl;
- revdisplay();
- int v;
- cout<<"Enter a data where u want to insert"<<endl;
- cin>>v;
- insert(v);
- cout<<endl;
- display();
- cout<<"Enter a data which you want to delete"<<endl;
- cin>>v;
- deletedata(v);
- cout<<endl;
- display();
- return 0;
- }
Add Comment
Please, Sign In to add comment