|
| 1 | +// Java program to implement a stack using singly linked |
| 2 | +// list |
| 3 | + |
| 4 | +// Class representing a node in the linked list |
| 5 | +class Node { |
| 6 | + int data; |
| 7 | + Node next; |
| 8 | + Node(int new_data) { |
| 9 | + this.data = new_data; |
| 10 | + this.next = null; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +// Class to implement stack using a singly linked list |
| 15 | +class Stack { |
| 16 | + |
| 17 | + // Head of the linked list |
| 18 | + Node head; |
| 19 | + |
| 20 | + // Constructor to initialize the stack |
| 21 | + Stack() { this.head = null; } |
| 22 | + |
| 23 | + // Function to check if the stack is empty |
| 24 | + boolean isEmpty() { |
| 25 | + |
| 26 | + // If head is null, the stack is empty |
| 27 | + return head == null; |
| 28 | + } |
| 29 | + |
| 30 | + // Function to push an element onto the stack |
| 31 | + void push(int new_data) { |
| 32 | + |
| 33 | + // Create a new node with given data |
| 34 | + Node new_node = new Node(new_data); |
| 35 | + |
| 36 | + // Check if memory allocation for the new node |
| 37 | + // failed |
| 38 | + if (new_node == null) { |
| 39 | + System.out.println("\nStack Overflow"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Link the new node to the current top node |
| 44 | + new_node.next = head; |
| 45 | + |
| 46 | + // Update the top to the new node |
| 47 | + head = new_node; |
| 48 | + } |
| 49 | + |
| 50 | + // Function to remove the top element from the stack |
| 51 | + void pop() { |
| 52 | + |
| 53 | + // Check for stack underflow |
| 54 | + if (isEmpty()) { |
| 55 | + System.out.println("\nStack Underflow"); |
| 56 | + return; |
| 57 | + } |
| 58 | + else { |
| 59 | + |
| 60 | + // Assign the current top to a temporary |
| 61 | + // variable |
| 62 | + Node temp = head; |
| 63 | + |
| 64 | + // Update the top to the next node |
| 65 | + head = head.next; |
| 66 | + |
| 67 | + // Deallocate the memory of the old top node |
| 68 | + temp = null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Function to return the top element of the stack |
| 73 | + int peek() { |
| 74 | + |
| 75 | + // If stack is not empty, return the top element |
| 76 | + if (!isEmpty()) |
| 77 | + return head.data; |
| 78 | + else { |
| 79 | + System.out.println("\nStack is empty"); |
| 80 | + return Integer.MIN_VALUE; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Driver code |
| 86 | +public class Stack_linkedlist { |
| 87 | +// stack<Character>st = new stack<>(); |
| 88 | + |
| 89 | + public static void main(String[] args) |
| 90 | + { |
| 91 | + // Creating a stack |
| 92 | + Stack st = new Stack(); |
| 93 | + |
| 94 | + // Push elements onto the stack |
| 95 | + st.push(11); |
| 96 | + st.push(22); |
| 97 | + st.push(33); |
| 98 | + st.push(44); |
| 99 | + |
| 100 | + // Print top element of the stack |
| 101 | + System.out.println("Top element is " + st.peek()); |
| 102 | + |
| 103 | + // removing two elemements from the top |
| 104 | + System.out.println("Removing two elements..."); |
| 105 | + st.pop(); |
| 106 | + st.pop(); |
| 107 | + |
| 108 | + // Print top element of the stack |
| 109 | + System.out.println("Top element is " + st.peek()); |
| 110 | + } |
| 111 | +} |
0 commit comments