Lecture 4 Stack DSA
Lecture 4 Stack DSA
AND ALGORITHMS
STACK
• Its Static and Dynamic. But it cannot be both at the same time
• Via Array
What is Stack - Continued
• It follows LIFO ( Last in first out )
• The first on top of stack of plates is the one that is picked first also
the last one that is placed on stack
• Push
Means to insert a new item
• Pop
Means to delete an item
Stack Operations
• Key Operations:
Note: This list does not mean its best in these applications
import java.util.Stack;
• fixed size stack has a fixed size and cannot grow or shrink dynamically.
• No overflow issue!
• No underflow issue!
Stack-Link list
Reference to diagrams in ppt notes tab
Stack-Link list
example
Stack-Link list
Push or Pop at end of linked list
Stack-Link list
Push or Pop at end of linked list
Stack-Link list
Push or Pop at beginning of linked list
Stack-Link list
Push or Pop at beginning of linked list
Stack- Linked List
public class StackAsLinkedList { public void push(int data)
{
StackNode root; StackNode newNode = new StackNode(data);
static class StackNode { if (root == null) {
root = newNode;
int data; }
StackNode next; else {
StackNode temp = root;
StackNode(int data) { this.data = data; } root = newNode;
} newNode.next = temp;
}
System.out.println(data + " pushed to stack");
public boolean isEmpty() }
sll.push(10);
sll.push(20);
sll.push(30);
• Data structure
• Array
• linked list
• Stack
• Assignment 1 announcement
• CHAT-GPT
• Prep insta
THANK YOU