Dsa Manual (2022) - 1-16
Dsa Manual (2022) - 1-16
Write a program that uses functions to perform the following operations on singly linked
list.
i)Creation ii) Insertion iii) Deletion iv)Traversal
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n");
printf("5.Delete from last\n6.Delete node after specified location\n7.Search for an
element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:beginsert();
break;
case 2:lastinsert();
break;
case 3:randominsert();
break;
case 4:begin_delete();
break;
case 5:last_delete();
break;
1
case 6:random_delete();
break;
case 7:search();
break;
case 8:display();
break;
case 9:exit(0);
break;
default:printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *newnode;
int item;
newnode = (struct node *) malloc(sizeof(struct node *));
if(newnode == NULL)
{
printf("\nmemory allocation error");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
newnode->data = item;
newnode->next = head;
head = newnode;
printf("\nNode inserted");
}
void lastinsert()
{
struct node *newnode,*current;
int item;
newnode = (struct node*)malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\nmemory allocation error");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
newnode->data = item;
2
if(head == NULL)
{
newnode -> next = NULL;
head = newnode;
printf("\nNode inserted");
}
else
{
current = head;
while (current -> next != NULL)
{
current = current -> next;
}
current->next = newnode;
newnode->next = NULL; printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *newnode, *current;
newnode = (struct node *) malloc (sizeof(struct node));
if(newnode == NULL)
{
printf("\nmemory allocation error");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
newnode->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
current=head;
for(i=0;i<loc;i++)
{
current = current->next;
if(current == NULL)
{
printf("\ncan't insert\n");
return;
}
3
newnode ->next = current ->next;
current ->next = newnode;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *current;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
current = head;
head = current->next;
free(current);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *current,*previous;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
current = head;
while(current->next != NULL)
{
previous = current;
current = current ->next;
}
previous->next = NULL;
free(current);
printf("\nDeleted Node from the last ...\n");
}
4
}
void random_delete()
{
struct node *current,*previous;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
current=head;
for(i=0;i<loc;i++)
{
previous = current;
current = current->next;
if(current == NULL)
{
printf("\nCan't delete");
return;
}
}
previous ->next = current ->next;
free(current);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *current;
int item,i=0,flag;
current = head;
if(current == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (current!=NULL)
{
if(current->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
5
{
flag=1;
}
i++;
current = current -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *current;
current = head;
if(current == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values \n");
while (current!=NULL)
{
printf("\n%d",current->data);
current = current -> next;
}
}
}
6
OUTPUT:
7
8
2. Write a program that uses functions to perform the following operations on doubly linked
list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *llink;
struct node *rlink;
int data;
};
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n");
printf("5.Delete from last\n6.Delete the node after the given
data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:insertion_beginning();
break;
case 2:insertion_last();
break;
case 3:insertion_specified();
break;
case 4:deletion_beginning();
break;
9
case 5:deletion_last();
break;
case 6:deletion_specified();
break;
case 7:search();
break;
case 8:display();
break;
case 9:exit(0);
break;
default:printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *newnode;
int item;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
newnode->rlink = NULL;
newnode->llink=NULL;
newnode->data=item;
head=newnode;
}
else
{
newnode->data=item;
newnode->llink=NULL;
newnode->rlink = head;
head->llink=newnode;
head=newnode;
}
printf("\nNode inserted\n");
}
10
void insertion_last()
{
struct node *newnode,*current;
int item;
newnode = (struct node *) malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
newnode->data=item;
if(head == NULL)
{
newnode->rlink = NULL;
newnode->llink = NULL;
head = newnode;
}
else
{
current = head;
while(current->rlink!=NULL)
{
current = current->rlink;
}
current->rlink = newnode;
newnode ->llink=current;
newnode->rlink = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *newnode,*current;
int item,loc,i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL)
{
printf("\n OVERFLOW");
}
else
{
current=head;
11
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
current = current->rlink;
if(current == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
newnode->data = item;
newnode->rlink = current->rlink;
current->rlink->llink=newnode;
newnode -> llink = current;
current->rlink = newnode;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *current;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->rlink == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
current = head;
head = head -> rlink;
head -> llink = NULL;
free(current);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *current;
12
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->rlink == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
current = head;
while(current->rlink!=NULL)
{
current = current->rlink;
}
current -> llink -> rlink = NULL;
free(current);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *current,*temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
current = head;
while(current -> data != val)
current = current -> rlink;
if(current -> rlink == NULL)
{
printf("\nCan't delete\n");
}
else if(current -> rlink -> rlink == NULL)
{
current ->rlink = NULL;
}
else
{
temp = current -> rlink;
current -> rlink = temp -> rlink;
temp -> rlink -> llink = current;
free(temp);
printf("\nnode deleted\n");
}
13
}
void display()
{
struct node *current;
printf("\n printing values...\n");
current = head;
while(current != NULL)
{
printf("%d\n",current->data);
current=current->rlink;
}
}
void search()
{
struct node *current;
int item,i=0,flag;
current = head;
if(current == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (current!=NULL)
{
if(current->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
current = current -> rlink;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
14
OUTPUT:
15
16