package Stack;
import LinkedList.ListNode;
public class LinkedStack<T> {
public int length;
public ListNode top;
public LinkedStack(){
length = 0;
top = null;
}
public void push(int data){
ListNode temp = new ListNode(data);
temp.setNext(top);
top = temp;
length++;
}
public int pop(){
int result = top.getData();
System.out.println(result);
top = top.getNext();
length--;
return result;
}
public int peek(){
int result = top.getData();
return result;
}
public boolean isEmpty(){
return (length == 0);
}
public int size(){
return length;
}
public String toString(){
String result = "[";
ListNode current = top;
while(current!= null){
result = result+current.getData()+",";
current = current.getNext();
}
result+="]";
return result;
}
public static void main(String[] args) {
LinkedStack<Integer> obj = new LinkedStack<Integer>();
obj.push(1);
obj.push(3);
obj.push(3);
System.out.println(obj);
obj.pop();
}
}
import LinkedList.ListNode;
public class LinkedStack<T> {
public int length;
public ListNode top;
public LinkedStack(){
length = 0;
top = null;
}
public void push(int data){
ListNode temp = new ListNode(data);
temp.setNext(top);
top = temp;
length++;
}
public int pop(){
int result = top.getData();
System.out.println(result);
top = top.getNext();
length--;
return result;
}
public int peek(){
int result = top.getData();
return result;
}
public boolean isEmpty(){
return (length == 0);
}
public int size(){
return length;
}
public String toString(){
String result = "[";
ListNode current = top;
while(current!= null){
result = result+current.getData()+",";
current = current.getNext();
}
result+="]";
return result;
}
public static void main(String[] args) {
LinkedStack<Integer> obj = new LinkedStack<Integer>();
obj.push(1);
obj.push(3);
obj.push(3);
System.out.println(obj);
obj.pop();
}
}
No comments:
Post a Comment