Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AakashCode12/63aea85e9bf6a70d4f9646e1fc51501c to your computer and use it in GitHub Desktop.
Save AakashCode12/63aea85e9bf6a70d4f9646e1fc51501c to your computer and use it in GitHub Desktop.
Stacks and Queue using Linked List data Structure
#include <stdio.h>
#include <stdlib.h>
//todo structure & pointers declaration
struct node
{
int data;
struct node *next;
};
struct node *front, *rear, *newnode, *temp;
int flagforstart=0;//?this zero indicates that there is no element in queue at start
// todo function prototyping
void enqueue();
void dequeue();
void peek();
void display();
//todo enqueue function
void enqueue(){
if (flagforstart == 0){
front=NULL;
newnode=(struct node*) malloc(sizeof(struct node));
printf("\nEnter The Value you want to enqueue : ");
scanf("%d",&newnode->data);
newnode->next=NULL;
front=rear=newnode;//!idhar teen log pehle wale pe point karenge
flagforstart++;//!indicates one linked list is added
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter The Value you want to enqueue : ");
scanf("%d", &newnode->data);
newnode->next =NULL;
rear->next = newnode;
rear = newnode;
flagforstart++; //!indicates one linked list is added
}
}
//todo enqueue function
void dequeue(){
if(flagforstart==0){
printf("\nQueue Underflow\n");
}
else
{
temp = front;
front = temp->next;
free(temp);
flagforstart--;
}
}
//todo peek function
void peek()
{
temp = front;
if (flagforstart == 0)
{
printf("\nEmpty stack\n");
}
else if (front == NULL)
{
printf("The element on Front of Queue is : %d\n", front->data);
}
else
{
printf("The element on Front of Queue is : %d\n", temp->data);
}
}
//todo display function
void display()
{
temp =front;
if (flagforstart == 0)
{
printf("\nEmpty Queue\n");
}
else
{
printf("\nThe Queue is : \n");
while (temp != NULL)
{
if (front==NULL)
{
printf("%d\n", front->data);
}
else
{
printf("%d\n", temp->data);
}
temp = temp->next;
}
}
}
//todo main function
int main()
{
int option = 0;
do
{
printf("\n***--------Main Menu--------***");
printf("\nPress the following keys for following functions");
printf("\n1) Enqueue\n2) Dequeue\n3) Peek\n4) Display\n5) Exit\n");
scanf("%d", &option);
switch (option)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
//so that default is not displayed when the option 5 is selected
break;
default:
printf("\nInvalid Option key is pressed\n");
break;
}
} while (option != 5);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
//todo structure & pointers declaration
struct node {
int data;
struct node *next;
};
struct node *top, *newnode, *temp;
int flagforstart=0;
// todo function prototyping
void push();
void pop();
void peek();
void display();
//** here comes the functions
//todo push function
void push() {
if (flagforstart==0) {
//for adding the first element to the stack
top=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the Value you want to add to the stack: ");
scanf("%d", &newnode->data);
newnode->next=NULL;
top=temp=newnode;
flagforstart++; //this indicates we created at least on node
}
else {
//for adding the elements
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the Value you want to add to the stack: ");
scanf("%d", &newnode->data);
newnode->next=top;
top=newnode;
flagforstart++;
}
}
//todo pop function
void pop() {
if (flagforstart==0) {
printf("\nStack Underflow\n");
}
else {
temp=top;
top=temp->next;
free(temp);
flagforstart--;
}
}
//todo peek function
void peek() {
temp=top;
if (flagforstart==0) {
printf("\nEmpty stack\n");
}
else if (top==NULL) {
printf("The element on top of stack is : %d\n", top->data);
}
else {
printf("The element on top of stack is : %d\n", temp->data);
}
}
//todo display function
void display() {
temp=top;
if (flagforstart==0) {
printf("\nEmpty stack\n");
}
else {
printf("\nThe Stack is : \n");
while (temp!=NULL) {
if (top==NULL) {
printf("%d\n", top->data);
}
else {
printf("%d\n", temp->data);
}
temp=temp->next;
}
}
}
//todo main function
int main()
{
int option=0;
do {
printf("\n***--------Main Menu--------***");
printf("\nPress the following keys for following functions");
printf("\n1) Push\n2) Pop\n3) Peek\n4) Display\n5) Exit\n");
scanf("%d", &option);
switch (option) {
case 1: push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
case 5:
//so that default is not displayed when the option 5 is selected
break;
default: printf("\nInvalid Option key is pressed\n");
break;
}
} while (option!=5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment