repl.it
@ABHISHEKAREER/

stack using linklist in java (datastructure)

Java

No description

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.lang.*;
import java.util.*;
import java.io.*;
class Node
{
  Node next;
  char data;
  Node(char data)
  {
    this.data=data;
    this.next=null;
  }
}
class Stack
{
  public Node head;
  public void push(char data)
  {
    Node newNode = new Node(data);
    if(isEmpty(head))
    {
      head=newNode;
    }
    else
    {
      newNode.next=head;
      head=newNode;
    }
  }
  public char pop()
  {
    if(isEmpty(head))
    {
      return '!';
    }
    else
    {
      char data=head.data;
      head=head.next;
      return data;
    }
  }
  public boolean isEmpty(Node temp)
  {
    if(temp==null)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  public void traverse()
  {
    Node temp=head;
    while(temp!=null)
    {
      System.out.print(temp.data);
      temp=temp.next;
    }
  }
}
class Main
{
  public static void main(String [] args)
  {
    Stack s1=new Stack();
    System.out.println("enter the data");
    Scanner ss=new Scanner (System.in);
    String s=ss.nextLine();
    int length=s.length();
    for(int i=0;i<length;i++)
    {
      s1.push(s.charAt(i));
    }
    s1.traverse();
    System.out.println();
    char ch=s1.pop();
    while(ch!='!')
    {
      System.out.println(ch);
      ch=s1.pop();
    }
  }
}
Fetching token
?