Lab Program 13-15
Lab Program 13-15
main() function
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 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
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();
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;
}
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 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);
}
}
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