EXPT01_14_B1[1]

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DEPARTMENT OF ELECTRONICS & COMPUTER SCIENCE

LAB MANUAL

DATA STRUCTURES (ESL303)


Experiment No. 1

Aim: Implementation of stack using arrays.

Theory:
A stack is an ordered list in which all insertions and deletions are made at one end, called the top.
Just like a plate stack in canteens where plates are added to the top and later removed from the
top.

The restrictions on a stack imply that if the elements A,B,C,D,E are added to the stack, n that
order, then the first element to be removed/deleted must be E. Equivalently we say that the last
element to be inserted into the stack will be the first to be removed. For this reason stacks are
sometimes referred to as Last In First Out (LIFO) lists.

The main function that has to be included in stack are:

1. isfull(): This is to check weather the stack is full or not. Returns true if stack is full.

2. isempty(): This is to check weather the stack is empty or not. Returns true if stack is empty.

3. push(): It is used to enter element at the top of the stack.

4. pop(): It is used to remove first element from the top of stack.

5. stacktop(): This function is used to return the value at the stack top.

After implementation of the program, apply the following test cases, set the size of the stack to 5.
-
1. Insert 6 elements and show five elements (1,2,3,4,5,6) are inserted successfully and for
6th element, you get an overflow message.
2. Pop 6 times and check that elements are displayed in the order 5,4,3,2,1, underflow.
Conclusion: Stack is a linear data structure thus it can be easily implemented using arrays. All
the operations- push(), pop() and test cases- overflow and underflow were tested successfully.

Program :

#include <stdio.h>
#include <stdlib.h> // Needed for exit function

#define MAX 5 // Maximum size of the stack

int stack[MAX], top = -1, number, i;

// Function prototypes
void push(int stack[]);
void pop();
void peek();
void display();

void main() {
int choice;

// Main
menu
while(1) {
printf("\nMenu: \n1.Push \n2.Pop \n3.Peek \n4.Display \n5.Exit"); printf("\
nEnter your choice: \n");
scanf("%d", &choice);

switch(choice) {
case 1: push(stack);
break;
case 2: pop();
break;
case 3: peek();
break;
case 4:
display();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice");
}
}
}

// Push function to add an element to the


stack void push(int stack[]) {
if(top == MAX-1) {
printf("Stack Overflow");
} else {
printf("\nEnter Number:
"); scanf("%d", &number);
stack[++top] = number;
}
}

// Pop function to remove the top element from the


stack void pop() {
if(top == -1) {
printf("Stack Underflow");
} else {
number = stack[top];
printf("%d popped",
number); top--;
}
}

// Peek function to view the top element of the


stack void peek() {
if(top == -1) {
printf("Stack is empty");
} else {
printf("\nThe Top element is at index %d and is %d", top, stack[top]);
}
}

// Display function to show all elements of the


stack void display() {
printf("\nStack: ");
for(i = 0; i <= top; i++) {
printf("%d ",
stack[i]);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy