From 3a29d38104bbb78932188aa9ac71c176e5ea1de0 Mon Sep 17 00:00:00 2001 From: Aniket Nayek Date: Wed, 6 Oct 2021 15:34:36 +0530 Subject: [PATCH] Added Stack and Queue implementation --- StackandQueue/queueusingarray.c | 87 ++++++++++++++++++++++++++++++ StackandQueue/queueusinglist.c | 95 +++++++++++++++++++++++++++++++++ StackandQueue/reame.md | 10 ++++ StackandQueue/stackusingarray.c | 80 +++++++++++++++++++++++++++ StackandQueue/stackusinglist.c | 90 +++++++++++++++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 StackandQueue/queueusingarray.c create mode 100644 StackandQueue/queueusinglist.c create mode 100644 StackandQueue/reame.md create mode 100644 StackandQueue/stackusingarray.c create mode 100644 StackandQueue/stackusinglist.c diff --git a/StackandQueue/queueusingarray.c b/StackandQueue/queueusingarray.c new file mode 100644 index 0000000..5632f8f --- /dev/null +++ b/StackandQueue/queueusingarray.c @@ -0,0 +1,87 @@ +#include +#include +#include +#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; +} \ No newline at end of file diff --git a/StackandQueue/queueusinglist.c b/StackandQueue/queueusinglist.c new file mode 100644 index 0000000..fd76f78 --- /dev/null +++ b/StackandQueue/queueusinglist.c @@ -0,0 +1,95 @@ +#include +#include + +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; +} \ No newline at end of file diff --git a/StackandQueue/reame.md b/StackandQueue/reame.md new file mode 100644 index 0000000..e4478ea --- /dev/null +++ b/StackandQueue/reame.md @@ -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!** \ No newline at end of file diff --git a/StackandQueue/stackusingarray.c b/StackandQueue/stackusingarray.c new file mode 100644 index 0000000..90dc72f --- /dev/null +++ b/StackandQueue/stackusingarray.c @@ -0,0 +1,80 @@ +#include +#include +#include +#define MAX 5 +#define MIN -1 + +int stack[MAX]; +int top = -1; + +bool notOverflow() +{ + if (top+1 +#include + +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; +}