Experiment 13: Data Structure & Algorithm Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

DATA STRUCTURE & ALGORITHM LAB

EXPERIMENT ~ 13
DATE: 8 June,2021
NAME: Ruchi Jain
CLASS/SEC: CSE-A
REGISTRATION NO: 10320210040

1) Write a program to implement QUEUES using linked lists.


PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\nMain Menu\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice:");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice!!\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value:\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("%d ",ptr -> data);
ptr = ptr -> next;
}
}
}
ALGORITHM:

o Step 1: create a menu n ask user to choose among input delete display or exit.

o Step 2: using switch cases give the choosen input.

o Step 3: define input. Allocate the space for the new node PTR

o Step 4: SET PTR -> DATA = VAL

o Step 5: IF FRONT = NULL


SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
o Step 6 : define delete.

 IF FRONT = NULL


Write " Underflow " and exit.

Step 7: SET PTR = FRONT

o Step 8: SET FRONT = FRONT -> NEXT

o Step 9: FREE PTR

o Step 10: define display.

If(front==null) print empty queue

Else print values (ptr->data)

ptr=ptr->next

o Step 11: END

OUTPUT:

You might also like