/*
* C++ Program to Implement Doubly Linked List
*/
#include
#include
#include
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*head;
/*
Class Declaration
*/
class dll
{
public:
void create(int value);
void append(int value);
void insert(int value, int index);
void del(int value);
void display();
dll()
{
head = NULL;
}
/* destructor*/
~dll()
{
node *ptr;
ptr = head;
while(ptr!=0){
node *next = ptr->next;
delete ptr;
ptr = next;
}
}
};
/*
* Main: Conatins Menu
*/
int main()
{
int option, element, index;
dll dl;
while (1)
{
cout<>option;
switch ( option )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create(element);
cout<>element;
dl.append(element);
cout<>element;
cout<<"Insert Element after postion: ";
cin>>index;
dl.insert(element, index);
cout<>element;
dl.del(element);
cout<info = value;
temp->next = NULL;
if (head == NULL)
{
temp->prev = NULL;
head = temp;
}
else
{
s = head;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
}
/*
* Insertion at the beginning
*/
void dll::append(int value)
{
if (head == NULL)
{
cout<<"First Create the list."<prev = NULL;
temp->info = value;
temp->next = head;
head->prev = temp;
head = temp;
cout<<"Element Inserted"<next;
if (q == NULL)
{
cout<<"There are less than ";
cout<info = value;
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<info == value)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout<<"Element Deleted"<next->next != NULL)
{
/*Element deleted in between*/
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<next;
}
/*last element deleted*/
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<info<<" <-> ";
q = q->next;
}
cout<<"NULL"<
Solution
/*
* C++ Program to Implement Doubly Linked List
*/
#include
#include
#include
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*head;
/*
Class Declaration
*/
class dll
{
public:
void create(int value);
void append(int value);
void insert(int value, int index);
void del(int value);
void display();
dll()
{
head = NULL;
}
/* destructor*/
~dll()
{
node *ptr;
ptr = head;
while(ptr!=0){
node *next = ptr->next;
delete ptr;
ptr = next;
}
}
};
/*
* Main: Conatins Menu
*/
int main()
{
int option, element, index;
dll dl;
while (1)
{
cout<>option;
switch ( option )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create(element);
cout<>element;
dl.append(element);
cout<>element;
cout<<"Insert Element after postion: ";
cin>>index;
dl.insert(element, index);
cout<>element;
dl.del(element);
cout<info = value;
temp->next = NULL;
if (head == NULL)
{
temp->prev = NULL;
head = temp;
}
else
{
s = head;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
}
/*
* Insertion at the beginning
*/
void dll::append(int value)
{
if (head == NULL)
{
cout<<"First Create the list."<prev = NULL;
temp->info = value;
temp->next = head;
head->prev = temp;
head = temp;
cout<<"Element Inserted"<next;
if (q == NULL)
{
cout<<"There are less than ";
cout<info = value;
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<info == value)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout<<"Element Deleted"<next->next != NULL)
{
/*Element deleted in between*/
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<next;
}
/*last element deleted*/
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<info<<" <-> ";
q = q->next;
}
cout<<"NULL"<

C++ Program to Implement Doubly Linked List #includei.pdf

  • 1.
    /* * C++ Programto Implement Doubly Linked List */ #include #include #include /* * Node Declaration */ using namespace std; struct node { int info; struct node *next; struct node *prev; }*head; /* Class Declaration */ class dll { public: void create(int value); void append(int value); void insert(int value, int index); void del(int value); void display(); dll() { head = NULL; } /* destructor*/ ~dll() { node *ptr;
  • 2.
    ptr = head; while(ptr!=0){ node*next = ptr->next; delete ptr; ptr = next; } } }; /* * Main: Conatins Menu */ int main() { int option, element, index; dll dl; while (1) { cout<>option; switch ( option ) { case 1: cout<<"Enter the element: "; cin>>element; dl.create(element); cout<>element; dl.append(element); cout<>element; cout<<"Insert Element after postion: "; cin>>index; dl.insert(element, index); cout<>element; dl.del(element); cout<info = value; temp->next = NULL; if (head == NULL) {
  • 3.
    temp->prev = NULL; head= temp; } else { s = head; while (s->next != NULL) s = s->next; s->next = temp; temp->prev = s; } } /* * Insertion at the beginning */ void dll::append(int value) { if (head == NULL) { cout<<"First Create the list."<prev = NULL; temp->info = value; temp->next = head; head->prev = temp; head = temp; cout<<"Element Inserted"<next; if (q == NULL) { cout<<"There are less than "; cout<info = value; if (q->next == NULL) { q->next = tmp; tmp->next = NULL; tmp->prev = q; } else
  • 4.
    { tmp->next = q->next; tmp->next->prev= tmp; q->next = tmp; tmp->prev = q; } cout<<"Element Inserted"<info == value) { tmp = head; head = head->next; head->prev = NULL; cout<<"Element Deleted"<next->next != NULL) { /*Element deleted in between*/ if (q->next->info == value) { tmp = q->next; q->next = tmp->next; tmp->next->prev = q; cout<<"Element Deleted"<next; } /*last element deleted*/ if (q->next->info == value) { tmp = q->next; free(tmp); q->next = NULL; cout<<"Element Deleted"<info<<" <-> "; q = q->next; } cout<<"NULL"< Solution /* * C++ Program to Implement Doubly Linked List */
  • 5.
    #include #include #include /* * Node Declaration */ usingnamespace std; struct node { int info; struct node *next; struct node *prev; }*head; /* Class Declaration */ class dll { public: void create(int value); void append(int value); void insert(int value, int index); void del(int value); void display(); dll() { head = NULL; } /* destructor*/ ~dll() { node *ptr; ptr = head; while(ptr!=0){ node *next = ptr->next; delete ptr;
  • 6.
    ptr = next; } } }; /* *Main: Conatins Menu */ int main() { int option, element, index; dll dl; while (1) { cout<>option; switch ( option ) { case 1: cout<<"Enter the element: "; cin>>element; dl.create(element); cout<>element; dl.append(element); cout<>element; cout<<"Insert Element after postion: "; cin>>index; dl.insert(element, index); cout<>element; dl.del(element); cout<info = value; temp->next = NULL; if (head == NULL) { temp->prev = NULL; head = temp; } else
  • 7.
    { s = head; while(s->next != NULL) s = s->next; s->next = temp; temp->prev = s; } } /* * Insertion at the beginning */ void dll::append(int value) { if (head == NULL) { cout<<"First Create the list."<prev = NULL; temp->info = value; temp->next = head; head->prev = temp; head = temp; cout<<"Element Inserted"<next; if (q == NULL) { cout<<"There are less than "; cout<info = value; if (q->next == NULL) { q->next = tmp; tmp->next = NULL; tmp->prev = q; } else { tmp->next = q->next; tmp->next->prev = tmp; q->next = tmp;
  • 8.
    tmp->prev = q; } cout<<"ElementInserted"<info == value) { tmp = head; head = head->next; head->prev = NULL; cout<<"Element Deleted"<next->next != NULL) { /*Element deleted in between*/ if (q->next->info == value) { tmp = q->next; q->next = tmp->next; tmp->next->prev = q; cout<<"Element Deleted"<next; } /*last element deleted*/ if (q->next->info == value) { tmp = q->next; free(tmp); q->next = NULL; cout<<"Element Deleted"<info<<" <-> "; q = q->next; } cout<<"NULL"<