Write a program
to implement Doubly Linked List.
/*
File:
DSPrgm03.c
Author:
Aditya Saini
Date: Oct
11, 2019
Description:
Program to implement the Doubly Linked List.
*/
#include <stdio.h>
#include <alloc.h>
//Structure definition
typedef struct node
{
int data;
struct node *next;
struct node *previous;
} node;
//Start and End pointer of Doubly Linked List
node *start = NULL;
node *end = NULL;
void insert_at_begin (void);
void insert_at_end (void);
void delete_from_begin (void);
void delete_from_end (void);
void traverse_from_start (void);
void traverse_from_end (void);
int main (void)
{
int choice;
//Menu
clrscr();
while (1)
{
printf ("\n\t***Menu***\n");
printf ("1. Insert at beginning of
Doubly Linked List\n");
printf ("2. Insert at end of Doubly
Linked List\n");
printf ("3. Delete from beginning of
Doubly Linked List\n");
printf ("4. Delete from end of
Doubly Linked List\n");
printf ("5. Traverse Doubly Linked List
from start\n");
printf ("6. Traverse Doubly Linked
List from end\n");
printf ("7. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
insert_at_begin ( );
break;
case 2:
insert_at_end ( );
break;
case 3:
delete_from_begin ( );
break;
case 4:
delete_from_end ( );
break;
case 5:
traverse_from_start ( );
break;
case 6:
traverse_from_end ( );
break;
case 7:
return 0;
default:
printf ("\nError! Wrong
Choice.\n");
}
}
};
//Function to insert the node at the beginning of
the Doubly Linked List.
void insert_at_begin (void)
{
node *new;
//Creating node
new = (node *) malloc (sizeof (node));
if (new == NULL)
{
printf ("\nError! Memory Full,
Please delete some elements to insert new one.\n");
return;
}
//Set data value
printf ("\nInput element: ");
scanf ("%d", &new ->
data);
new -> previous = NULL;
new -> next = NULL;
//Placing first node of Doubly Linked
List.
if (start == NULL)
{
start = new;
end = new;
return;
}
//Placing node at the beginning of the
Doubly Linked List.
new -> next = start;
start -> previous = new;
start = new;
};
//Function to insert the node at the end of the
Doubly Linked List.
void insert_at_end (void)
{
node *new;
//Creating node
new = (node *) malloc (sizeof (node));
if (new == NULL)
{
printf ("\nError! Memory Full,
Please delete some elements to insert new one.\n");
return;
}
//Set data value
printf ("\nInput element: ");
scanf ("%d", &new ->
data);
new -> previous = NULL;
new -> next = NULL;
//Placing first node of Doubly Linked
List.
if (start == NULL)
{
start = new;
end = new;
return;
}
//Placing node at the end of the Doubly
Linked List.
end -> next = new;
new -> previous = end;
end = new;
};
//Function to delete the node from the beginning of
the Doubly Linked List.
void delete_from_begin (void)
{
node *temp;
if (start == NULL)
{
printf ("\nError! Empty Linked
List.\n");
return;
}
//If there is only one node in Doubly
Linked List.
if (start -> next == NULL)
{
free (start);
start = NULL;
end = NULL;
return;
}
//Delete node from the beginning of the
Doubly Linked List.
temp = start;
start = start -> next;
start -> previous = NULL;
free (temp);
};
//Function to delete the node from the end of the
Doubly Linked List.
void delete_from_end (void)
{
node *temp;
if (start == NULL)
{
printf ("\nError! Empty Linked
List.\n");
return;
}
//If there is only one node in Doubly
Linked List.
if (end -> previous == NULL)
{
free (start);
start = NULL;
end = NULL;
return;
}
//Delete node from the end of the Doubly
Linked List.
temp = end;
end = temp -> previous;
end -> next = NULL;
free (temp);
};
//Function to traverse the Doubly Linked List from
start.
void traverse_from_start (void)
{
node *temp;
temp = start;
if (temp == NULL)
{
printf ("\nError! Empty Linked
List.\n");
return;
}
printf ("Linked List: ");
while (temp != NULL)
{
printf ("%d ", temp -> data);
temp = temp -> next;
}
printf ("\n");
};
//Function to traverse the Doubly Linked List from
end.
void traverse_from_end (void)
{
node *temp;
temp = end;
if (temp == NULL)
{
printf ("\nError! Empty Linked
List.\n");
return;
}
printf ("Reverse Linked List: ");
while (temp != NULL)
{
printf ("%d ", temp -> data);
temp = temp -> previous;
}
printf ("\n");
};
Output
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 1
Input element: 5
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 1
Input element: 4
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 1
Input element: 3
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 1
Input element: 2
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 1
Input element: 1
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 2
Input element: 6
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 2
Input element: 7
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 2
Input element: 8
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 2
Input element: 9
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 2
Input element: 10
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 5
Linked List:
1 2 3
4 5 6
7 8 9 10
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 6
Reverse Linked List: 10
9 8 7
6 5 4
3 2 1
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 3
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 3
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 4
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 4
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 5
Linked List:
3 4 5
6 7 8
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 6
Linked List:
8 7 6
5 4 3
***Menu***
1. Insert at beginning of Doubly Linked List
2. Insert at end of Doubly Linked List
3. Delete from beginning of Doubly Linked List
4. Delete from end of Doubly Linked List
5. Traverse Doubly Linked List from start
6. Traverse Doubly Linked List from end
7. Exit
Choice: 7
No comments:
Post a Comment
Please do not post spam links.