0% found this document useful (0 votes)
42 views5 pages

C++ Doubly Linked List Operations

The document contains C++ code for implementing a double linked list, including functions for creating the list, inserting and deleting nodes at various positions, and traversing the list in both directions. It provides a menu-driven interface for user interaction to perform these operations. The code also includes error handling for invalid positions during insertion and deletion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

C++ Doubly Linked List Operations

The document contains C++ code for implementing a double linked list, including functions for creating the list, inserting and deleting nodes at various positions, and traversing the list in both directions. It provides a menu-driven interface for user interaction to perform these operations. The code also includes error handling for invalid positions during insertion and deletion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream.

h>
struct dlinklist #include <iostream>
{ #include <cstdlib>
struct dlinklist *left;
int data; using namespace std;
struct dlinklist *right; struct dlinklist {
}; dlinklist *left;
typedef struct dlinklist node; int data;
node *start = NULL; dlinklist *right;
node* getnode() };
{ typedef struct dlinklist node;
node * newnode; node *start = NULL;
newnode = (node *) malloc(sizeof(node)); node* getnode() {
cout<<"\n Enter data: "); node *newnode;
cin>>"%d", &newnode -> data); newnode = new node();
newnode -> left = NULL; newnode cout << "\nEnter data: ";
-> right = NULL; cin >> newnode->data;
return newnode; newnode->left = NULL;
} newnode->right = NULL;
int countnode(node *start) return newnode;
{ }
if(start == NULL) return int countnode(node *start) {
0; if(start == NULL) return 0;
else return 1 + countnode(start -> right); else return 1 + countnode(start->right);
} }
int menu() int menu() {
{ int ch; int ch;
cout<<"\n [Link]"; cout << "\n1. Create";
cout<<"\n------------------------------"; cout << "\n------------------------------";
cout<<"\n 2. Insert a node at beginning "; cout << "\n2. Insert a node at beginning";
cout<<"\n 3. Insert a node at end"; cout << "\n3. Insert a node at end";
cout<<"\n 4. Insert a node at middle"; cout << "\n4. Insert a node in middle";
cout<<"\n\n------------------------------"; cout << "\n------------------------------";
cout<<"\n 5. Delete a node from beginning"; cout << "\n5. Delete a node from beginning";
cout<<"\n 6. Delete a node from Last"; cout << "\n6. Delete a node from end";
cout<<"\n 7. Delete a node from Middle"; cout<<"\ cout << "\n7. Delete a node from middle";
n------------- cout << "\n------------------------------";
-----------------"; cout << "\n8. Traverse the list from Left to Right";
cout<<"\n 8. Traverse the list from Left to Right "; cout << "\n9. Traverse the list from Right to Left";
cout<<"\n 9. Traverse the list from Right to Left "; cout << "\n------------------------------";
cout<<"\n------------------------------"; cout << "\n10. Count the Number of nodes in the
cout<<"\n [Link] the Number of nodes in the list"; list";
cout<<"\n [Link] "; cout<<"\n\n Enter your choice: cout << "\n11. Exit";
"); cin>>"%d", &ch); return ch; cout << "\n\nEnter your choice: ";
} cin >> ch;
void createlist(int n) return ch;
{ }
int i; node *newnode;
node *temp; void createlist(int n) {
for(i = 0; i < n; i++) int i;
{ node *newnode, *temp;
newnode = getnode();
if(start == NULL) start = for(i = 0; i < n; i++) {
newnode; newnode = getnode();
else
{ if(start == NULL)
temp = start; while(temp -> start = newnode;
right) temp = temp -> right; else {
temp -> right = newnode; temp = start;
newnode -> left = temp; while(temp->right != NULL) temp = temp->right;
}
}} temp->right = newnode;
void traverse_left_to_right() newnode->left = temp;
{ }
node *temp; temp }
= start; }
cout<<"\n The contents of List: ";
if(start == NULL ) cout<<"\n void traverse_left_to_right() {
Empty List"; node *temp = start;
else
{ cout << "\nThe contents of the list: ";
while (temp != NULL) if(start == NULL)
{ cout << "\nEmpty List";
cout<<"\t %d ", temp -> data; else {
temp = temp -> right; while (temp != NULL) {
}} cout << "\t" << temp->data;
} temp = temp->right;
void traverse_right_to_left() }
{ }
node *temp; temp = start; }
cout<<"\n The contents of List: ";
if(start == NULL) cout<<"\n void traverse_right_to_left() {
Empty List"; node *temp = start;
else
{ cout << "\nThe contents of the list: ";
while(temp -> right != NULL) if(start == NULL) {
temp = temp -> right; cout << "\nEmpty List";
} } else {
while(temp != NULL) while(temp->right != NULL)
{ temp = temp->right;
cout<<"\t%d", temp -> data;
temp = temp -> left; while(temp != NULL) {
} cout << "\t" << temp->data;
} temp = temp->left;
void dll_insert_beg() }
{ }
node *newnode; }
newnode = getnode();
if(start == NULL) start = void dll_insert_beg() {
newnode; node *newnode = getnode();
else
{ if(start == NULL) {
newnode -> right = start; start = newnode;
start -> left = newnode; } else {
start = newnode; newnode->right = start;
} start->left = newnode;
} start = newnode;
void dll_insert_end() }
{ }
node *newnode, *temp;
newnode = getnode(); void dll_insert_end() {
if(start == NULL) start = node *newnode = getnode(), *temp;
newnode; else
{ if(start == NULL) {
temp = start; while(temp -> start = newnode;
right != NULL) temp = temp - } else {
> right; temp = start;
temp -> right = newnode; newnode while(temp->right != NULL) temp = temp->right;
-> left = temp;
} temp->right = newnode;
} newnode->left = temp;
void dll_insert_mid() }
{ }
node *newnode,*temp;
int pos, nodectr, ctr = 1; void dll_insert_mid() {
newnode = getnode(); node *newnode = getnode(), *temp;
cout<<"\n Enter the position: "); int pos, nodectr, ctr = 1;
cin>>"%d", &pos; nodectr =
countnode(start); cout << "\nEnter the position: ";
if(pos - nodectr >= 2) cin >> pos;
{ nodectr = countnode(start);
cout<<"\n Position is out of range..";
return; if(pos > 1 && pos < nodectr) {
} temp = start;
if(pos > 1 && pos < nodectr) while(ctr < pos - 1) {
{ temp = temp->right;
temp = start; ctr++;
while(ctr < pos - 1) }
{ newnode->left = temp;
temp = temp -> right; newnode->right = temp->right;
ctr++; temp->right->left = newnode;
} temp->right = newnode;
newnode -> left = temp; } else {
newnode -> right = temp -> right; cout <<”position of list is not a middle position”
temp -> right -> left = newnode; }
temp -> right = newnode; }
}
else void dll_delete_beg() {
cout<<"position %d of list is not a middle position ", node *temp;
pos;
} if(start == NULL) {
void dll_delete_beg() cout << "\nEmpty list";
{ } else {
node *temp; temp = start;
if(start == NULL) start = start->right;
{
cout<<"\n Empty list"; if(start != NULL)
getch(); start->left = NULL;
return ;
} delete temp;
else }
{ }
temp = start;
start = start -> right; start -> left = NULL; void dll_delete_last() {
free(temp); node *temp;
}
} if(start == NULL) {
void dll_delete_last() cout << "\nEmpty list";
{ } else {
node *temp; temp = start;
if(start == NULL) while(temp->right != NULL) temp = temp->right;
{
cout<<"\n Empty list"; if(temp->left != NULL)
getch(); temp->left->right = NULL;
return ; else
} start = NULL; // If there's only one node
else
{ delete temp;
temp = start; while(temp -> }
right != NULL) temp = temp - }
> right;
temp -> left -> right = NULL; void dll_delete_mid() {
free(temp); int pos, nodectr, i = 1;
temp = NULL; node *temp;
}
} if(start == NULL) {
void dll_delete_mid() cout << "\nEmpty List";
{ } else {
int i = 0, pos, nodectr; cout << "\nEnter the position of the node to delete:
node *temp; if(start ";
== NULL) cin >> pos;
{ nodectr = countnode(start);
cout<<"\n Empty List";
getch(); if(pos > 1 && pos < nodectr) {
return; temp = start;
} while(i < pos) {
else temp = temp->right;
{ i++;
cout<<"\n Enter the position of the node to delete: “; }
cin>>"%d", &pos; nodectr =
countnode(start); if(pos > temp->left->right = temp->right;
nodectr) temp->right->left = temp->left;
{ delete temp;
cout<<"\nthis node does not exist";
getch(); cout << "\nNode deleted.";
return; } else {
} cout << "\nInvalid middle position.";
if(pos > 1 && pos < nodectr) }
{ }
temp = start; }
i = 1;
while(i < pos) int main() {
{ int ch, n;
temp = temp -> right;
i++; while(1) {
} ch = menu();
temp -> right -> left = temp -> left; switch(ch) {
temp -> left -> right = temp -> right; f case 1:
ree(temp); cout << "\nEnter Number of nodes to create: ";
cout<<"\n node deleted.."; cin >> n;
} createlist(n);
else cout << "\nList created.";
{ break;
cout<<"\n It is not a middle position.."; case 2:
getch(); dll_insert_beg();
} break;
} case 3:
} dll_insert_end();
void main(void) break;
{ int ch, n; clrscr(); case 4:
while(1) dll_insert_mid();
{ break;
ch = menu(); case 5:
switch( ch) dll_delete_beg();
{ break;
case 1 : case 6:
cout<<"\n Enter Number of nodes to create: "; dll_delete_last();
cin>>"%d", &n); createlist(n); cout<<"\n List break;
created.."; break; case 7:
case 2 : dll_delete_mid();
dll_insert_beg(); break;
break; case 8:
case 3 : traverse_left_to_right();
dll_insert_end(); break;
break; case 9:
case 4 : traverse_right_to_left();
dll_insert_mid(); break;
break; case 10:
case 5 : cout << "\nNumber of nodes: " <<
dll_delete_beg(); countnode(start);
break; break;
case 6 : case 11:
dll_delete_last(); exit(0);
break; }
case 7 : }
dll_delete_mid(); return 0;
break; }
case 8 :
traverse_left_to_right();
break;
case 9 :
traverse_right_to_left();
break;
case 10 :
cout<<"\n Number of nodes: %d", countnode(start);
break;
case 11:
exit(0);
} getch();
}
}

ANGELICA FELIPE
BSIT 2-1
DOUBLE LINKED LIST

You might also like