Experiment 4
Implementation of Queue
Program 4b
Aim : To implement queues using linked lists using C-program
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* create(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
void Add(struct node** top, struct node** end) {
int d;
printf("\nEnter the data: ");
scanf("%d", &d);
struct node* newnode = create(d);
if (*top == NULL && *end == NULL) {
*top = newnode;
*end = newnode;
return;
} else {
(*end)->next = newnode;
*end = newnode;
return;
void deq(struct node** top, struct node** end) {
if (*top == NULL) {
printf("\nQueue is empty\n");
return;
struct node* temp = *top;
*top = (*top)->next;
free(temp);
void display(struct node* top, struct node* end) {
if (top == NULL && end == NULL) {
printf("\nQueue is empty\n");
return;
} else {
printf("\nThe queue elements are:\n");
while (top != NULL) {
printf("%d\n", top->data);
top = top->next;
printf("\n");
return;
}
}
int main() {
struct node* top = NULL;
struct node* end = NULL;
int ch = 0;
while (ch != 5) {
printf("\n1: Enqueue\n");
printf("2: Dequeue\n");
printf("3: Display Queue\n");
printf("4: Exit\n\n");
printf("Enter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
Add(&top, &end);
break;
case 2:
deq(&top, &end);
break;
case 3:
display(top, end);
break;
return 0;
}
Output:
Result:
Operations on queue were done using linked lists.