0% found this document useful (0 votes)
31 views6 pages

Experiment No 8.

Uploaded by

Prince Kaswala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Experiment No 8.

Uploaded by

Prince Kaswala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

Title:- Implement Stack / Linear Queue ADT using Linked List.

The array implementation can not be used for the large scale applications where the
queues are implemented. One of the alternatives of array implementation is linked list
implementation of queue.

The storage requirement of linked representation of a queue with n elements is o(n)


while the time requirement for operations is o(1).

In a linked queue, each node of the queue consists of two parts i.e. data part and the
link part. Each element of the queue points to its immediate next element in the
memory.

In the linked queue, there are two pointers maintained in the memory i.e. front pointer
and rear pointer. The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element of the queue.

Insertion and deletions are performed at rear and front end respectively. If front and rear
both are NULL, it indicates that the queue is empty.

The linked representation of queue is shown in the following figure.

Insertion Algorithm

○ Step 1: Allocate the space for the new node PTR

○ Step 2: SET PTR -> DATA = VAL


○ Step 3: 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]

○ Step 4: END

Deletion Algorithm

○ Step 1: IF FRONT = NULL


Write " Underflow "
Go to Step 5
[END OF IF]

○ Step 2: SET PTR = FRONT

○ Step 3: SET FRONT = FRONT -> NEXT

○ Step 4: FREE PTR

○ Step 5: END

CODE:-

#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("\n*************************Main Menu*****************************\n");

printf("\n===============================================================
==\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("\n%d\n",ptr -> data);

ptr = ptr -> next;

} } }

OUTPUT:-

You might also like