Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Expt 2 Stack using Array

The document outlines an experiment on implementing a stack data structure using an array at Rizvi College of Engineering, Mumbai. It details the theory, algorithm, and program code for stack operations such as push, pop, and display, along with a menu-driven interface for user interaction. The evaluation rubric for the experiment includes criteria for correctness, clarity of documentation, and delivery.

Uploaded by

mbpatil5004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Expt 2 Stack using Array

The document outlines an experiment on implementing a stack data structure using an array at Rizvi College of Engineering, Mumbai. It details the theory, algorithm, and program code for stack operations such as push, pop, and display, along with a menu-driven interface for user interaction. The evaluation rubric for the experiment includes criteria for correctness, clarity of documentation, and delivery.

Uploaded by

mbpatil5004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Rizvi College of Engineering, Mumbai

_____________________________________________________________________________________

Rizvi
RIZVI EDUCATION SOCIETY’S

College of Engineering
Approved by AICTE Recognized by DTE Affiliated to University of Mumbai Accredited B+ by NAAC

New Rizvi Educational Complex, Off Carter Rd, Bandra West, Mumbai-400050

Experiment No. 2

Title Array Implementation of Stack.

Name SUYASH PAWAR


Year/Branch F.E/AIDS
Roll No. 17

UIN 241A017

Evaluation Rubric Table

Criteria Points
Correctness of the program

Clarity of documentation

Delivery

Total
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________

Title: Array Implementation of Stack.

Aim: To Implement Stack ADT using Array

Theory: A stack data structure can be implemented using a one-dimensional array. But a stack
implemented using arrays stores only a fixed number of data values. This implementation is very simple.
Just define a one dimensional array of specific size and insert or delete the values into that array by using
LIFO principle with the help of a variable called 'top'. Initially, the top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one and then insert. Whenever we want to delete a
value from the stack, then delete the top value and decrement the top value by one.

Algorithm:

Stack Operations using Array


A stack can be implemented using array as follows

Creation of Stack

● Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
● Step 2 - Declare all the functions used in stack implementation.
● Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
● Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
● Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________

push (int value) - Inserting value into the stack


In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is always
inserted at top position. Push function takes one integer value as parameter and inserts that value into the
stack. We can use the following steps to push an element onto the stack.

● Step 1 - Check whether stack is FULL. (if top = = SIZE-1)


● Step 2 - If it is FULL, then display "Stack is FULL. Insertion is not possible" and terminate the
function.
● Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).

pop() - Delete a value from the Stack


In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is always deleted from top
position. Pop function does not take any value as parameter. We can use the following steps to pop an element from the
stack...

● Step 1 - Check whether stack is EMPTY. (if top = = -1)


● Step 2 - If it is EMPTY, then display "Stack is EMPTY. Deletion is not possible" and terminate the function.
● Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).

display() - Displays the elements of a Stack


We can use the following steps to display the elements of a stack.

● Step 1 - Check whether stack is EMPTY. (if top = = -1)


● Step 2 - If it is EMPTY, then display "Stack is EMPTY" and terminate the function.
● Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value
and decrement i value by one (i--).
● Step 3 - Repeat above step until i value becomes '0'.

Program:

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

int stack[SIZE];
int top = -1;
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________

void push(int); void


pop();
void display();

void main()
{
int choice,value;
while(1)
{
printf("\n\t\tMenu\t\t\n");
printf("Select from the given options\n");
printf("Option 1: Insert an element\nOption 2: Delete an element\nOption 3: Display array.\nOption
4: Exit the program.\n");
scanf("%d",&choice);

switch(choice)
{ case 1: printf("Enter
value to insert: ");
scanf("%d",&value);
push(value);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4: printf("\nExiting
the program"); exit(0);

default:
printf("\nInvalid option!!!Choose from the given options.\n");
break;
}
}
}
void push(int value)
{
if (top == SIZE-1)
{
printf("\nStack is FULL.Insertion is not possible\n");
}
else
{
top = top+1;
stack[top]=value;
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________

void pop()
{ if(top ==
-1)
{
printf("\nStack is EMPTY\n");
}
else
{
printf("\nDeleted :%d",stack[top]);
top = top-1;
}
}

void display()
{
int i;
if(top == -1)
{
printf("\nStack is EMPTY\n");
}
else
{
printf("\nArray elements\n");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
}

Output:
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________

Conclusion:

COs Covered:

You might also like