






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the Stack data structure, its implementation using arrays and linked lists, fundamental operations such as push, pop, top, size, and capacity, and various applications including symbol balancing, infix to postfix conversion, and redo-undo features. It also covers the time complexity and efficiency of stacks.
Typology: Essays (high school)
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Stack ADT Basic operations of stack Pushing, popping etc. Implementations of stacks using array linked list
Push: Equivalent to an insert Pop: Deletes the most recently inserted element Top: Examines the most recently inserted element Size: Number of elements a stack contains at present Capacity: Number of elements it is capable of holding
Stacks are less flexible but are more efficient and easy to implement Stacks are known as LIFO (Last In, First Out) lists or or FILO(First In Last Out)The last element inserted will be the first to be retrieved.
Need to declare an array size ahead of time Associated with each stack is TopOfStack for an empty stack, set TopOfStack to - Push (1) Increment TopOfStack by 1. (2) Set Stack[TopOfStack] = X Pop (1) Set return value to Stack[TopOfStack] (2) Decrement TopOfStack by 1 These operations are performed in very fast constant time
class Stack
{
private int[] ele; // name of the array for stack
private int top; // storage index
private int max; // total capacity
public Stack(int size)
{
ele = new int[size];//Maximum size of Stack
top = -1;
max = size;
}
Attributes of Stack
maxTop: the max size of stack top: the index of the top element of stack values: point to an array which stores elements of stack
Operations of Stack
IsEmpty: return true if stack is empty, return false otherwise IsFull: return true if stack is full, return false otherwise Top: return the element at the top of stack Push: add an element to the top of stack Pop: delete the element at the top of stack DisplayStack: print all the data in the stack
void Push(const double x); Push an element onto the stack If the stack is full, print the error information. Note top always represents the index of the top element. After pushing an element, increment top.
public void push(int item) { if (top == max - 1) { Console.WriteLine("Stack Overflow"); return; } else { ele[++top] = item; } }
void printstack()
Print all the elements
public void printStack() { if (top == -1) { Console.WriteLine("Stack is Empty"); return; } else {for (int i = 0; i <= top; i++) { Console.WriteLine("{0} pushed into stack", ele[i]); } } }
static main(void) { Stack p = new Stack(5); p.push(5); p.push(6); p.push(-3); p.push(-8); p.printStack(); p.size(); p.pop(); }
result
Implementation based on Linked List
Now, we will create our stack class. We will define a pointer, top, and initialize it to null. So, our LinkedListStack class will be:
Push an Element Onto a Stack
Pop an Element From the Stack
Peek the Element From Stack
https://www.geeksforgeeks.org/sorting-array-using-stacks/
Iterative tower of Hanoi
https://www.geeksforgeeks.org/iterative-tower-of-hanoi/
https://www.geeksforgeeks.org/create-mergable-stack/
Expression Evaluation
https://www.geeksforgeeks.org/expression-evaluation/
The Celebrity Problem
https://www.geeksforgeeks.org/the-celebrity-problem/