mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
Refactor queue implementation in QueueInLL.c
This commit is contained in:
+75
-105
@@ -1,128 +1,98 @@
|
|||||||
#include <stdio.h>
|
#include<stdio.h>
|
||||||
#include <stdlib.h>
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
typedef struct node{
|
||||||
|
int data;
|
||||||
|
struct node* next;
|
||||||
|
}Node;
|
||||||
|
|
||||||
typedef int element;
|
Node* front = NULL;
|
||||||
typedef struct queue *queueptr;
|
Node* rear = NULL;
|
||||||
|
|
||||||
|
void enqueue(int value)
|
||||||
|
{
|
||||||
|
Node* newnode = (Node*)malloc(sizeof(Node));
|
||||||
|
|
||||||
typedef struct queue {
|
if(!newnode){
|
||||||
element item;
|
fprintf(stderr, "Memory allocation failed\n");
|
||||||
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
temp->item = item;
|
newnode->data = value;
|
||||||
temp->link = NULL;
|
newnode->next = NULL;
|
||||||
|
|
||||||
if (ISEMPTY()) {
|
if(front == NULL){
|
||||||
front = temp;
|
rear = front = newnode;
|
||||||
} else {
|
}else{
|
||||||
rear->link = temp;
|
rear->next = newnode;
|
||||||
|
rear = newnode;
|
||||||
}
|
}
|
||||||
rear = temp;
|
printf("added %d onto rear\n", value);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dequeue(){
|
||||||
element deleteq() {
|
Node* temp = front;
|
||||||
queueptr temp = front;
|
int item;
|
||||||
element item;
|
if(front == NULL){
|
||||||
|
printf("stack is empty");
|
||||||
if (ISEMPTY()) {
|
return -1;
|
||||||
fprintf(stderr, "The queue is empty, cannot delete.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
item = temp->data;
|
||||||
item = temp->item;
|
front = front->next;
|
||||||
front = temp->link;
|
|
||||||
free(temp);
|
free(temp);
|
||||||
if (ISEMPTY()) {
|
|
||||||
|
if(front== NULL){
|
||||||
rear = NULL;
|
rear = NULL;
|
||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void display(){
|
||||||
void display() {
|
Node* temp = front;
|
||||||
queueptr temp = front;
|
if(front == NULL){
|
||||||
|
printf("Stack is empty");
|
||||||
if (ISEMPTY()) {
|
|
||||||
printf("Queue is empty.\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Queue contents (front to rear): ");
|
while(temp!=NULL){
|
||||||
while (temp != NULL) {
|
printf("%d->",temp->data);
|
||||||
printf("%d ", temp->item);
|
temp = temp->next;
|
||||||
temp = temp->link;
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("NULL\n");
|
||||||
|
}
|
||||||
|
int main(){
|
||||||
|
int choice,value;
|
||||||
|
while(1){
|
||||||
|
printf("\n---queue menu---");
|
||||||
|
printf("\n1. enqueue");
|
||||||
|
printf("\n2. dequeue");
|
||||||
|
printf("\n3. display");
|
||||||
|
printf("\n4. exit");
|
||||||
|
printf("\nEnter your choice: ");
|
||||||
|
scanf("%d", &choice);
|
||||||
|
|
||||||
|
switch(choice){
|
||||||
|
case 1: printf("Enter item to be added: ");
|
||||||
|
scanf("%d", &value);
|
||||||
|
enqueue(value);
|
||||||
|
break;
|
||||||
|
case 2: value = dequeue();
|
||||||
|
if(value!=-1)
|
||||||
|
printf("popped %d", value);
|
||||||
|
break;
|
||||||
|
case 3: display();
|
||||||
|
break;
|
||||||
|
case 4: printf("exiting\n");
|
||||||
|
exit(0);
|
||||||
|
default:
|
||||||
|
printf("enter valid choice bruh");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user