0% found this document useful (0 votes)
4 views

DSU EXP 22

The document provides a C program to implement a linear queue using a linked list, including functions for inserting, deleting, and displaying queue elements. It also includes practical examples of enqueue and dequeue operations, as well as exercises differentiating between stacks and queues and discussing the advantages of using linked lists for queue implementation. Key benefits of linked lists include dynamic sizing, efficient operations, and no memory wastage.

Uploaded by

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

DSU EXP 22

The document provides a C program to implement a linear queue using a linked list, including functions for inserting, deleting, and displaying queue elements. It also includes practical examples of enqueue and dequeue operations, as well as exercises differentiating between stacks and queues and discussing the advantages of using linked lists for queue implementation. Key benefits of linked lists include dynamic sizing, efficient operations, and no memory wastage.

Uploaded by

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

EXP 22:

Write a 'C' Program to perform INSERT and DELETE operations on Linear Queue
using a Linked List.

#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("\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;

}
Practical Related:

Write a C program to implement a linear queue using linked list with operations
for enqueue (15), enqueue (48), enqueue (69), dequeue, enqueue (12), enqueue
(23), dequeue, dequeue.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} node;

typedef struct Queue {

node* front;

node* rear;

} queue;

// Function to create a new node

node* createNode(int data)

node* newNode = (node*)malloc(sizeof(node));

if (newNode == NULL)

return NULL;
newNode->data = data;

newNode->next = NULL;

return newNode;

queue* createQueue()

queue* newQueue = (queue*)malloc(sizeof(queue));

newQueue->front = newQueue->rear = NULL;

return newQueue;

int isEmpty(queue* q)

return q->front == NULL;

void enqueue(queue* q, int data)

node* newNode = createNode(data);

if (!newNode) {

printf("Queue Overflow!\n");

return;

if (q->rear == NULL)

{
q->front = q->rear = newNode;

return;

q->rear->next = newNode;

q->rear = newNode;

int dequeue(queue* q)

if (isEmpty(q)) {

printf("Queue Underflow\n");

return -1;

node* temp = q->front;

q->front = q->front->next;

if (q->front == NULL)

q->rear = NULL;

int data = temp->data;

free(temp);

return data;

int peek(queue* q)

if (isEmpty(q))
return -1;

return q->front->data;

void printQueue(queue* q)

node* temp = q->front;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main()

// Create a new queue

queue* q = createQueue();

// Enqueue elements into the queue

enqueue(q, 15);

enqueue(q, 48);

enqueue(q, 69);

// Print the queue

printf("Queue: ");

printQueue(q);
dequeue(q);

// Print the queue

printf("Queue: ");

printQueue(q);

enqueue(q,12);

enqueue(q,23);

// Print the queue

printf("Queue: ");

printQueue(q);

dequeue(q);

// Print the final queue

printf("Queue: ");

printQueue(q);

return 0;

Exercise

1. Differentiate between stack and queue.


2. Give advantages of using linked list in implementation of Queue.

1 . Dynamic Size:

 A linked list provides a dynamically allocated structure, meaning the size of the queue
can grow or shrink as needed without the need for resizing or reallocation, unlike arrays
with fixed sizes.

2. Efficient Enqueue and Dequeue Operations:

 Enqueue (adding to the rear) and Dequeue (removing from the front) operations in a
queue implemented with a linked list are both efficient. 3. No Memory Wastage:

4. No Shifting Required:

 Unlike arrays, where dequeuing may require shifting elements to maintain order, in a
linked list, no such shifting is necessary. The queue’s front is simply moved by updating
the head pointer.

5. Easily Handles Overflow:

 In a linked list, there’s no concept of "overflow" (unless the system runs out of memory)
because the size is only limited by available memory.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy