mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
99 lines
1.9 KiB
C
99 lines
1.9 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
|
|
typedef struct node{
|
|
int data;
|
|
struct node* next;
|
|
}Node;
|
|
|
|
Node* front = NULL;
|
|
Node* rear = NULL;
|
|
|
|
void enqueue(int value)
|
|
{
|
|
Node* newnode = (Node*)malloc(sizeof(Node));
|
|
|
|
if(!newnode){
|
|
fprintf(stderr, "Memory allocation failed\n");
|
|
exit(1);
|
|
}
|
|
|
|
newnode->data = value;
|
|
newnode->next = NULL;
|
|
|
|
if(front == NULL){
|
|
rear = front = newnode;
|
|
}else{
|
|
rear->next = newnode;
|
|
rear = newnode;
|
|
}
|
|
printf("added %d onto rear\n", value);
|
|
|
|
|
|
}
|
|
|
|
int dequeue(){
|
|
Node* temp = front;
|
|
int item;
|
|
if(front == NULL){
|
|
printf("stack is empty");
|
|
return -1;
|
|
}
|
|
item = temp->data;
|
|
front = front->next;
|
|
free(temp);
|
|
|
|
if(front== NULL){
|
|
rear = NULL;
|
|
}
|
|
return item;
|
|
}
|
|
|
|
void display(){
|
|
Node* temp = front;
|
|
if(front == NULL){
|
|
printf("Stack is empty");
|
|
return;
|
|
}
|
|
|
|
while(temp!=NULL){
|
|
printf("%d->",temp->data);
|
|
temp = temp->next;
|
|
|
|
}
|
|
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;
|
|
}
|