Programming Problems: Simple Queue +--------------------------------------+ Your
ID: 3697749 • Letter: P
Question
Programming Problems: Simple Queue +--------------------------------------+ Your task in this assignment is complete Phase I of Project II. In Project II with Phase I, you are asked to write a simple queue that handles integer data. Please the skelton code that is included in this assignment for more detail. Requirements +--------------------------------------+ 1. The queue should handle integer data. 2. The queue should be based on linked list. 3. Your code should demonistrate that you can perform the "enqueue" and "dequeue" operation
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
//This is a linked list node which is used to store a queue entry
struct QlinkedlistNode
{
int key;
struct QlinkedlistNode *next;
};
// In the queue the front stores the front node of linked list and rear stores the last node of Linkedlist.
struct Queue
{
struct QlinkedlistNode *front, *rear;
};
// This function is used to create a new linked list node
struct QlinkedlistNode* createnewNode(int k)
{
struct QlinkedlistNode *temporary = (struct QlinkedlistNode*)malloc(sizeof(struct QlinkedlistNode));
temporary->key = k;
temporary->next = NULL;
return temporary;
}
//This function creates an empty queue
struct Queue *createQueue()
{
struct Queue *z = (struct Queue*)malloc(sizeof(struct Queue));
z->front = z->rear = NULL;
return ;
}
// This function is to add a key k to q which means it adds a node to new node after rear and moves rear to next node.
void enQueue(struct Queue *z, int k)
{
// Create a new LL node
struct QlinkedlistNode *temporary = createnewNode(k);
//now If queue is empty, then new node is front and rear both
if (z->rear == NULL)
{
z->front = z->rear = temporary;
return;
}
// Otherwise we will add the new node at the end of queue and change rear of the queue
z->rear->next = temporary;
z->rear = temporary;
}
// This function is used to remove a key from given queue q which means it removes the front node and moves front to next node.
struct QNode *deQueue(struct Queue *z)
{
// first,if queue is empty, return NULL.
if (z->front == NULL)
return NULL;
// next we will store previous front and move front one node ahead
struct QlinkedlistNode *temporary = z->front;
z->front = z->front->next;
// Finally if front becomes NULL, then change rear also as NULL
if (z->front == NULL)
z->rear = NULL;
return temporary;
}
//This is the driver Program to test anove functions
int main()
{
struct Queue *z = createQueue();
enQueue(z, 30);
enQueue(z, 50);
deQueue(z);
deQueue(z);
enQueue(z, 80);
enQueue(z, 90);
enQueue(z, 50);
struct QlinkedlistNode *p = deQueue(z);
T if (p != NULL)
printf("The Dequeued item will be%d", p->key);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.