Unit 4 Linked List
Unit 4 Linked List
that data. The linked list is a linear data structure that contains a sequence of elements such that each element
links to its next element in the sequence. Each element in a linked list is called "Node".
Single linked list is a sequence of elements in which every element has link to its next element in the
sequence.
Always next part (reference part) of the last node must be NULL.
Example
Insertion
Deletion
Display
Before we implement actual operations, first we need to set up an empty list. First, perform the following steps
before implementing actual operations.
Step 1 - Include all the header files which are used in the program.
Step 2 - Declare all the user defined functions.
Step 3 - Define a Node structure with two members data and next
Step 4 - Define a Node pointer 'head' and set it to NULL.
Step 5 - Implement the main method by displaying operations menu and make suitable function calls in
the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp →
next is equal to NULL).
Step 6 - Set temp → next = newNode.
Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the single linked list...
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
struct Node
{
int data;
struct Node *next;
}
*head = NULL;
void main()
{
int choice,value,choice1,no;
clrscr();
while(1){
mainMenu:
printf("\n\n****** MENU ******\n1. Insert\n2. Display\n3. Delete\n4. Exit\nEnter your
choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
while(1){
printf("Where you want to insert: \n1. At Beginning\n2. At End\n3. Between\
nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the values after which you wanto insert: ");
scanf("%d",&no);
insertBetween(value,no);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
gotomainMenu;
}
gotosubMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: \n1. From Beginning\n2. From End\
n3. Specific\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd();
break;
case 3: printf("Enter the value which you wanto delete: ");
scanf("%d",&no);
removeSpecific(no);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
gotomainMenu;
}
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
}
//INSERTION AT THE BEGINNING OF SLL
void insertAtBeginning(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");
}
//INSERTION AT THE END OF SLL
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
}
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL) // only one node
{
head = NULL;
free(temp);
}
else // more than one node
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
//DELETION OF A NODE FROM THE END OF SLL
void removeEnd()
{
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL) //ONLY ONE NODE
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}
//DELETION OF A SPECIFIC NODE IN SLL
NOTE :
IN EXAMINATION, NORMALLY SPECIFIC FUNCTIONS ARE ASKED FOR IMPLEMENTATION. BUT IF COMPLETE
PROGRAM IS ASKED THEN ALWAYS WRITE A SIMPLE MAIN() FUNCTION UNLIKE WHAT WE DISCUSSED ABOVE. THE
ABOVE MAIN() FUNCTION WAS WRITTEN TO MAKE THE PROGRAM USER FRIENDLY.
main()
int no;
clrscr();
scanf(“%d”,&no);
insertAtBeginning(no);
getch();
{
---
---
---
}
In single linked list, every node points to its next node in the sequence and the last node points to NULL. But in
circular linked list, every node points to its next node in the sequence but the last node points to the first node in
the list.
A circular linked list is a sequence of elements in which every element has a link to its next element in
the sequence and the last element has a link to the first element.
That means circular linked list is similar to the single linked list except that the last node points to the first node in
the list
Example
//IMPLEMENTATION OF CLL
#include<stdio.h>
#include<conio.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();
struct Node
{
int data;
struct Node *next;
}*head = NULL;
void main()
{
int choice1, choice2, value, location;
clrscr();
while(1)
{
printf("\n*********** MENU *************\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: printf("Enter the value to be inserted: ");
scanf("%d",&value);
//while(1)
//{
printf("\nSelect from the following Inserting options\n");
printf("1. At Beginning\n2. At End\n3. After a Node\n4. Cancel\nEnter
your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the location after which you want to insert:
");
scanf("%d",&location);
insertAfter(value,location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Inserting option!!!\n");
}
break;
//}
case 2: //while(1)
//{
printf("\nSelect from the following Deleting options\n");
printf("1. At Beginning\n2. At End\n3. Specific Node\n4. Cancel\nEnter
your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: deleteBeginning();
break;
case 2: deleteEnd();
break;
case 3: printf("Enter the Node value to be deleted: ");
scanf("%d",&location);
deleteSpecific(location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Deleting option!!!\n");
break;
}
//}
EndSwitch: break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select correct option!!!");
}
}
}