Stack's and Queue's
Stack's and Queue's
Here below is the program for the Stack implementation using arrays. The PUSH, POP and DISPLAY operations
are as follows.
#include<stdio.h>
#include<process.h>
#define SIZE 10
void push();
void pop();
void display();
void main()
int choice;
while(1)
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d",&choice);
switch(choice)
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push( )
int value;
if(top == SIZE-1)
else
scanf("%d",&value);
top++;
stack[top] = value;
printf("\nInsertion success!!!");
void pop()
{
if(top == -1)
else
top--;
void display()
int i;
if(top == -1)
printf("\nStack is Empty!!!");
else
printf("%d\n",stack[i]);
Result -
ii ) Stack using Linked List ( Pointers )
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
struct node
int data;
};
void push();
void pop();
void display();
void main()
while(1)
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d",&choice);
switch(choice)
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push()
int value;
scanf("%d", &value);
ptr->data = value;
if(top == NULL)
ptr->next = NULL;
else
ptr->next = top;
top = ptr;
printf("\nInsertion is Success!!!\n");
void pop()
if(top == NULL)
else
temp=top;
free(temp);
void display()
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
temp=top;
while(temp->next != NULL)
printf("%d--->",temp->data);
printf("%d--->NULL",temp->data);
Result –
Program -2
A queue is linear data structure that consists of a collection is of items that follow a first-in-first-
out sequence. This implies that the first item to be inserted will be the first to be removed. You
can also say that items are removed in the order they were inserted.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define SIZE 10
void enQueue( );
void deQueue();
void display();
void main()
int choice;
while(1)
printf("1. Insertion\n");
printf("2. Deletion\n");
printf("3. Display\n");
printf("4. Exit\n");
switch(choice)
case 1: enQueue( );
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
void enQueue()
int value;
if(rear == SIZE-1)
else
if(front == -1)
front = 0;
else
scanf("%d",&value);
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
void deQueue()
else
front++;
void display()
printf("\nQueue is Empty!!!");
else
int i;
printf("%d\t",queue[i]);
}
}
Result –
ii ) Queue using Linked list ( or ) Pointers
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL,*rear = NULL;
void enQueue();
void deQueue();
void display();
void main()
{
int choice;
while(1)
{
printf("\n\n***** QUEUE USING LINKED LIST MENU *****\n");
printf("1. enQueue\n");
printf("2. deQueue\n"); printf("3. Display\n");
printf("4. Exit\n"); printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
ptr -> next = NULL;
if(front == NULL)
{
front=ptr;
rear=ptr;
}
else
{
rear = ptr;
printf("\nInsertion is Success!!!\n");
void deQueue()
if(front == NULL)
temp=front;
free(temp);
void display()
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
temp=front;
while(temp->next != NULL)
printf("%d--->",temp->data);
printf("%d--->NULL",temp->data);
Results –