CSE 2001 Lab Session 2 - Stack Operations
CSE 2001 Lab Session 2 - Stack Operations
Date
Course Code CSE 2001
Course Name Data Structures and Algorithms
Semester III
Course Instructor Name of the instructor
Learning LO1: Identify the key operations of a stack, including push, pop, peek, and
Outcomes checking the stack's status.
LO2: Implement a stack using arrays and execute fundamental stack
operations effectively in a program.
Definition: A stack is an ordered list in which insertion and deletion are done at one end,
called
top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First
Out (LIFO) or First in Last Out (FILO) list.
ADT
The following operations make a stack an ADT. For simplicity, assume the data is an integer
type.
Applications
1. Balancing of symbols
2. Infix-to-postfix conversion
3. Evaluation of postfix expression
4. Implementing function calls (including recursion)
5. Finding of spans (finding spans in stock markets, refer to Problems section)
6. Page-visited history in a Web browser [Back Buttons]
7. Undo sequence in a text editor
8. Matching Tags in HTML and XML
package myStack;
/**
* @author pavan
*
*/
public class Stack {
int size;
int stack[];
int top;
public Stack(int size) {
this.size = size;
this.stack = new int[size];
this.top = -1;
}
public int Top()
{
return stack[top];
}
public int Size()
{
return top+1;
}
public boolean IsEmptyStack()
{
return top==-1?true:false;
}
public boolean IsFullStack()
{
return top>=size-1?true:false;
}
public void push(int element)
{
if(IsFullStack())
{
System.out.println("Stack is OverFlow");
}
else
{
top++;
stack[top]=element;
}
}
public int pop()
{
int deletedElemnt;
if(IsEmptyStack())
{
System.out.println("Under Flow");
return -1;
}
else
{
deletedElemnt=stack[top];
top--;
return deletedElemnt;
}
}
public void display()
{
if(IsEmptyStack())
{
System.out.println("No ELment to display");
}
else
{
for(int i=0;i<=top;i++)
{
System.out.print("| "+stack[i]);
}
}
}
package myStack;
import java.util.Scanner;
/**
* @author pavan
*
*/
public class TestStack {