-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStackOfStrings.java
More file actions
49 lines (46 loc) · 1.17 KB
/
LinkedStackOfStrings.java
File metadata and controls
49 lines (46 loc) · 1.17 KB
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
// maintain pointer to first node and remove/add from front.
public class LinkedStackOfStrings {
private Node first=null;//inner class variables as stack
// public LinkedStackOfStrings(){
// Node n=new Node();
// System.out.println(n.item+"--");
// }
//inner class
//should not have static members
private class Node{
String item;//instance variables have a default value
Node next; //object
public String toString(){
return item ;
}
}
public boolean isEmpty(){
return first==null;
}
public void push(String item){
Node oldfirst=first;
// System.out.println(first);
first=new Node();
first.item=item;
first.next=oldfirst;
System.out.println(first.item);
System.out.println(first.next);
// System.out.println(first + "===========");
}
// public String pop(){
// String item=first.item;
// first=first.next;
// return item;
// }
public static void main(String[] args ){
LinkedStackOfStrings l=new LinkedStackOfStrings();
// System.out.println(l.first);
// System.out.println("====");
String s="hello";
l.push(s);
String sa="hello2";
l.push(sa);
String sab="hello3";
l.push(sab);
}
}