0% found this document useful (0 votes)
13 views2 pages

Ds 13

Uploaded by

anavadyapradeep
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)
13 views2 pages

Ds 13

Uploaded by

anavadyapradeep
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/ 2

Program

/* Experiment No.13 Queue using Linked List


Write a menu driven C Program to implement a queue using linked list with the
following operations:
a. Insert an element to the queue.
b. Delete an element from the queue.
c. Display the content of the queue
17.Anavadya Pradeep.23/09/2024 */
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
void insert(struct Node *);
void delete(struct Node *);
void display(struct Node *);
int main(){
int choice = 0;
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head->data = 0;
head->next = NULL;
printf("Menu: \n1:Insert an element to the queue.\n)
printf("2:Delete an element from the queue.\n"
printf("3:Display the content of the queue\n 4:Exit\n");
while (choice != 4)
{
printf("\nEnter choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert(head);
break;
case 2:
delete (head);
break;
case 3:
display(head);
break;
default:
break;
}
}
}

void insert(struct Node *head)


{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
printf("Enter data: ");
scanf("%d", &temp->data);
while (head->next != NULL)
head = head->next;
temp->next = NULL;
head->next = temp;
printf("Inserted data!!\n");
}

void delete(struct Node *head)


{
if (head->next == NULL)
{
printf("Queue empty!!\n");
return;
}
else
{
struct Node *temp = head->next;
head->next = temp->next;
free(temp);
printf("Deleted data!!\n");
}
}
void display(struct Node *head)
{
if (head->next == NULL)
printf("Queue empty!!\n");
else
{
printf("Queue contents: ");
while ((head = head->next) != NULL)
printf("%d ", head->data);
printf("\n");
}
}

Sample Output

Result
The program was executed and the output verified.

You might also like