You are on page 1of 12

WEEK:4

IMPLEMENTATION OF STACK AND QUEUE


NAME: G.SHIVANI
ROLLNO: 16951A05H5
YEAR: I SEMESTER:11
COURSE NAME: DATA STRUCTURES COURSE CODE:ACS102
PROGRAM NAME: STACK AND ITS OPERATIONS USING ARRAY
Algorithm:
1.start
2.Stack is a linear data structure which works under the principle of last in first out.
Operations: push(adding element) and pop(removing element)
3.push:
if(top==MAX),display stack overflow. Otherwise reading the data and making a [top]=data and
incrementing the top value by top++.
4.pop:
If(top==0),display stack underflow. Otherwise printing the element at the top of the stack and
decrementingthe top value by doing the top.
5.Display:
if(top==0),display stack is empty. Otherwise printing the elements in the stack from a[0] to
a[top].
6.stop.

Source Code:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 6

int stack[MAX];
int top=0;

int menu(){

int ch;

printf("\npress 1 for push.");

printf("\npress 2 for pop.");

printf("\npress 3 for display.");

printf("\npress 4 to quit.");

printf("enter correct option:");

scanf("%d",&ch);

return ch;

void display()

int i;

if(top==0){

printf("\nstack empty.");

return;

}else{

printf("\nelements in stack.");

for(i=0;i<top;i++){

printf("\t%d",stack[i]);

void pop()
{

if(top==0){

printf("\nstack underflow.");

return;

else

printf("\npoped element is:%d",stack[--top]);

void push()

int data;

if(top==MAX){

printf("\nstack is overflow.");

return;

}else

printf("\nenter data:");

scanf("%d",&data);

stack[top]=data;

top=top+1;

printf("\ndata pushed into the stack.");

void main()

int ch;

do
{

ch=menu();

switch(ch){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

getch();

while(1);

Input/output:
press 1 for push

press 2 for pop

press 3 for display

press 4 to quit
enter correct option:1

enter data:2

data pushed into the stack.

press 1 for push

press 2 for pop

press 3 for display

press 4 to quit

enter correct option:1

enter data:5

data pushed into the stack.

press 1 for push

press 2 for pop

press 3 for display

press 4 to quit

enter correct option:1

enter data:17

data pushed into the stack.

press 1 for push

press 2 for pop

press 3 for display

press 4 to quit

enter correct option:3

elements in stack: 2 5 17

press 1 for push

press 2 for pop


press 3 for display

press 4 to quit

enter correct option:2

popped element is 17

press 1 for push

press 2 for pop

press 3 for display

press 4 to quit

enter correct option:3

elements in stack:2 5

press 1 for push

press 2 for pop

press 3 for display

press 4 to quit

enter correct option:4


NAME: G.SHIVANI
ROLLNO: 16951A05H5
YEAR: I SEMESTER:11
COURSE NAME: DATA STRUCTURES COURSE CODE:ACS102
PROGRAM NAME: QUEUE AND ITS OPERATIONS USING ARRAY
Algorithm:
1.start
2.QUEUE is a linear data structure which works under the principle of first in first out.
Operations: insertion deletion display.
3.Insertion:
If(rear==MAX),display queue is full else reading data and inserting at a a[rear] and doing rear+
+.
4. deletion:
if(front==rear),display is empty. Else printing element at a[front] and doing front++.
5.Display:
if(front== rear),display queue is empty.else printing the elements from a[front] to a[rear].
6.stop.
source code:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 10

int a[MAX];

int front,rear;

void insert()

int data;

if(rear==MAX){
printf("\nQueue is full.");

return;

else{

printf("\n enter data:");

scanf("%d",&data);

a[rear]=data;

rear++;

printf("\ndata inserted into the Queue.");

void delete()

if(rear==front){

printf("\nqueue is empty.");

else

printf("\ndeleted element from Queue is %d",a[front++]);

void display(){

int i;
if(front==rear){

printf("Queue is empty.");

else

printf("elements in Queue:");

for(i=front;i<rear;i++){

printf("\t%d",a[i]);

int menu(){

int ch;

printf("\npress 1 for inserting.");

printf("\npress 2 for deleting.");

printf("\npress 3 to display.");

printf("\npress 4 to quit.");

printf("\nenter correct option:");

scanf("%d",&ch);

return ch;

void main()
{

int ch;

do

ch=menu();

switch(ch)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

return;

while(1);

}
Input/output:
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:1


Enter data:10
Inserted into the queue
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:1


Enter data:20
Inserted into the queue
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:1


Enter data:30
Inserted into the queue
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:3


The elements in the queue are 30 20 10
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:2


The deleted element from the queue is:10
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:3


The elements in the queue is:30 20
1 for inserting
2 for deleting

3 to display

4 to quit

Enter your choice:2


Enter data:10
Queue is full.

You might also like