Feedjit

Articles for you

Tuesday, December 31, 2013

Doubly LinkList in C++, Object Oriented Programming, Data Structure in C++ Addition, Deletion, Search

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
using namespace std;
struct node
{

 node *next;
    int data ;
    node * prev;
 node () {  data=0; next=prev=NULL; } 
} ;
//////////////////////////////////////////////
class Dlinklist
{  
node *head ,*tail ;

public:

Dlinklist() { head = tail =NULL; }

void add_to_tail ();
void add_to_head();
void del_from_tail();
void del_from_head();
void show_from_head();
void show_from_tail();  
 };
/////////////////////////////////////////
void Dlinklist::add_to_head()
{
 
 node* temp = new node ();
cout<<"Enter the value"<<endl;
cin>>temp->data; 
 if(head==0)
{ head=tail=temp; }

else { 
 temp->next =head;
 head->prev=temp;
 head=temp; }
}
/////////////////////////////////////////////////////
void Dlinklist::add_to_tail()
{ 
 node* temp =new node();
cout<<"Enter the value"<<endl;
cin>>temp->data; 


if( tail==0)
{ head=tail=temp; }
else {

 tail->next=temp;
 temp->prev = tail;
 tail =temp ;
  } 
}
////////////////////////////////////////////////////
void Dlinklist::del_from_head()
{ if (head==0)
cout<<"Empty list"<<endl;
else 
{ node* temp =head;
head=head->next; head->prev=0; delete temp; }
}
//////////////////////////////////////////////////
void Dlinklist::del_from_tail()
{ if(tail==0)
{  cout<<"Empty List"<<endl; }

else {  node* temp=tail;  tail= tail->prev;  tail->next = 0; delete temp; } }
////////////////////////////////////////////////////
void Dlinklist::show_from_head()
{
 node* temp = head;
 while(temp!=0)
 {   cout<<endl<<temp->data;
 temp=temp->next;  } }
//////////////////////////////
void Dlinklist::show_from_tail()
 {   node* temp = tail;

while(temp!=0)
{ cout<<endl<<temp->data;
temp=temp->prev; } }
//////////////////////////

void main()
{ Dlinklist d1;
 int c;


 cout<<endl<<"---------MENU---------"<<endl;
do{ cout<<"1. Add value to head"<<endl;
  cout<<"2. Add value to tail"<<endl;
   cout<<"3. Show values from head"<<endl;
   cout<<"4. Show values from tail"<<endl;
   cout<<"5. Delete values from head"<<endl; 
   cout<<"6. Delete values from tail"<<endl;
   cout<<"Enter Your Choice"<<endl;
    
 
cin>>c;
switch(c)
 {
 case 1:d1.add_to_head();break;
 case 2:d1.add_to_tail();break;
 case 3:d1.show_from_head();break;
 case 4:d1.show_from_tail();break;
 case 5:d1.del_from_head();break;
 case 6: d1.del_from_tail(); break;
 default:cout<<"Invalid"<<endl;break;
}
 } while(1);
}

Read More

Articles for you