Skip to content

Instantly share code, notes, and snippets.

@AhmedMaad
Created December 18, 2022 23:12
Show Gist options
  • Save AhmedMaad/9ee053d935c743242a3e79cb4fc0e22c to your computer and use it in GitHub Desktop.
Save AhmedMaad/9ee053d935c743242a3e79cb4fc0e22c to your computer and use it in GitHub Desktop.
Super Market Stack with Queue using LinkedList
#include <iostream>
using namespace std;
/*
Inside a supermarket, there was a queue of customers at the cashier (4 customers),
named "1,2,3, and 4" The first customer paid for her products and then left the market,
after that a VIP customer named "5" entered the shop and he will pay for his products before customers "2,3, and 4".
Implement this scenario using Stack with Queue using LinkedList , and then print the final queue.
*/
struct node{
int data;
node *next;
};
node *head = NULL;
void enqueue(int value){
node* new_node = new node;
new_node-> data = value;
new_node -> next = NULL;
if(head == NULL)
head = new_node;
else{
node * temp = head;
while(temp->next != NULL)
temp = temp -> next;
temp -> next = new_node;
}
}
void dequeue(){
node *first_node = head;
head = first_node->next;
delete(first_node);
}
void push(int value){
node* new_node = new node;
new_node -> data = value;
if(head == NULL){
new_node -> next = NULL;
head = new_node;
}
else{
new_node->next = head;
head = new_node;
}
}
void display(){
node * temp = head;
while(temp != NULL){
cout<<'\t'<<temp->data<<endl;
temp = temp->next;
}
}
int main() {
enqueue(1); //Insert Last
enqueue(2); //Insert Last
enqueue(3); //Insert Last
enqueue(4); //Insert Last
display(); //Print All elements
cout<<endl;
dequeue(); //Delete First
display(); //Print All elements
cout<<endl;
push(5); //Insert First
display(); //Print All elements
cout<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment