0% found this document useful (0 votes)
2 views8 pages

C Program for Stack ADT Implementation

Uploaded by

Daivik Chaulkar
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)
2 views8 pages

C Program for Stack ADT Implementation

Uploaded by

Daivik Chaulkar
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

Name: Harsh Shukla

Roll no: 21
Class: SE AI & DS

Aim-: C program to study and implement Stack ADT using Struct.


Program:
#include <stdio.h>
#include <stdlib.h>
#define size 100
int i,k,j;
struct stack1
{
int s[size];
int top;
}STACK;

void push();
int pop();
void display();

int peek(int k) {
if (k>[Link]+1 || k<0) {
printf("Please Enter a valid position\n");
}
else {
j = STACK.s[[Link]-k+1];
printf("The value at %d is:\n%d\n",k,j);
return j;
}
}

void StackTop() {
if ([Link] == -1) {
printf("Stack is Empty!!\n");
}
else {
printf("Top element is:\n%d\n",STACK.s[[Link]]);
}
}

void StackBottom() {
if ([Link] == -1) {
printf("Stack is Empty!!\n");
}
else {
printf("Bottom element is:\n%d\n",STACK.s[0]);
}
}

void isEmpty() {
if ([Link] == -1) {
printf("The Stack is Empty!!\n");
}
else {
printf("The Stack is not Empty!!\n");
}
}

void isFull() {
if ([Link] == size-1) {
printf("The Stack is Full!!\n");
}
else {
printf("The Stack is not Full!!\n");
}
}

void main()
{
[Link] = -1;
int ch,ch1,ch2;
do {
printf("Enter your choice ->\n 1->Insert element\n 2-
>Delete element\n 3->Display\n 4->peek\n 5->Top/Bottom
element\n 6->Is Stack Empty or Full\n 7->exit\n");
scanf("%d",&ch);

switch (ch)
{
case 1:
push();
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
printf("Enter the posiotion you want to see:\n");
scanf("%d",&k);
peek(k);
break;

case 5:
printf("Which value you want to see?\n1-->Top\n2--
>Bottom\n");
scanf("%d",&ch1);
switch (ch1) {
case 1:
StackTop();
break;

case 2:
StackBottom();
break;

default:
printf("please enter a valid number.\n");
}
break;

case 6:
printf("Please choose what you want to check:-\n1--
>Is Stack Empty\n2-->Is Stack Full\n");
scanf("%d",&ch2);
switch (ch2) {
case 1:
isEmpty();
break;

case 2:
isFull();
break;

default:
printf("please enter a valid number.\n");
}
break;

case 7:
exit(0);
break;
default:
printf("Please Enter a valid choice!!\n");
}
}
while (ch!=7);
}

void push() {
int h;
printf("Enter the no. of elements to insert:\n");
scanf("%d",&h);
for (i=1; i<=h; i++) {
if ([Link] == size - 1) {
printf("Stack is full!!...It may overflow!!\n");
}
else {
[Link]++;
printf("Enter the value to push:\n");
scanf("%d",&STACK.s[[Link]]);
printf("Element pushed successfully!!\n");
}
}
}

int pop() {
if ([Link] == -1) {
printf("The Stack is empty!!...It may cause
underflow!!\n");
}
else {
int n = STACK.s[[Link]];
[Link]--;
printf("%d deleted successfully!!\n",n);
return n;
}
}

void display() {
if ([Link] == -1) {
printf("Stack is Empty!!\n");
}
else {
printf("Top = %d -->",[Link]);
printf("%d\n",STACK.s[[Link]]);
for (i = ([Link])-1; i>=0; i--) {
printf("\t %d\n",STACK.s[i]);
}
}
}

Output:
Enter your choice ->
1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

1
Enter the no. of elements to insert:
5
Enter the value to push:
10
Element pushed successfully!!
Enter the value to push:
20
Element pushed successfully!!
Enter the value to push:
30
Element pushed successfully!!
Enter the value to push:
40
Element pushed successfully!!
Enter the value to push:
50
Element pushed successfully!!

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

3
Top = 4 -->50
40
30
20
10

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit
4

Enter the posiotion you want to see:


3
The value at 3 is:
30

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

2
50 deleted successfully!!

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

5
Which value you want to see?
1-->Top
2-->Bottom
1
Top element is:
40
Enter your choice ->
1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

5
Which value you want to see?
1-->Top
2-->Bottom
2
Bottom element is:
10

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit
6

Please choose what you want to check:-


1-->Is Stack Empty
2-->Is Stack Full
1
The Stack is not Empty!!

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit
6
Please choose what you want to check:-
1-->Is Stack Empty
2-->Is Stack Full
2
The Stack is not Full!!

Enter your choice ->


1->Insert element
2->Delete element
3->Display
4->peek
5->Top/Bottom element
6->Is Stack Empty or Full
7->exit

You might also like