Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Linear Queue: Joy Mukherjee Programming & Data Structures

The document describes a linear queue implemented using a linked list. It defines a node struct with data and next pointer fields. A queue struct contains front and rear pointers to the first and last nodes. Nodes are added to the rear of the queue using an enqueue function. Nodes are removed from the front using a dequeue function, which removes the front node, updates front to point to the next node, and frees the memory of the removed node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

Linear Queue: Joy Mukherjee Programming & Data Structures

The document describes a linear queue implemented using a linked list. It defines a node struct with data and next pointer fields. A queue struct contains front and rear pointers to the first and last nodes. Nodes are added to the rear of the queue using an enqueue function. Nodes are removed from the front using a dequeue function, which removes the front node, updates front to point to the next node, and frees the memory of the removed node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Linear Queue

Joy Mukherjee
Programming & Data Structures
Linear Queue using Linked List

typedef struct node


{
int data;
struct node *next;
}node;

typedef struct queue{


node *front;
node *rear;
}queue;
Linear Queue using Linked List

The keyword struct is used to create an user-defined


datatype
Here struct node is an user-defined datatype
It has two members: Data of type int & next of type
struct node *.
The keyword typedef tells the compiler that struct node
(written before {) and node (written after }) have
identical properties .
Queue has two members: front and rear of type node *(
or struct node *).
Linear Queue

12 2000 2 3000 99 NULL

Q->front= 1000 2000 Q->rear=3000


queue * enqueue(queue *Q, int data)
node *new_node = NULL;

// Allocate memory from heap for new_node of size sizeof(node)


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

// Put data into new_node


new_node->data = data;

// Set next of new_node as NULL


new_node->next = NULL;
queue * enqueue(queue *Q, int data)
if(isEmpty(Q)) {
Q->front = new_node;
Q->rear = new_node;
return Q;
}

// Set new_node as the next of Q->rear


Q->rear->next = new_node;

// Set the rear to new_node


Q->rear = new_node;
return Q;
Q = enqueue(Q, 33)
node *new_node = (node *)malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;

12 2000 2 3000 99 NULL 33 NULL

Q->front = 1000 2000 Q->rear=3000 new_node=500

Q->rear->next = new_node;
Q->rear = new_node;
return Q;

12 2000 2 3000 99 500 33 NULL

Q->front = 1000 2000 3000 Q->rear=new_node=500


queue * dequeue(queue *Q)
if (isEmpty(Q)) {
printf("queue is Empty.\n"); return Q;
}
node *del_node = Q->front;

Q->front = Q->front->next;
if(Q->rear == del_node)
Q->rear = NULL;

free(del_node);
return Q;
Q = dequeue(Q)
12 2000 2 3000 99 500 33 NULL

del_node=Q->front = 1000 2000 3000 Q->rear=500

node *del_node = Q->front;

12 2000 2 3000 99 500 33 NULL

del_node= 1000 Q->front =2000 3000 Q->rear=500

Q->front = Q-> front ->next;

2 3000 99 500 33 NULL

Q->front =2000 3000 Q->rear=500

free(del_node);
return Q;

You might also like