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 *prev;
- };
- typedef struct node Node;
- Node* create_node(int item, Node *next,Node*prev)
- {
- Node* new_node=new Node();
- if(new_node==NULL)
- cout<<"Error"<<endl;
- else
- {
- new_node->data=item;
- new_node->next=next;
- new_node->prev=prev;
- }
- return new_node;
- }
- Node* prepend(int item,Node* head)
- {
- Node* new_node=create_node(item,head,NULL);
- return new_node;
- }
- Node* append(int item,Node *head)
- {
- Node *new_node=create_node(item,NULL,NULL);
- if(head==NULL)
- return new_node;
- else
- {
- Node* current=new Node();
- current=head;
- while(current->next!=NULL)
- current=current->next;
- current->next=new_node;
- new_node->prev=current;
- }
- return head;
- }
- Node* insertn(int item,Node* head,int key)
- {
- Node* new_node=create_node(item,NULL,NULL);
- if(head==NULL)
- cout<<"Error"<<endl;
- else
- {
- Node *current=new Node();
- current=head;
- while(current!=NULL&¤t->data!=key)
- current=current->next;
- if(current!=NULL)
- {
- new_node->next=current->next;
- current->next=new_node;
- new_node->prev=current;
- }
- else
- cout<<"Value not found"<<endl;
- }
- return head;
- }
- void display(Node *head)
- {
- cout<<"Linked list:"<<endl;
- while(head!=NULL)
- {
- cout<<head->data<<" ";
- head=head->next;
- }
- cout<<endl;
- }
- void display_rev(Node* head)
- {
- Node* temp=new Node();
- temp=head;
- if(temp==NULL)
- return;
- cout<<"reversed:"<<endl;
- while(temp->next!=NULL)
- temp=temp->next;
- while(temp!=NULL)
- {
- cout<<temp->data<<" ";
- temp=temp->prev;
- }
- }
- int main()
- {
- Node* head=NULL;
- head=prepend(10,head);
- head=insertn(20,head,10);
- head=insertn(30,head,20);
- head=insertn(40,head,30);
- head=insertn(50,head,40);
- head=insertn(60,head,50);
- head=insertn(70,head,60);
- display(head);
- display_rev(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement