Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver
- {
- public static void main(String args[])
- {
- Stack S = new Stack();
- S.push(0);
- S.push(5);
- Stack T = copyStack(S);
- T.display();
- S.display();
- }
- public static Stack copyStack(Stack S)
- {
- Stack st = new Stack(), T = new Stack();
- while (S.isEmpty() == false)
- {
- st.push(S.pop().data);
- }
- while (st.isEmpty() == false)
- {
- int temp = st.pop().data;
- T.push(temp);
- S.push(temp);
- }
- return T;
- }
- }
- class Node
- {
- int data;
- Node next;
- public Node(int d)
- {
- data = d;
- }
- }
- class Stack
- {
- Node Top;
- public void push(int d)
- {
- Node temp = new Node(d);
- temp.next = Top;
- Top = temp;
- return;
- }
- public Node pop()
- {
- if (isEmpty())
- {
- System.out.println("Can't pop, The Stack is Empty");
- return null;
- }
- Node temp = Top;
- Top = Top.next;
- return temp;
- }
- public Node top()
- {
- if (isEmpty())
- {
- System.out.println("The Stack is Empty");
- }
- return Top;
- }
- public void display()
- {
- if (isEmpty())
- {
- System.out.print("The Stack is Empty");
- }
- Node curr = Top;
- while (curr != null)
- {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- return;
- }
- public boolean isEmpty()
- {
- return Top == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment