Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 Stack

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

Unit :3

What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Stack has one end, whereas the Queue has two ends (front and rear). It contains only one
pointer top pointer 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.

Some key points related to stack

 It is called as stack because it behaves like a real-world stack, piles of books, etc.
 A Stack is an abstract data type with a pre-defined 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.

Standard 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.

PUSH operation

The steps involved in the PUSH operation are given below:

 Before inserting an element in a stack, we check whether the stack is full.


 If we try to insert the element in a stack, and the stack is full, then the overflow condition
occurs.
 When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
 When the new element is pushed in a stack, first, the value of the top gets incremented, i.e.,
top=top+1, and the element will be placed at the new position of the top.
 The elements will be inserted until we reach the max size of the stack.
POP operation

The steps involved in the POP operation is given below:

 Before deleting the element from the stack, we check whether the stack is empty.
 If we try to delete the element from the empty stack, then the underflow condition occurs.
 If the stack is not empty, we first access the element which is pointed by the top
 Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
Array implementation of Stack
In array implementation, the stack is formed by using the array. All the operations
regarding the stack are performed using arrays. Lets see how each operation can be
implemented on the stack using array data structure.

Adding an element onto the stack (push operation)

Adding an element into the top of the stack is referred to as push operation. Push operation
involves following two steps.

1. Increment the variable Top so that it can now refer to the next memory location.
2. Add element at the position of incremented top. This is referred to as adding new element at
the top of the stack.

Stack is overflown when we try to insert an element into a completely filled stack therefore,
our main function must always avoid stack overflow condition.
Algorithm: PUSH(Insert) Operation in Stack

Step 1: If Top=Max-1

Print “Overflow : Stack is full” and Exit

End If

Step 2: Top=Top+1

Step 3: Stack[TOP]=Element

Step 4: End

Algorithm: POP(Delete) Operation in Stack

Step 1: If TOP=-1

Print “Underflow: Stack is empty” and Exit

End if

Step 2: Set Del_element=Stack[Top]

Step 3: Top=Top-1

Step 4: Del_Element

Step 5: End

Java program to implement Stack [PUSH and POP Operation]


// Stack implementation in Java

Class Stack{

// store elements of stack


Private int arr[];
// represent top of stack
Private int top;
// total capacity of the stack
Private int capacity;

// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}

// push elements to the top of stack


Public void push(int x){
if (isFull()) {
System.out.println("Stack OverFlow");

// terminates the program


System.exit(1);
}

// insert element on top of stack


System.out.println("Inserting " + x);
arr[++top] = x;
}

// pop elements from top of stack


Public int pop(){

// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}

// pop element from top of stack


Return arr[top--];
}

// return size of the stack


Public int getSize(){
return top + 1;
}

// check if the stack is empty


public Boolean isEmpty(){
return top == -1;
}

// check if the stack is full


public Boolean isFull(){
return top == capacity - 1;
}

// display elements of stack


publicvoidprintStack(){
for (inti = 0; i<= top; i++) {
System.out.print(arr[i] + ", ");
}
}

Public static void main(String[] args){


Stack stack = newStack(5);

stack.push(1);
stack.push(2);
stack.push(3);

System.out.print("Stack: ");
stack.printStack();

// remove element from stack


stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();

}
}

Output
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,

You might also like