Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Doubly linklist

doubly linklist

  • Login to see the comments

Doubly linklist

  1. 1. Task: Define doubly linked . code: #include "stdafx.h" #include<conio.h> #include<iostream> using namespace std; class double_linklist { private: struct node { int data; node *prev; node *next; }*p; int c; public: double_linklist() { p=NULL; c=0; } bool isempty() { return(p==NULL); } void addatbeg(int num) { c++; node *n; n=new node; n->data=num; n->prev=NULL; if(p==NULL) { n->next=NULL; p=n; } else { n->next=p; p->prev=n; p=n; }} void addatend(int num) { c++;
  2. 2. node *n; n=new node; n->data=num; n->next=NULL; if(p==NULL) { n->prev=NULL; p=n; } else { node *temp=p; while(temp->next!=NULL) { temp=temp->next; } n->prev=temp; temp->next=n; } } void addafter(int num,int location) { if(isempty()) cout<<"Doubly link list is empty!!!"<<endl; else if(c<location) cout<<"Location not found!!!"<<endl; else { c++; node *n,*temp=p; n=new node; n->data=num; while(1<location) { temp=temp->next; location--; } temp->next->prev=n; n->next=temp->next; temp->next=n; n->prev=temp;}} void del(int num) { if(isempty()) cout<<"Doubly link list is empty!!!"<<endl; else { node *temp=p; while(temp!=NULL) {
  3. 3. if(temp->data==num) { if(temp==p) { if(c==1) p=NULL; else temp=p=p->next; p->prev=NULL; } else if(temp->next==NULL) { temp->prev->next=NULL; } else { temp->next->prev=temp->prev; temp->prev->next=temp->next; } c--; } temp=temp->next;}}} void display() { if(p==NULL) cout<<"Doubly link list is empty!!!"<<endl; else { node *temp=p; cout<<"Doubly link list elements are : "<<endl; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next;}}} void count() { cout<<"Total no of nodes is : "<<c<<endl; } ~double_llist() { node *temp; while(p!=NULL) { temp=p; p=p->next; delete temp;}}}; int _tmain(int argc, _TCHAR* argv[]) { double_llist d_linkllist; int c,num,location;
  4. 4. while(1) { cout<<"nDoubly linked list menu : "<<endl; cout<<"n1.add at end of listn2.add at beginning of listn3.add after specific in listn4.del from the list"; cout<<"n5.display the listn6.count total nodes of the listn7.exit"<<endl; cout<<"Enter your choice : "; cin>>c; switch(c) { case 1: { cout<<"Enter value : "; cin>>num; d_llist.addatend(num); break; } case 2: { cout<<"Enter value : "; cin>>num; d_llist.addatbeg(num); break; } case 3: { cout<<"Enter value : "; cin>>num; cout<<"Enter location after which you want to enter value : "; cin>>location; d_llist.addafter(num,location); break; } case 4: { cout<<"Enter value : "; cin>>num; d_llist.del(num); break; } case 5: { d_llist.display(); break; } case 6: { d_llist.count(); break;
  5. 5. } case 7: { cout<<"You are exiting....."<<endl; system("pause"); exit(0); break; } default: cout<<"Invalid choice!!!"<<endl;}} system("pause"); return 0;} Output:

×