Exp 8
Exp 8
Aim:
To Write a C Program for linked list implement of List using
structure and pointer.
Algorithm:
Step1: Create node and set the value as NULL.
Step 2: Read the list operation type.
Step 3: If operation type is create then process the following steps.
1. Allocate memory for node.
2. Read data and assign neat field as NULL.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node.
2. Read data for data area of the node.
3. Read the position the Data to be inserting.
4. Availability of the position is true then assign node as first.
5. If it is in the middle then insert based on the position.
Step5: If operation type is deleting then do the following steps.
● Read the position.
● Check list is Empty .If it is true display the message List empty.
● If position is first then assign head to the next element.
● If the position is last, then move the current node to previous and
make its next as null.
● If it is in middle, then move to that position and link the next
element to the previous node.
1.If position is enter Mediate.
1. Move the cur to required postion.
void main()
{ int choice,value,choice1,loc1,loc2;
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 two values where you
wanto insert: "); scanf("%d%d",&loc1,&loc2);
insertBetween(value,loc1,loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: \n1. From Beginning\n2.
From End\n3. Spesific\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd(value);
break;
case 3: printf("Enter the value which you wanto delete: ");
scanf("%d",&loc2);
removeSpecific(loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
}
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
} }}
void removeEnd()
{
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}
void removeSpecific(int delValue)
{
struct Node *temp1 = head, *temp2;
while(temp1->data != delValue)
{
if(temp1 -> next == NULL){
printf("\nGiven node not found in
the list!!!"); goto functionEnd;
}
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = temp1 -> next;
free(temp1);
printf("\nOne node deleted!!!\n\n");
functionEnd:
}
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
Output:
Result: Thus the C Program to implement List using Linked list has been
executed successfully.