Thursday, 20 August 2015

(group b )dsps assigment doubly link list implemention through c++






INPUT:
#include<iostream>
using namespace std;
class node
{
 int data;
 node *next;
 node *prev;
 public:
 node *create();
 void display_f(node *head);
 void display_b(node *head);
 node *insert_b(node *head);
 node *insert_end(node *head);
 node *insert_in(node *head);
 node *del_b(node *head);
 node *del_end(node *head);
 node *del_in(node *head);
 void search(node *head);
 int count(node *head);
 node *concat(node *head,node *head1);
};

node* node::create()
{
 node *head,*p,*q;
 int i,n;
 head=NULL;
 cout<<"\nEnter the no. of nodes";
 cin>>n;
 for(i=0;i<n;i++)
 {
  if(head==NULL)
  {
   head=new node;
   q=p=head;
  }
  else
  {
  p->next=new node;
  p=p->next;
  p->prev=q;
  q=q->next;
  }
 cout<<"\nenter the data of node"<<i+1<<"\t\t";
 cin>>p->data;
 p->next=head;
 head->prev=p;
 }
return head;
}

void node::display_f(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 }
 else
 {
 node *p;
 p=head;
 cout<<"\n\nDCLL is:";
 do
  {
  cout<<p->data<<"->";
  p=p->next;
  }
 while(p->next!=head);
  cout<<p->data<<"\n\n";
  }
 }


void node::display_b(node *head)
{
 if(head==NULL)
 {
 cout<<"Linked list is empty";
 }
 else
 {
 node *p,*q;
 cout<<"\n\nDCLL is:";
 p=head->prev;
 q=p;
 do
 {
 cout<<q->data<<"->";
 q=q->prev;
 }
 while(q!=head);
 cout<<q->data<<"\n\n";
 }
}
node *node::insert_b(node *head)
{
 if(head==NULL)
 {
 cout<<"\n Linked list is empty";
 return head;
 }
 else
 {
 node *p,*q;
 p=new node;
 cout<<"\nEnter the data of node:";
 cin>>p->data;
 q=head->prev;
 p->next=head;
 head->prev=p;
 p->prev=q;
 q->next=p;
 head=p;
 return (head);
 }
}

node* node::insert_end(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
else
 {
 node *p;
 p=new node;
 cout<<"\nEnter the data:";
 cin>>p->data;
 head->prev->next=p;
 p->prev=head->prev;
 p->next=head;
 head->prev=p;
 return (head);
 }
}
node* node::insert_in(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
 else
 {
 node *p,*q;
 int x;
 p=new node;
 cout<<"\nEnter the data:";
 cin>>p->data;
 cout<<"\nInsert after which no?";
 cin>>x;
 if(head->data==x)
 {
 p->next=head->next;
 head->next->prev=p;
 p->prev=head;
 return(head);
 }
 else
 {
 node *q,*r;
 q=head;
 do
  {
  q=q->next;
  }
 while(q!=head && q->data!=x);
 if(q!=head)
 {
 r=q->next;
 p->next=q->next;
 q->next=p;
 p->prev=q;
 r->prev=p;
 return(head);
 }
 else
 {
 cout<<"\nData not found";
 }
}
}
}

void node::search(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 }
 else
 {
 int z,c=1;
 node *q;
 cout<<"\n\nEnter data to be searched:";
 cin>>z;
 if(head->data==z)
 {
 cout<<"\ndata found at position:"<<c;
 }
 else
 {
 q=head;
 do
 {
 c++;
 q=q->next;
 }
 while(q!=head && q->data!=z);
 if(q!=head)
 {
 cout<<"\ndata found at "<<c<<" position";
 }
 else
 {
 cout<<"\ndata not found";
 }
}
}
}
int node::count(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return 0;
 }
 else
 {
 int c=0;
 node *q;
 q=head;
 do
 {
 c++;
 q=q->next;
 }
 while(q!=head);
 cout<<"\nNumber of nodes is"<<c;
 return c;
 }
}

