Practical 1
Practical 1
Since 2001
Bhartiya Gramin Punarrachna Sanstha’s
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 Operations
The following are some common operations implemented on the
stack:
#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!!!
int topelement();
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;
}