0% found this document useful (0 votes)
6 views6 pages

Exp 1 Final STK Adt Wup

The document outlines an experiment to implement a Stack Abstract Data Type (ADT) using an array. It describes the stack's LIFO nature, various operations (push, pop, peek, isFull, isEmpty), and provides algorithms for each operation along with a C program implementation. The conclusion emphasizes the learning outcomes regarding Stack ADT and its array implementation.

Uploaded by

waghjayesh07
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)
6 views6 pages

Exp 1 Final STK Adt Wup

The document outlines an experiment to implement a Stack Abstract Data Type (ADT) using an array. It describes the stack's LIFO nature, various operations (push, pop, peek, isFull, isEmpty), and provides algorithms for each operation along with a C program implementation. The conclusion emphasizes the learning outcomes regarding Stack ADT and its array implementation.

Uploaded by

waghjayesh07
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/ 6

EXPERIMENT NO:-01

Date of Performance:

Date of Submission:

Aim :To implement stack ADT using array.

Theory:

A Stack is one of the most common Data Structure. We can implement stack using an Array or
Linked list. Stack has only one End referred as TOP. So the element can only be inserted and
removed from TOP only. Hence Stack is also known as LIFO (Last In First Out).

The following diagram depicts a stack and its operations −

The various Operations/Functions of Stack are:

Operation Purpose
push() Pushing (storing) an element on the stack.
pop() Removing (accessing) an element from the stack.
peek() get the top data element of the stack, without removing it.
isFull() check if stack is full.
isEmpty() check if stack is empty.

Algorithm 1: To check Stack is full or not

 Step 1 − Checks if the stack is full using condition (top == size-1)


 Step 2 − If the stack is full(condition in step 1 is true), return true.
 Step 3 − If the stack is not full, return false.

Algorithm 2: To check Stack is empty or not

 Step 1 − Checks if the stack is empty using condition (top == -1)


 Step 2 − If the stack is empty (condition in step 1 is true), return true.
 Step 3 − If the stack is not empty, return false.

Algorithm 3: To push element on the stack

 Step 1 − Checks if the stack is full using algorithm 1.


 Step 2 − If the stack is full, produces an error and exit.
 Step 3 − If the stack is not full, increments top to point next empty space.
 Step 4 − Add data element to the stack location, where top is pointing.
 Step 5 − Returns success.

(top=3) (top=4)

Algorithm 4: To pop element from the stack

 Step 1 − Checks if the stack is empty.


 Step 2 − If the stack is empty, produces an error and exit.
 Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
 Step 4 − Decreases the value of top by 1.
 Step 5 − Returns success.

(top=4) (top=3)
Program:

#include<stdio.h>
int size = 5;
int top = -1;
int val;
int choice;
int STK[5];
int i;
int j;
int k;

int isstkfull(){
if(top == (size - 1))
return 1;
else
return 0;
}

int isstkempty(){
if(top == -1)
return 1;
else
return 0;
}

void push(int val){


if(isstkfull())
printf("Stack is full cannot push\n");
else{
top ++;
STK[top] = val;
}
}

int pop()
{
if(isstkempty())
printf("Stack is Empty cannot pop\n");
else{
val = STK[top];
top--;
return val;
}
}

int stktop(){
if(isstkempty())
printf("Stack is Empty\n");
else{
val = STK[top];
return val;
}
}

void display(){
if(isstkempty())
printf("Stack is empty\n");
else{
for(i=top; i>=0; i--){
val = STK[i];
printf("%d\n",val);
}
}
}

int main()
{
do{
printf("1.PUSH\n2.POP\n3.StackTop\n4.StackisFull\n5.StackisEmpty\n6.Display\n7.Exit\
n");
printf("Enter your choice\n");
scanf("%d",&choice);

switch(choice){
case 1:
printf("Enter the value to be pushed in the stack\n");
scanf("%d",&val);
push(val);
break;
case 2:
val = pop();
printf("Pop val= %d\n",val);
break;
case 3:
val = stktop();
printf("Top of stack value is = %d\n",val);
break;
case 4:
j = isstkfull();
if(j == 1)
printf("Stack is Full\n");
else
printf("Stack is not Full\n");
break;
case 5:
k = isstkempty();
if( k == 1)
printf("Stack is Empty\n");
else
printf("Stack is not Empty\n");
break;
case 6:
display();
break;
case 7:
printf("Exit the Program\n");
break;
}
}while(choice!=7);
}

Input & Output:


Conclusion: Thus, we learned about Stack ADT and how to implement
Stack ADT using array.

Sign and Remark:

R1 R2 R3 R4 Total Marks Signature


(3) (5) (4) (3) (15)

You might also like