Lab Program 13: Create a singly linked list & display it
Aim: Create a singly linked list of N nodes and display it
Algorithm
Step 01: Declare all user defined functions
Step 02: void insertAtEnd(int);
Step 03: void display();
Step 04: Define Node structure with data and next pointer
Step 05: Declare head pointer
main() function
Step 01: Declare integer choice,value,loc;
Step 02: while(1) repeat steps 3 to 9
Step 03: print “*** MENU *** 1. Insert 2. Display 3. Exit "
Step 04: print"Enter your choice: ");
Step 05: read choice
Step 06: If (choice == 1) then print "Enter the value to be insert: "
Read value
call insertAtEnd(value);
Step 07: If (choice == 2) then display();
Step 08: If (choice == 3) then exit function
Step 09: Goto step 3
Step 10: End
void insertAtEnd(int value) function
Step 01: Declare Node pointer variables *newNode, *temp;
Step 02: Create a new node and assign its address to newNode
newNode = (struct Node*)malloc(sizeof(struct Node))
Step 03: newNode->data = value;
Step 04: newNode->next = NULL;
Step 05: if(head == NULL)
head = newNode;
Step 06: else
{
temp=head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
Step 07: printf("\nNew node inserted at end!!!\n");
Step 08: End
void display()
Step 01: Declare Node variable *temp = head
Step 02: if(head == NULL)
printf("\nList is Empty\n");
Step 03: else
{ printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
Step 04: End
Program
#include<stdio.h>
#include<stdlib.h>
void insertAtEnd(int);
void display();
struct Node
{
int data;
struct Node *next;
}*head = NULL;
int main()
{
int choice,value,loc;
while(1)
{
printf("\n*** MENU ***\n1. Insert\n2. Display\n3. Exit\n ");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtEnd(value);
break;
case 2: display();
break;
case 3: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
getch();
return 0;
}
void insertAtEnd(int value)
{
struct Node *newNode, *temp;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
temp=head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nNew node inserted at end!!!\n");
}
void display()
{
struct Node *temp = head;
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
Output:
Lab Program 14: Delete a node from a singly linked list
Aim: Delete a given node (by value) from a singly linked list
Algorithm
Step 01: Declare all user defined functions
Step 02: void insertAtEnd(int);
Step 03: void display();
Step 04: Define Node structure with data and next pointer
Step 05: Declare head pointer
main() function
Step 01: Declare integer choice,value,loc;
Step 02: while(1) repeat steps 3 to 9
Step 03: print “*** MENU *** 1. Insert 2. Display 3. Deletion 4. Exit "
Step 04: print"Enter your choice: ");
Step 05: read choice
Step 06: If (choice == 1) then print "Enter the value to be insert: "
Read value
call insertAtEnd(value)
Step 07: If (choice == 2) then call display()
Step 08: If (choice == 3) then print "Enter the value which you want to delete:
Read loc
Call removeSpecific(loc)
Step 09: If (choice == 4) then exit function
Step 10: Goto step 3
Step 11: End
void insertAtEnd(int value) function
Step 01: Declare Node pointer variables *newNode, *temp;
Step 02: Create a new node and assign its address to newNode
newNode = (struct Node*)malloc(sizeof(struct Node))
Step 03: newNode->data = value;
Step 04: newNode->next = NULL;
Step 05: if(head == NULL)
head = newNode;
Step 06: else
{
temp=head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
Step 07: printf("\nNew node inserted at end!!!\n");
Step 08: End
void display()
Step 01: Declare Node variable *temp = head
Step 02: if(head == NULL)
printf("\nList is Empty\n");
Step 03: else
{ printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
Step 04: End
void removeSpecific(int delValue)
Step 01: struct Node *temp, *preNode;
Step 02: temp = head;
Step 03: while(temp!= NULL) repeat steps3 to 5
Step 04: if(temp -> data == delValue)
{
if(head-> next == NULL) /* check only one node present in the list*/
head= NULL;
else if(head== temp) /* node to be deleted is the first node */
head = temp -> next;
else
preNode-> next = temp -> next;
free(temp);
printf("\nOne node deleted!!!\n\n");
break;
}
else
{
preNode = temp;
temp = temp -> next;
}
Step 05: goto Step 3
Step 06: if(temp== NULL)
printf("\nGiven node not found in the list!!!");
Step 07: End
Program
#include<stdio.h>
#include<stdlib.h>
void insertAtEnd(int);
void display();
void removeSpecific(int);
struct Node
{
int data;
struct Node *next;
}*head = NULL;
int main()
{
int choice,value,loc;
while(1)
{
printf("\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);
insertAtEnd(value);
break;
case 2: display();
break;
case 3: printf("Enter the value which you want to delete: ");
scanf("%d",&loc);
removeSpecific(loc);
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
getch();
return 0;
}
void insertAtEnd(int value)
{
struct Node *newNode, *temp;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
temp=head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nNew node inserted at end!!!\n");
}
void removeSpecific(int delValue)
{
struct Node *temp, *preNode;
temp = head;
while(temp!= NULL)
{
if(temp -> data == delValue)
{
if(head-> next == NULL) /* check only one node present in the list*/
head= NULL;
else if(head== temp) /* node to be deleted is the first node */
head = temp -> next;
else
preNode-> next = temp -> next;
free(temp);
printf("\nOne node deleted!!!\n\n");
break;
}
else
{
preNode = temp;
temp = temp -> next;
}
}
if(temp== NULL)
printf("\nGiven node not found in the list!!!");
}
void display()
{
struct Node *temp = head;
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
Output:
Lab Program 15:Search an element from SLL
Aim: Create a singly linked list and search an element from that list
Algorithm
Step 1: Declare array a[20]
Step 2: Declare integer variable i,x,n
Step 3: print "Enter size of the array”
Step 4: Read n
Step 5: print "Enter array elements:"
Step 6: Read n elements into array using a loop
Step 7: Print "Enter element to search:"
Step 8: Read x
Step 9: Set i=0
Step 10: Repeat for Step 11 and 12 until (i<n)
Step 11: if(a[i]==x)then goto 13
Step 12: i=i+1 goto Step10
Step 13: if(i<n)then Print "Element found at index i”
else print "Element not found"
Step 14: End
Program
#include<stdio.h>
#include<stdlib.h>
void insertAtEnd(int);
void display();
void search( int);
struct Node
{
int data;
struct Node *next;
}*head = NULL;
int main()
{
int choice,value,sValue;
while(1)
{
printf("\n*** MENU ***\n1. Insert\n2. Display\n3.Search \n 4. Exit\nEnter your
choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtEnd(value);
break;
case 2: display();
break;
case 3: printf("Enter the value to search: ");
scanf("%d",&sValue);
search(sValue);
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
getch();
return 0;
}
void insertAtEnd(int value)
{
struct Node *newNode, *temp;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
temp=head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nNew node inserted at end!!!\n");
}
void display()
{
struct Node *temp = head;
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
void search( int sValue)
{
struct Node *temp = head;
int pos=1 ,flag=0;;
while(temp!= NULL)
{
if(temp->data == sValue)
{
flag=1;
break;
}
else
{
temp = temp->next;
pos++;
}
}
if(flag==1)
printf("Value found at position %d",pos);
else
printf("Value not found");
}
Output:
Enter size of the array:5
Enter array elements:
20
10
30
50
40
Enter element to search:30
Element found at index 2