Created
May 1, 2020 06:21
-
-
Save shreyanshi2228/fa31b568e72a4a4a0f0402e8ee25fe1a to your computer and use it in GitHub Desktop.
I have implemented a Queue using singly linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct node | |
{ | |
int data; | |
struct node* link; | |
}; | |
struct node *rear = NULL; | |
struct node *front= NULL; | |
void push (int x ) | |
{ | |
struct node* temp = (struct node*)malloc(sizeof(struct node)); | |
if (!temp) | |
{ | |
printf("Sorry:/ We cannot allocate memory"); | |
return; | |
} | |
printf("Enter element bossyyyy which you want to push\n"); | |
scanf("%d", &x); | |
temp->data = x; | |
temp->link = NULL; | |
if(front == NULL && rear == NULL) | |
{ | |
front = rear = temp; | |
return; | |
} | |
rear->link = temp; | |
rear = temp; | |
} | |
int pop() | |
{ | |
struct node* temp = front; | |
if (front == NULL && rear == NULL) | |
{ | |
printf("Sorry!!! Queue is empty. We have nothing to pop\n"); | |
return 0; | |
} | |
if (front == rear) | |
{ | |
front = rear = NULL; | |
} | |
else | |
{ | |
front = front->link; | |
free(temp); | |
} | |
} | |
void display() | |
{ | |
struct node *temp = front; | |
temp = front; | |
if (front == NULL && rear ==NULL) | |
{ | |
printf("Awwww queue is empty\n"); | |
return; | |
} | |
while(temp!= NULL) | |
{ | |
printf("%d ",temp->data); | |
temp = temp->link; | |
} | |
printf("\n"); | |
} | |
int main() | |
{ | |
int choose_number, e; | |
while(1) | |
{ | |
printf("1. Push element\n"); | |
printf("2. Pop element\n"); | |
printf("3. Display elements\n"); | |
printf("Choose a number my dear friend\n"); | |
scanf("%d", &choose_number); | |
switch (choose_number) | |
{ | |
case 1: push(e); | |
break; | |
case 2: e = pop(); | |
printf("e => %d\n", e); | |
break; | |
case 3: display(); | |
break; | |
default: printf(" You have an Invalid choice\n"); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment