Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java stack using linked list

class StackUsingLinkedlist {
              private class Node {
                int data;
                Node next;
              }
            
              Node top;
            
              StackUsingLinkedlist() {
                this.top = null;
              }

            public void push(int data) {
              Node newNode = new Node();
              newNode.data = data;
              newNode.next = top;
              top = newNode;
            }
            
            public boolean isEmpty() {
              return top == null;
            }
            
            public int peek() {
              if (!isEmpty()) {
                return top.data;
              } else {
                System.out.println("Stack is empty");
                return -1;
              }
            }
            
            public void pop() {
              if (top == null) {
                System.out.print("
Stack empty");
                return;
              }
              top = top.next;
            }
            
            public void display() {
              if (top == null) {
                System.out.print("
Stack empty");
                exit(1);
              } else {
                Node node = top;
                while (node != null) {
                  System.out.printf("%d->", node.data);
                  node = node.next;
                }
              }
            }
Comment

stack implementation through linked list

// C++ program to Implement a stack
//using singly linked list
#include <bits/stdc++.h>
using namespace std;
 
// Declare linked list node
 
struct Node
{
    int data;
    struct Node* link;
};
 
struct Node* top;
 
// Utility function to add an element
// data in the stack insert at the beginning
void push(int data)
{
     
    // Create new node temp and allocate memory
    struct Node* temp;
    temp = new Node();
 
    // Check if stack (heap) is full.
    // Then inserting an element would
    // lead to stack overflow
    if (!temp)
    {
        cout << "
Heap Overflow";
        exit(1);
    }
 
    // Initialize data into temp data field
    temp->data = data;
 
    // Put top pointer reference into temp link
    temp->link = top;
 
    // Make temp as top of Stack
    top = temp;
}
 
// Utility function to check if
// the stack is empty or not
int isEmpty()
{
    return top == NULL;
}
 
// Utility function to return top element in a stack
int peek()
{
     
    // Check for empty stack
    if (!isEmpty())
        return top->data;
    else
        exit(1);
}
 
// Utility function to pop top
// element from the stack
void pop()
{
    struct Node* temp;
 
    // Check for stack underflow
    if (top == NULL)
    {
        cout << "
Stack Underflow" << endl;
        exit(1);
    }
    else
    {
         
        // Top assign into temp
        temp = top;
 
        // Assign second node to top
        top = top->link;
 
        // Destroy connection between
        // first and second
        temp->link = NULL;
 
        // Release memory of top node
        free(temp);
    }
}
 
// Function to print all the
// elements of the stack
void display()
{
    struct Node* temp;
 
    // Check for stack underflow
    if (top == NULL)
    {
        cout << "
Stack Underflow";
        exit(1);
    }
    else
    {
        temp = top;
        while (temp != NULL)
        {
 
            // Print node data
            cout << temp->data << "-> ";
 
            // Assign temp link to temp
            temp = temp->link;
        }
    }
}
 
// Driver Code
int main()
{
     
    // Push the elements of stack
    push(11);
    push(22);
    push(33);
    push(44);
 
    // Display stack elements
    display();
 
    // Print top element of stack
    cout << "
Top element is "
         << peek() << endl;
 
    // Delete top elements of stack
    pop();
    pop();
 
    // Display stack elements
    display();
 
    // Print top element of stack
    cout << "
Top element is "
         << peek() << endl;
          
    return 0;
}
 
// This code is contributed by Striver
Comment

Implement stack using Linked List Java

public class LinkedListStack {
    private Node head; // the first node
 
    // nest class to define linkedlist node
    private class Node {
        int value;
        Node next;
    }
 
    public LinkedListStack() {
        head = null;
    }
 
    // Remove value from the beginning of the list for demonstrating behaviour of stack
    public int pop() throws LinkedListEmptyException {
        if (head == null) {
            throw new LinkedListEmptyException();
        }
        int value = head.value;
        head = head.next;
        return value;
    }
 
    // Add value to the beginning of the list for demonstrating behaviour of stack
    public void push(int value) {
        Node oldHead = head;
        head = new Node();
        head.value = value;
        head.next = oldHead;
    }
 
    public static void main(String args[]) 
    {
        LinkedListStack lls=new LinkedListStack();
        lls.push(20);
        lls.push(50);
        lls.push(80);
        lls.push(40);
        lls.push(60);
        lls.push(75);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        System.out.println("Element removed from LinkedList: "+lls.pop());
        lls.push(10);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        printList(lls.head);
    }
    public static void printList(Node head) {
        Node temp = head;
        while (temp != null) {
            System.out.format("%d ", temp.value);
            temp = temp.next;
        }
        System.out.println();
    }
}
 
/**
 * 
 * Exception to indicate that LinkedList is empty.
 */
 
class LinkedListEmptyException extends RuntimeException {
    private static final long serialVersionUID = 1L;
 
    public LinkedListEmptyException() {
        super();
    }
 
    public LinkedListEmptyException(String message) {
        super(message);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: JFrame frame = new JFrame (); 
Java :: node in java 
Java :: how to get elements of a list in java 
Java :: how to convert line into string java 
Java :: Divide two integers without using multiplication, division and mod operator 
Java :: File Appender log4j2.properties spring 
Java :: how to name loops in java 
Java :: minimum and maximum in array in java 
Java :: string palindrome in java 
Java :: linked list java 
Java :: java ceil 
Java :: java 8 stream option 
Java :: java multithreading 
Java :: converting string to int in java 
Java :: java get enum by ordinal 
Java :: android navigation drawer open activity 
Java :: How to return the elements of a matrix in spiral order, in Java? 
Java :: android get sdk version 
Java :: java check if string ends with 
Java :: android foreground services set auto cancel not working 
Java :: java windowbuilder multiple monitors windowed mode 
Java :: java print boolean with spaces 
Java :: Create JDBC connection using properties file 
Java :: java stream inner join two lists 
Java :: java calcuate milliseconds since 1970 
Java :: springbootservletinitializer maven dependency 
Java :: split string into array java 
Java :: module java.base does not "opens java.io" to unnamed module 
Java :: list of strings java 
Java :: array de meses java 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =