Link class:
public class Link {
public long data;
public Link next;
public Link(long data) {
this.data = data;
}
public void displayLink() {
System.out.println("data :"+ data);
}
}
LinkedList class:
public class LinkedList {
private Link first;
public LinkedList() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public void insertFirst(long data) {
Link newLink = new Link(data);
newLink.next = first;
first = newLink;
}
public long deleteFirst() {
Link temp = first;
first = first.next;
return temp.data;
}
public void displayList() {
Link current = first;
while (current != null) {
System.out.println(current.data + "----");
current = current.next;
}
}
}
LinkStack class:
public class LinkStack {
private LinkedList theList;
public LinkStack() {
theList = new LinkedList();
}
public void push(long data) {
theList.insertFirst(data);
}
public void pop() {
theList.deleteFirst();
}
public boolean isEmpty() {
return theList.isEmpty();
}
public void displayList() {
System.out.println("displaying the stack---------------");
theList.displayList();
}
}
MainApp:
public class MainApp {
public static void main(String[] args) {
LinkStack stack = new LinkStack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.displayList();
stack.pop();
stack.displayList();
}
}
No comments:
Post a Comment