Topic: Implement a stack & a queue.
public class List {
Object data;
List next;
public List(Object d) {
this.data = d;
this.next = null;
}
public List(){};
}
public class Stack {
List top;
Object pop(){
if (top!=null){
Object pop=top.data;
top=top.next;
return pop;
}
return null;
}
void push(Object item){
List t=new List(item);
t.next=top;
top=t;
}
Object peek(){
return top.data;
}
}
public class Queue {
List first, last;
void enqueue(Object item){
if(first==null){
last=new List(item);
first=last;
}else{
last.next=new List(item);
last=last.next;
}
}
Object dequeue(){
if(first!=null){
Object item=first.data;
first=first.next;
return item;
}
return null;
}
}