This is a Stack class by using Linked list. I push the new data at the tail, so
ID: 3840960 • Letter: T
Question
This is a Stack class by using Linked list. I push the new data at the tail, so I pop at the tail too. However, there are something wrong for the pop method. I push "R","a","c","e","c","a", and "r", but I cannot pop out the very last Node "R". How to fix it?
public class Stack <T>
{
private class Node{
private T data;
private Node link;
public Node(){
data = null;
link = null;
}
public Node(T input, Node next){
data = input;
link = next;
}
}
private Node head;
public Stack(){
head = null;
}
public void push(T input){
if (head==null){
head = new Node(input, null);
}
else{
Node position = head;
while (position.link!=null){//stop at last node
position = position.link;
}
Node a = new Node(input, null);
position.link = a;
}
}
public T pop(){
Node removed = new Node();
Node position = head;
int count=1;
int length = size();
while (count<length-1){//stop at second last node
position = position.link;
count++;
}
removed = position.link;
position.link = null;
return removed.data;
}
public int size(){
int count = 0;
Node next = head;
if (head == null){
count=0;
}
else{
while (next!=null){
next = next.link;
count++;
}
}
return count;
}
public boolean isEmpty(){
return (size()==0);
}
public void outputList( ) {
Node current = head;
while (current != null) {
System.out.println(current.data);
current = current.link;
}
}
public static void main(String[] args) {
Stack<Character> a = new Stack<Character>();
// Queue<Character> q = new LinkedList<Character>();
a.push('R');
a.push('a');
a.push('c');
a.push('e');
a.push('c');
a.push('a');
a.push('r');
System.out.println("Size : " + a.size());
a.outputList();
while(!a.isEmpty()) {
System.out.println(a.pop());
}
}
}
Explanation / Answer
public class Stack <T>
{
private class Node{
private T data;
private Node link;
public Node(){
data = null;
link = null;
}
public Node(T input, Node next){
data = input;
link = next;
}
}
private Node head;
public Stack(){
head = null;
}
public void push(T input){
if (head==null){
head = new Node(input, null);
}
else{
Node position = head;
while (position.link!=null){//stop at last node
position = position.link;
}
Node a = new Node(input, null);
position.link = a;
}
}
public T pop(){
Node removed = new Node();
Node position = head;
Node pre = head;
if(head.link == null){
removed = head;
head = null;
return removed.data;
}
while (position.link != null){//stop at second last node
pre = position;
position = position.link;
}
removed = pre.link;
pre.link = null;
return removed.data;
}
public int size(){
int count = 0;
Node next = head;
if (head == null){
count=0;
}
else{
while (next!=null){
next = next.link;
count++;
}
}
return count;
}
public boolean isEmpty(){
return (size()==0);
}
public void outputList( ) {
Node current = head;
while (current != null) {
System.out.println(current.data);
current = current.link;
}
}
public static void main(String[] args) {
Stack<Character> a = new Stack<Character>();
// Queue<Character> q = new LinkedList<Character>();
a.push('R');
a.push('a');
a.push('c');
a.push('e');
a.push('c');
a.push('a');
a.push('r');
System.out.println("Size : " + a.size());
a.outputList();
System.out.println();
while(!a.isEmpty()) {
System.out.println("poped "+a.pop());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.