node* node::concat(node *head,node *head1)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
 else
 {
 node *p;
 p=head->prev;
 head->prev->next=head1;
 head->prev=head1->prev;
 head1->prev->next=head;
 head1->prev=p;
 return(head);
 }
}
 node* node::del_b(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
 else if(head->next==head)
 {
 delete(head);
 head=NULL;
 return(head);
 }
 else
 {
 node *p;
 p=head;
 head=p->next;
 head->prev=p->prev;
 p->prev->next=head;
 delete(p);
 return(head);
 }
}
 node* node::del_end(node *head)
{
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
 else if(head->next==head)
 {
 delete(head);
 head=NULL;
 return(head);
 }
 else
 {
 node *p;
 p=head->prev;
 p->prev->next=head;
 head->prev=p->prev;
 delete(p);
 return(head);
 }
}
node* node::del_in(node *head)
 {
 if(head==NULL)
 {
 cout<<"\nLinked list is empty";
 return(head);
 }
 else if(head->next==head)
 {
 delete(head);
 head=NULL;
 return(head);
 }
 else
 {
 node *p;
 int x;
 cout<<"\nEnter which data to delete";
 cin>>x;
 if(head->data==x)
 {
 p=head;
 head=p->next;
 head->prev=p->prev;
 p->prev->next=head;
 delete p;
 return (head);
 }
 else
 {
 node *q,*r;
 p=head;
 do
 {
 p=p->next;
 }
 while(p!=head && p->data!=x);
 if(p!=head)
 {
 q=p->next;
 q->prev=p->prev;
 p->prev->next=q;
 delete p;
 return (head);
 }
 else
 {
 cout<<"\nData not found";
 }
}
}
}
int main()
{
 node obj1,*head,*head1;
 int choice,c;
 do
 {
 cout<<"\n1.Create\n2.Display begin\n3.Display end\n4.Insert begin\n5.Insert end\n6.Insert middle\n7.Search\n8.Count\n9.Concat\n10.Delete begin\n11.Delete end\n12.Delete middle\n13.exit\n";
 cout<<"\nEnter your choice:";
 cin>>choice;
 switch(choice)
  {
 case 1:head=obj1.create();
 break;
 case 2:obj1.display_f(head);
 break;
 case 3:obj1.display_b(head);
 break;
 case 4:head=obj1.insert_b(head);
 break;
 case 5:head=obj1.insert_end(head);
 break;
 case 6:head=obj1.insert_in(head);
 break;
 case 7:obj1.search(head);
 break;
 case 8:c=obj1.count(head);
 break;
 case 9:head1=obj1.create();
 head=obj1.concat(head,head1);
 break;
 case 10:head=obj1.del_b(head);
 break;
 case 11:head=obj1.del_end(head);
 break;
 case 12:head=obj1.del_in(head);
 break;
 case 13:
 default:break;
 cout<<"\nInvalid choice";
 }
}
while(choice!=13);
return 0;
 }




OUTPUT:
1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:1

Enter the no. of nodes3

enter the data of node1           1

enter the data of node2           2

enter the data of node3           3

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:2


DCLL is:1->2->3


1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:3


DCLL is:3->2->1


1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:4

Enter the data of node:4

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:5

Enter the data:6

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:6

Enter the data:6

Insert after which no?2

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:2

                                 
DCLL is:4->1->2->6->3->6


1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:7


Enter data to be searched:2

data found at 3 position
1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:8

Number of nodes is6
1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:9

Enter the no. of nodes2

enter the data of node1           7

enter the data of node2           8

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:2


DCLL is:4->1->2->6->3->6->7->8


1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:10

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:11

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:12

Enter which data to delete3

1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:2


DCLL is:1->2->6->6->7


1.Create
2.Display begin
3.Display end
4.Insert begin
5.Insert end
6.Insert middle
7.Search
8.Count
9.Concat
10.Delete begin
11.Delete end
12.Delete middle
13.exit

Enter your choice:13

No comments:

Post a Comment