0% found this document useful (0 votes)
13 views9 pages

Baba Stacks DS 05-03-2022

The document presents a C program that demonstrates stack operations, including PUSH and POP, using both a simple array implementation and a structure-based approach. It includes code snippets for creating a stack, pushing elements onto it, popping elements off, and displaying the stack contents. The output of the program is also provided, showing the results of various stack operations.

Uploaded by

bababasham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Baba Stacks DS 05-03-2022

The document presents a C program that demonstrates stack operations, including PUSH and POP, using both a simple array implementation and a structure-based approach. It includes code snippets for creating a stack, pushing elements onto it, popping elements off, and displaying the stack contents. The output of the program is also provided, showing the results of various stack operations.

Uploaded by

bababasham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ROLL NO : 21701F0013

NAME OF THE STUDENT :M.BABA VALI

COURSE : M.C.A

DATE: 05-03-2022

PROGRAM : STACKS

STACKS
Write a c program that illustrates stack operations PUSH and POP

#include<stdio.h>

//#include<process.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5//maximum number of elements that can be stored

int top=-1,stack[MAX];

void push();

void pop();

void display();

int main()

{
int ch;

while(1)//infinite loop,will end when choice will be 4

printf("\n***stack menu ***");

printf("\n\n1.push\n2.pop\n3.Display\n4.exit");

printf("\n\nenter your choice(1-4):");

scanf("%d",&ch);

switch(ch)

case 1: push();

break;

case2:pop();

break;

case3:display();

break;

case4:exit(0);

default:printf("\nwrong choice!!");

void push()

int val;

if(top==MAX-1)

{
printf("\nstack is full!!");

else

printf("\nenter elements to push:");

scanf("%d",&val);

top=top+1;

stack[top]=val;

void pop()

if(top==-1)

printf("\nstack is empty!!");

else

printf("\ndeleted element is %d",stack[top]);

top=top-1;

void display()

int i;
if(top==-1)

printf("\nstack is empty!!");

else

printf("\nstack is...\n");

for(i=top;i>=0;--i)

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

OUTPUT:

***stack menu ***

1.push

2.pop

3.Display

4.exit

enter your choice(1-4):1


enter elements to push:22

***stack menu ***

1.push

2.pop

3.Display

4.exit

enter your choice(1-4):2

wrong choice!!

***stack menu ***

1.push

2.pop

3.Display

4.exit

enter your choice(1-4):3

wrong choice!!

***stack menu ***


1.push

2.pop

3.Display

4.exit

enter your choice(1-4):4

wrong choice!!

***stack menu ***

1.push

2.pop

3.Display

4.exit

2. C program for array implementation of stack


#include<limits.h>

#include<stdio.h>

#include<stdlib.h>

// a structure to represent a stack

struct stack{

int top;

unsigned capacity;
int* array;

};

//function to create a stack of given cpacity.it initilizes size of

// stack as 0

struct stack* createstack(unsigned capacity)

struct stack*stack=(struct stack*)malloc(sizeof(struct stack));

stack->capacity=capacity;

stack->top=-1;

stack->array=(int*)malloc(stack->capacity*sizeof(int));

return stack;

//stack is full when top is equal to the last index

int isfull(struct stack* stack)

return stack ->top==stack->capacity-1;

//stack is empty when top is equal to -1

int isempty(struct stack*stack)

return stack->top==-1;
}

//function to add an item to stack.it increases top by 1

void push(struct stack*stack,int item)

if(isfull(stack))

return;

stack->array[++stack->top]=item;

printf("%d pushed to stack\n",item);

//function to remove an item from stack.it decreases top by 1

int pop(struct stack* stack)

if(isempty(stack))

return INT_MIN;

return stack->array[stack->top--];

//functions to return the top from stack without removing it

int peek (struct stack*stack)

if(isempty(stack))

return INT_MIN;

return stack->array[stack->top];

}
//driver program to test above functions

int main()

struct stack* stack=createstack(100);

push(stack, 10);

push(stack, 20);

push(stack, 30);

printf("%d popped from stack\n",pop(stack));

return 0;

Output:

10 pushed to stack

20 pushed to stack

30 pushed to stack

30 popped from stack

--------------------------------

You might also like