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

Practical 1

Uploaded by

Nilesh Koli
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)
31 views9 pages

Practical 1

Uploaded by

Nilesh Koli
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

HIT/ CSE/LOM/ ___

Since 2001
Bhartiya Gramin Punarrachna Sanstha’s

Hi-Tech Institute of Technology, Aurangabad


A Pioneer to Shape Global Technologies
Approved By AICTE, DTE Govt. of Maharashtra & Affiliated to Dr. Babasaheb Ambedkar Technological University, Lonere, Raigad
P-119, Bajajnagar, MIDC Waluj, Aurangabad, Maharashtra, India - 431136P: (0240) 2552240, 2553495, 2553496

DEPARTMENT: COMPUTER SCIENCE & ENGINEERING DEPARTMENT


PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: Write a program to implement stack using arrays.
EXPERIMENT NO.: 01 SUBJECT: DATA STRUCTURES
CLASS:SY B.TECH CSE SEMESTER: II

Aim: Write a program to implement stack using arrays.

Hardware Requirement:

Intel(R) Core(™) i5-10505 CPU @ 3.20 GHz Processor, 8GB RAM, 256 GB HDD, 20” LCD Monitor, Keyb

Software Requirement:
TURBO C++

Theory:
● A Stack is a linear data structure that follows the LIFO
(Last-In-First-Out) principle.
● Stack has one end. It contains only one pointer, top,
pointing to the topmost element of the stack. Whenever an
element is added in the stack, it is added on the top of
the stack, and the element can be deleted only from the
stack.
● In other words, a stack can be defined as a container in
which insertion and deletion can be done from the one end
known as the top of the stack.
● It is called a stack because it behaves like a real-world
stack, piles of books, etc.
● A Stack is an abstract data type with a predefined
capacity, which means that it can store the elements of a
limited size.
● It is a data structure that follows some order to insert
and delete the elements, and that order can be LIFO or
FILO.

Working of Stack

● Stack works on the LIFO pattern. As we can observe in the


below figure there are five memory blocks in the stack;
therefore, the size of the stack is 5.
● Suppose we want to store the elements in a stack and
let's assume that stack is empty. We have taken the stack
of size 5 as shown below in which we are pushing the
elements one by one until the stack becomes full.

● Since our stack is full as the size of the stack is 5. In


the above cases, we can observe that it goes from the top
to the bottom when we were entering the new element in
the stack.
● The stack gets filled up from the bottom to the top. When
we perform the delete operation on the stack, there is
only one way for entry and exit as the other end is
closed.
● It follows the LIFO pattern, which means that the value
entered first will be removed last.
● In the above case, the value 5 is entered first, so it
will be removed only after the deletion of all the other
elements.

Stack Operations
The following are some common operations implemented on the
stack:

● push(): When we insert an element in a stack then the


operation is known as a push. If the stack is full then
the overflow condition occurs.

● pop(): When we delete an element from the stack, the


operation is known as a pop. If the stack is empty means
that no element exists in the stack, this state is known
as an underflow state.

● isEmpty(): It determines whether the stack is empty or


not.

● isFull(): It determines whether the stack is full or


not.'

● peek(): It returns the element at the given position.

● count(): It returns the total number of elements


available in a stack.

● change(): It changes the element at the given position.

● display(): It prints all the elements available in the


stack.

Sample Program Code:

#include<stdio.h>
#include<conio.h>
#define SIZE 10

void push(int);
void pop();
void display();
int stack[SIZE], top = -1;

void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}}}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not
possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not
possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}}

Output:
***** MENU *****
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 23
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 45
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 54
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 67
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Deleted : 67

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
54
45
23

Extra Programs For Practice:


Write a program to implement a stack using a linked list such
that the push and pop operations of stack still take O(1)time.
#include <stdio.h>
#include <stdlib.h>
struct node{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();

void push(int data);


void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;

void main(){
int no, ch, e;
printf("1 - Push");
printf("2 - Pop");
printf("3 - Top");
printf("4 - Empty");
printf("5 - Exit");
printf("6 - Display");
printf("7 - Stack Count");
printf("8 - Destroy stack");
create();
while (1){
printf("Enter choice : ");
scanf("%d", &ch);
switch (ch){
case 1:
printf("Enter element : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("stack is empty");
else{
e = topelement();
printf("Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" wrong choice:Try again ");
break;
}
}
}
//empty stack
void create(){
top = NULL;
}
void stack_count(){
printf("no: of elements in stack : %d", count);
}
//push data
void push(int data){
if (top == NULL){
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display(){
top1 = top;
if (top1 == NULL){
printf("empty stack");
return;
}
while (top1 != NULL){
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop(){
top1 = top;
if (top1 == NULL){
printf("error");
return;
}
else
top1 = top1->ptr;
printf("Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement(){
return(top->info);
}
//check stack empty or not
void empty(){
if (top == NULL)
printf("empty stack");
else
printf("stack not empty with %d values", count);
}
void destroy(){
top1 = top;
while (top1 != NULL){
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("all are destroyed");
count = 0;
}

Conclusion: Hence we have implemented stack using arrays.

You might also like