Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

queue using array and linked list.doc

The document contains a C program that implements a queue using both array and linked list data structures. It provides a menu-driven interface for users to enqueue, dequeue, and display elements in both implementations. The program includes function prototypes and definitions for managing the queue operations for both data structures.

Uploaded by

Karri Lokesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

queue using array and linked list.doc

The document contains a C program that implements a queue using both array and linked list data structures. It provides a menu-driven interface for users to enqueue, dequeue, and display elements in both implementations. The program includes function prototypes and definitions for managing the queue operations for both data structures.

Uploaded by

Karri Lokesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>

// Node structure for linked list implementation


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

// Queue structure for array implementation


#define MAX_SIZE 100
struct QueueArray {
int array[MAX_SIZE];
int front, rear;
};

// Function prototypes
void enqueueArray(struct QueueArray *q, int data);
int dequeueArray(struct QueueArray *q);
void displayArray(struct QueueArray *q);
void enqueueLinkedList(struct Node **front, struct Node **rear, int
data);
int dequeueLinkedList(struct Node **front);
void displayLinkedList(struct Node *front);

int main() {
// For array implementation
struct QueueArray qArray;
qArray.front = qArray.rear = -1;

// For linked list implementation


struct Node *frontLL = NULL, *rearLL = NULL;

int choice, data;

do {
printf("\nQueue Implementation Menu:\n");
printf("1. Enqueue (Array)\n");
printf("2. Dequeue (Array)\n");
printf("3. Display (Array)\n");
printf("4. Enqueue (Linked List)\n");
printf("5. Dequeue (Linked List)\n");
printf("6. Display (Linked List)\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to enqueue (array): ");
scanf("%d", &data);
enqueueArray(&qArray, data);
break;
case 2:
data = dequeueArray(&qArray);
if (data != -1)
printf("Dequeued element (array): %d\n", data);
break;
case 3:
displayArray(&qArray);
break;
case 4:
printf("Enter data to enqueue (linked list): ");
scanf("%d", &data);
enqueueLinkedList(&frontLL, &rearLL, data);
break;
case 5:
data = dequeueLinkedList(&frontLL);
if (data != -1)
printf("Dequeued element (linked list): %d\n", data);
break;
case 6:
displayLinkedList(frontLL);
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please enter a valid choice.\n");
}
} while (choice != 7);

return 0;
}

// Functions for array implementation


void enqueueArray(struct QueueArray *q, int data) {
if (q->rear == MAX_SIZE - 1) {
printf("Queue overflow (array).\n");
return;
}
if (q->front == -1)
q->front = 0;
q->rear++;
q->array[q->rear] = data;
}

int dequeueArray(struct QueueArray *q) {


if (q->front == -1 || q->front > q->rear) {
printf("Queue underflow (array).\n");
return -1;
}
int data = q->array[q->front];
q->front++;
return data;
}

void displayArray(struct QueueArray *q) {


if (q->front == -1) {
printf("Queue is empty (array).\n");
return;
}
printf("Queue elements (array): ");
for (int i = q->front; i <= q->rear; i++)
printf("%d ", q->array[i]);
printf("\n");
}

// Functions for linked list implementation


void enqueueLinkedList(struct Node **front, struct Node **rear, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*rear == NULL) {
*front = *rear = newNode;
return;
}
(*rear)->next = newNode;
*rear = newNode;
}

int dequeueLinkedList(struct Node **front) {


if (*front == NULL) {
printf("Queue underflow (linked list).\n");
return -1;
}
struct Node* temp = *front;
int data = temp->data;
*front = (*front)->next;
free(temp);
return data;
}

void displayLinkedList(struct Node *front) {


if (front == NULL) {
printf("Queue is empty (linked list).\n");
return;
}
printf("Queue elements (linked list): ");
while (front != NULL) {
printf("%d ", front->data);
front = front->next;
}
printf("\n");
}

You might also like