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