mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
129 lines
2.2 KiB
C
129 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
|
|
typedef int element;
|
|
typedef struct queue *queueptr;
|
|
|
|
|
|
typedef struct queue {
|
|
element item;
|
|
queueptr link;
|
|
} queue;
|
|
|
|
|
|
queueptr front = NULL;
|
|
queueptr rear = NULL;
|
|
|
|
int ISEMPTY();
|
|
void addq(element item);
|
|
element deleteq();
|
|
void display();
|
|
|
|
|
|
int main() {
|
|
int choice, item;
|
|
|
|
while (1) {
|
|
printf("\n--- Single Queue Menu ---\n");
|
|
printf("1. Add to Queue\n");
|
|
printf("2. Delete from Queue\n");
|
|
printf("3. Display Queue\n");
|
|
printf("4. Exit\n");
|
|
printf("Enter your choice: ");
|
|
|
|
scanf("%d", &choice);
|
|
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
printf("Enter item to add: ");
|
|
scanf("%d", &item);
|
|
addq(item);
|
|
printf("Item %d added to queue.\n", item);
|
|
break;
|
|
|
|
case 2:
|
|
|
|
item = deleteq();
|
|
printf("Deleted item %d from queue.\n", item);
|
|
break;
|
|
|
|
case 3:
|
|
display();
|
|
break;
|
|
|
|
case 4:
|
|
printf("Exiting program.\n");
|
|
exit(0);
|
|
|
|
default:
|
|
printf("Invalid choice. Please try again.\n");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ISEMPTY() {
|
|
return (front == NULL);
|
|
}
|
|
|
|
|
|
void addq(element item) {
|
|
|
|
queueptr temp = (queueptr)malloc(sizeof(queue));
|
|
|
|
|
|
if (temp == NULL) {
|
|
fprintf(stderr, "Memory allocation failed. The queue is full!\n");
|
|
exit(1);
|
|
}
|
|
|
|
temp->item = item;
|
|
temp->link = NULL;
|
|
|
|
if (ISEMPTY()) {
|
|
front = temp;
|
|
} else {
|
|
rear->link = temp;
|
|
}
|
|
rear = temp;
|
|
}
|
|
|
|
|
|
element deleteq() {
|
|
queueptr temp = front;
|
|
element item;
|
|
|
|
if (ISEMPTY()) {
|
|
fprintf(stderr, "The queue is empty, cannot delete.\n");
|
|
exit(1);
|
|
}
|
|
|
|
item = temp->item;
|
|
front = temp->link;
|
|
free(temp);
|
|
if (ISEMPTY()) {
|
|
rear = NULL;
|
|
}
|
|
return item;
|
|
}
|
|
|
|
|
|
void display() {
|
|
queueptr temp = front;
|
|
|
|
if (ISEMPTY()) {
|
|
printf("Queue is empty.\n");
|
|
return;
|
|
}
|
|
|
|
printf("Queue contents (front to rear): ");
|
|
while (temp != NULL) {
|
|
printf("%d ", temp->item);
|
|
temp = temp->link;
|
|
}
|
|
printf("\n");
|
|
}
|