You are on page 1of 14

EXPERIMENT: 3

NAME: AMAN YADAV


UID:19BCS2402
SEMESTER: 3rd
BRANCH: BE-CSE
CLASS: CSE – 20 (B)

SUBJECT CODE: CSP - 231


SUBJECT NAME: DS LAB

I. AIM: Write a menu driven program that maintains a linear linked list whose
elements are stored in on ascending order and implements the following
operations (using separate functions):

a) Insert a new element

b) Delete an existing element

c) Search an element

d) Display all the elements

LOGISTIC USED :

The program is the process to insert new elements in the desired place as well as at the end of
the array. And, also the program will delete the element from the desired position and will
display the new array.
II. ALGORITHM/FLOWCHART: FOR
INSERT A NEW ELEMENT:
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]

Step 2: SET NEW_NODE = PTR


Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
FOR DELETE AN ELEMENT:
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
FOR SEARCH AN ELEMENT:
Step 1: SET PTR = HEAD.
Step 2: Set I = 0.
STEP 3: IF PTR = NULL.
STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL.
STEP 5: if ptr → data = item.
STEP 6: I = I + 1.
STEP 7: PTR = PTR → NEXT.
STEP 8: EXIT.
FOR DISPLAY ELEMENTS
STEP1: Define a node current which initially points to the head of the list.
STEP2: Traverse through the list till current points to null.
STEP3: Display each node by making current to point to node next to it in
each iteration
IV CODING

#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
void search();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1){
printf("\n***SINGLE LINKED LIST OPERATIONS:****\n");
printf("\n MENU \n");
printf(" \n");
printf("\n 1.Create \n");
printf("\n 2.Display \n");
printf("\n 3.Insert at the beginning\n");
printf("\n 4.Insert at the end \n"); printf("\n
5.Insert at specified position \n"); printf("\n
6.Delete from beginning\n"); printf("\n
7.Delete from the end\n");
printf("\n 8.Delete from specified position\n");
printf("\n 9.Search an element\n");
printf("\n 10.Exit \n");
printf("\n \n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
case 3:

case 2:
case 4: create();
break;

case 5: display();
break;

case 6: insert_begin();
break;

case 7: insert_end();
break;

case 8: insert_pos();
break;

case 9: delete_begin();
break;

default: delete_end();
break;

} delete_pos();
} break;

exit(0);
break;

printf("\n Wrong Choice:\n");


break;
return 0;
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = start;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr==NULL)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}

}
V. Observations/Discussions/ Complexity Analysis

In this experiment we have learnt that how to search an element in an linked


list which is helpful in our day to day life. The complex part was to print the
statement where we have to display that the element is not present in the
linked list. Required line was one and because of switch loop the output was
shown several times, which we need to show once and only in the case when
the element is not in the linked list.

VI. Result/Output/Writing Summary:


VII. Learning outcomes (What I HAVE LEARNT):

1. We have learnt how to search element in a set of


elements in an arrange numbers.
2. How to control the functions of linked list
properly
3. How to search, insert, delete an element in
linked list.

You might also like