Skip to content

Added Stack and Queue Algorithm #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions StackandQueue/queueusingarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define size 5

int queue[size];
int front = -1;
int rear = -1;

bool isEmpty()
{
if(front == -1 && rear == -1)
return true;
return false;
}

bool isFull()
{
if(rear == size-1)
return true;
return false;
}

void enQueue()
{
int val;
if(isFull())
printf("Queue is FULL !!");
else if(isEmpty())
{
printf("\nEnter value : ");
scanf("%d",&val);
front = rear = 0;
queue[rear]=val;
}
else
{
printf("\nEnter value : ");
scanf("%d",&val);
rear++;
queue[rear]=val;
}
printf("\n");
}

void deQueue()
{
if(isEmpty())
printf("\nQueue is EMPTY!!\n");
else if(front == rear)
front=rear=-1;
else
front ++;
}

void print()
{
if(isEmpty())
printf("\nQueue is EMPTY!!\n");
else
{
for(int i=front;i<=rear;i++)
printf("%d ",queue[i]);
}
printf("\n");
}
int main()
{
int choice;
do
{
printf("ENTER CHOICE :\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while (choice!=4);
return 0;
}
95 changes: 95 additions & 0 deletions StackandQueue/queueusinglist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
int data;
struct node *next;
}node;

node *head = NULL;

node *createNode()
{
node *temp;
temp = (node*)malloc(sizeof(node));
return temp;
}

void enQueue()
{
node *temp = createNode();
printf ("Enter Your Data : ");
scanf ("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
node *t=head;
while (t->next!=NULL)
t=t->next;
t->next=temp;
}
printf("Data successfully Entered \n\n");

}

void deQueue()
{
node *t=head;
node *del;
if(head==NULL)
printf("\nQueue is empty, UNDERFLOW!!\n");
else if(head->next==NULL)
{
del=head;
head=NULL;
printf("\n%d Popped from Queue\n\n",del->data);
}
else
{
del=head;
head=head->next;
printf("\n%d Popped from Queue\n\n",del->data);
}
free(del);
}

void print()
{
if(head==NULL)
{
printf("Queue is EMPTY!!\n");
return;
}
node *t = head;
printf ("Data present in the Queue : \n");
while(t!=NULL)
{
printf ("%d \n",t->data);
t=t->next;
}
}

int main()
{
int choice;
do
{
printf("ENTER CHOICE :\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while (choice!=4);
return 0;
}
10 changes: 10 additions & 0 deletions StackandQueue/reame.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Added Stack and Queue implementation
Added the following programs :
1. stackusingarray.c
2. stackusinglist.c
3. queueusingarray.c
4. queueusinglist.c

The following prorams are the implementation of **Stack** and **Queue** algorithms using array and linked list.

**Cheers!**
80 changes: 80 additions & 0 deletions StackandQueue/stackusingarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define MAX 5
#define MIN -1

int stack[MAX];
int top = -1;

bool notOverflow()
{
if (top+1<MAX)
return true;
return false;
}

bool notUnderflow()
{
if(top!=MIN)
return true;
return false;
}

void push()
{
int val;
if (notOverflow())
{
printf("Enter Your Data : ");
scanf("%d",&val);
stack[++top]=val;
}
else
{
printf("\nSTACK OVERFLOW!!!\n\n");
}
}

void pop()
{
if (notUnderflow())
{
printf("Popped Value : %d\n",stack[top--]);
}
else
{
printf("\nSTACK UNDERFLOW!!!\n\n");
}
}

void print()
{
int i;
printf("\nData present in the stack : ");
for(i=0;i<=top;i++)
printf("%d ",stack[i]);
printf("\n\n");
}

int main()
{
int choice;
do
{
printf("ENTER CHOICE :\n1.Push\n2.Pop\n3.Display\n4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while (choice!=4);
return 0;
}
90 changes: 90 additions & 0 deletions StackandQueue/stackusinglist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<stdio.h>
#include<stdlib.h>

typedef struct Stack
{
int data;
struct Stack *next;
}node;

node *stack = NULL;

node *createNode()
{
node *temp;
temp = (node*)malloc(sizeof(node));
return temp;
}

void push()
{
node *temp = createNode();
printf ("Enter Your Data : ");
scanf ("%d",&temp->data);
temp->next=NULL;
temp->next=stack;
stack=temp;
printf("\n\n%d Pushed into stack\n\n",temp->data);
}

void pop()
{
node *t=stack;
node *del;
if(stack==NULL)
printf("\nStack is empty, UNDERFLOW!!\n");
else if(stack->next==NULL)
{
del=stack;
stack=NULL;
printf("\n\n%d Popped into stack\n\n",del->data);
}
else
{
del=stack;
stack=stack->next;
printf("\n\n%d Popped into stack\n\n",del->data);
}
free(del);
}

void print()
{
if(stack==NULL)
{
printf("Stack is empty!!");
return 0;
}
node *t = stack;
int count = 0;
printf ("\nData present in the stack\n");
while(t!=NULL)
{
count++;
printf ("%d \n",t->data);
t=t->next;
}
printf("\nNumber of elements in the stack : %d\n\n",count);
}

int main()
{
int choice;
do
{
printf("ENTER CHOICE :\n1.Push\n2.Pop\n3.Display\n4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while (choice!=4);
return 0;
}