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 data;
- Node *next;
- Node *prev;
- };
- class LinkList{
- public:
- Node *head;
- LinkList(){
- head = NULL;
- }
- void insert_at_first( int value)
- {
- Node *newNode = new Node();
- newNode->data = value;
- newNode->next = head;
- newNode->prev = NULL;
- if( head != NULL)
- {
- head->prev = newNode;
- }
- head = newNode;
- //cout<<head->data;
- }
- ///insert after a specific node;
- void insert_at_middle(int value)
- {
- Node *temp = head->next;
- if( temp == NULL)
- {
- cout<<"The given previous node can not be NUlL";
- return ;
- }
- Node *newNode = new Node();
- newNode->data = value;
- newNode->next = temp->next;
- temp->next = newNode;
- newNode->prev = temp;
- if(newNode->next != NULL)
- newNode->next->prev = newNode;
- }
- void insert_after_value(int value,int key)
- {
- Node *temp = head;
- if(temp == NULL)
- {
- cout<<"Link list is empty";
- return ;
- }
- do{
- if(temp->data == key)
- {
- Node *newNode = new Node();
- newNode->data = value;
- newNode->next = temp->next;
- temp->next = newNode;
- newNode->prev = temp;
- if(newNode->next != NULL)
- {
- newNode->next->prev = newNode;
- }
- return ;
- }
- temp = temp ->next;
- }while(temp != NULL);
- cout<<"Value is not found";
- }
- void insert_at_end( int value)
- {
- Node *temp = head;
- Node *newNode = new Node();
- newNode->data = value;
- newNode->next = NULL;
- if(head == NULL)
- {
- newNode->prev = NULL;
- head = newNode;
- return ;
- }
- while( temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = newNode;
- newNode->prev = temp;
- }
- void bubble_sort()
- {
- int swapped;
- Node *right_ptr = NULL;
- if( head == NULL)
- return ;
- do{
- swapped = 0;
- Node *temp = head;
- while ( temp->next != right_ptr)
- {
- if(temp->data > temp->next->data )
- {
- swap(temp->data, temp->next->data);
- swapped = 1;
- }
- temp = temp->next;
- }
- right_ptr = temp;
- //cout<<lptr<<endl;
- }while(swapped);
- }
- void delete_node(int key)
- {
- Node *temp = head,*previous;
- //cout<<temp->data;
- if( temp->data == key)
- {
- head = temp->next;
- head->prev = NULL;
- delete(temp);
- return;
- }
- while(temp != NULL && temp->data != key)
- {
- previous = temp;
- temp = temp->next;
- }
- if(temp == NULL)
- {
- cout<<"Value is not found";
- return;
- }
- cout<<previous->data;
- previous->next = temp->next;
- if(temp->next != NULL)
- previous->next->prev = previous;
- }
- void display()
- {
- Node *temp,*last;
- temp = head;
- cout<<"Traversal in forward direction "<<endl;
- while( temp != NULL)
- {
- cout<<temp->data<<" ";
- last = temp;
- temp = temp-> next;
- }
- cout<<endl<<" Traversal in reverse direction "<<endl;
- while(last != NULL)
- {
- cout<<last->data<<" ";
- last= last->prev;
- }
- }
- void reverse_List()
- {
- Node *temp = NULL;
- Node *curr = head;
- cout<<endl<<endl;
- while( curr != NULL)
- {
- ///swap between node previous and node next;
- temp = curr->prev;
- curr->prev = curr->next;
- curr->next = temp;
- curr = curr->prev;
- }
- if( temp != NULL)
- head = temp->prev;
- }
- };
- int main()
- {
- LinkList ll;
- ll.insert_at_first(10);
- ll.insert_at_first(20);
- ll.insert_at_first(30);
- ll.insert_at_middle(500);
- ll.insert_at_end(600);
- ll.display();
- cout<<endl<<"which value after you want to insert"<<endl;
- int value;
- cin>>value;
- ll.insert_after_value(40,value);
- ll.display();
- cout<<endl<<"after reverse"<<endl;
- ll.reverse_List();
- ll.display();
- cout<<endl<<"After short"<<endl;
- ll.bubble_sort();
- ll.display();
- cout<<endl<<"After Delete"<<endl;
- cout<<"which value you want to delete"<<endl;
- cin>>value;
- ll.delete_node(value);
- ll.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement