Can You Help Me Debug My Java LinkedList Stack Implementation?

  • Java
  • Thread starter grouchy
  • Start date
  • Tags
    Java
In summary, the conversation is about using a list class and a Linkedlist class to create a stack. The person is having trouble with their remove from head method and is unsure about their addHead method. They are looking for someone to review their code and provide some guidance. The expert suggests being more careful about keeping track of head and tail pointers, and also questions the need for a tail pointer in a singly-linked list used as a stack. The person updates their code and reports that their addHead and removeHead methods are working as intended, but they are having trouble with their addTail method always adding to the head.
  • #1
grouchy
73
0
Hi, i need to use a list class (provided and complete) and then use a Linkedlist class (need to add the methods) to get a stack going. I am having trouble with my remove from head method although I ain't too sure if my addHead method is right... I'd appreciate it if someone could look over my code and give me some pointers. I wrote in bold and caps where I aint sure. thx.

class Link
{
public Object data;
public Link next;
public Link(Object d, Link n)
{
data = d; next = n;
}
}



//LINKEDLIST CLASS AND MAIN
import java.util.Scanner;

class LinkList
{
private Link head; // reference to first Link

private Link tail; // reference to last Link

private int size;

public LinkList()
{

tail = null;
head = null;

}

public Object peekHead() // return reference to first Object
{
return head.data;
}

public Object peekTail() // return reference to last Object
{
return tail.data;
}

//THE ADD METHOD, NOT SURE IF DONE RIGHT
public void addHead(Object newData)
{
if (head == null)
{
head = new Link(newData, tail);


}

else if (head != null && tail == null)
{
tail = head;
head = new Link (newData, tail);
}
else
{

head.next = head;
head = new Link (newData, head.next);

}
}

public void addTail(Object newData)
{

}

//THE REMOVE METHOD
public Object removeHead()
{
Link removing = head;
if (head != null)
{
head = head.next;
}

return removing.data;

}

//MAIN METHOD
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);

int choice = 0;

LinkList first = new LinkList();

while (choice != 6)
{

System.out.println("What would you like to do? (enter the number)");
System.out.println("1 - Push onto Head of Stack.");
System.out.println("2 - Remove from Head of Stack.");
System.out.println("3 - Peek at Head of Stack.");
System.out.println("4 - Push at Tail of Stack.");
System.out.println("5 - Peak at Tail of Stack.");
System.out.println("6 - Close Program.");
choice = scan.nextInt();


switch(choice)
{
case 1:
System.out.println("What do you want to push on Head?");
Object pushingHead = scan.next();

first.addHead(pushingHead);
break;

case 2:
System.out.println("Removing: " + first.removeHead());
break;

case 3:
System.out.println("Peeking at Head of Stack: " + first.peekHead());
break;

case 4:
System.out.println("What do you want to push on Tail?");
Object pushingTail = scan.next();

first.addHead(pushingTail);
break;

case 5:
System.out.println("Peeking at Tail of Stack: " + first.peekTail());
break;

case 6:
System.out.println("Good Bye!");
break;
}


}

}
}
 
Technology news on Phys.org
  • #2
Hmm...

Your removehead method looks generally right, but one question. Think about what happens when you remove the VERY LAST object in the whole linked list. What happens to "tail"?

Your addhead on the other hand contains a lot of problems. I'm just going to write in some comments noting the problems I see, let me know if I'm not clear enough:

Code:
public void addHead(Object newData)
{
if (head == null)
{
head = new Link(newData, tail); // If head is null, then won't tail also be null?
}

else if (head != null && tail == null) // It should never be possible for this case to be true. Head is supposed to be the first item of the list. Tail is supposed to be the last item of the list. If either head or tail is null, ever, then that implies the list is empty.
{
tail = head; // In other words, you just set the end of the list to be the new start of the list. Why?
head = new Link (newData, tail); // As required by the "if" statement above, tail is currently null. Is this what you want?
}

else
{
head.next = head; // Think about this: You are setting "the item after A" to be equal to "A". You just turned head into a very small linked list pointing to itself.
head = new Link (newData, head.next); // "Set the new head to be a Link object containing newData, and followed by the item AFTER the current head". Is this what you want?

}
}

I think you need to be more careful in general about thinking: What should head be pointing to? What should tail be pointing to? If at any given moment I start with the object pointed to by "head" and step forward until I hit null, which objects will I see on the way if the list is working properly?

One more thing: why *are* you keeping track of a tail pointer? Usually "tail" is something people use in conjunction with a doubly-linked list. Your list is singly-linked. And if you intend to use this linked list only as a stack, then there is no reason whatsoever to keep track of the tail.
 
  • #3
ok, i think I know what you mean. Also, about the tail pointer, I am supposed to have it reference my last object on the stack.

I tried to change somethings around to make more sense. The problem, for example if I addHead A, then B, then C, then D. When I go to remove,the first time it removes D, but the second attempt (and third, and fourth, etc) it just tells me its removing C but doesn't actually do it. I also tried to finishish addTail and it just adds to the front... so I need to keep working on that one.

Thx for the tips btw.

here is the updated code...


public void addHead(Object newData)
{
if (head == null && tail == null)
{
head = new Link(newData, tail);
tail = head;


}

else if (head != null && tail != null)
{
head.next = head;
head = new Link (newData, head.next);

if(head.next == null)
{
tail = head;
}
}
}

public void addTail(Object newData)
{
Link current;
if (head == null && tail == null)
{
head = new Link(newData, tail);
tail = head;
}
else
{
while (head != null)
{
head = head.next;
}
head = new Link(newData, tail);
tail = head;
}
}

public Object removeHead()
{
Link removing = head;
if (head != null)
{
head = head.next;
if(head.next == null)
{
tail = head;
}

}

return removing.data;
 
  • #4
UPDATE: Ok, I think my addHead and removeHead methods work as intended. They seem to add and remove (thx for pointing out that tail would remain in the end). Only problem I'm having now is addTail. it always seems to add to the head. Here is the code.

public void addHead(Object newData)
{

if (head == null && tail == null)
{
head = new Link(newData, tail);
tail = head;

}

else
{
head = new Link(newData,head);
}
}

public void addTail(Object newData)
{
if (head == null && tail == null)
{
head = new Link(newData, tail);
tail = head;


}

else if (head != null && tail != null)
{
while(head != null)
head = head.next;
head = new Link(newData,head);
tail = head;
}
}

public Object removeHead()
{
{
Link removing = head;
if (head != null)
{
head = head.next;
}
if (head == null)
tail = null;

return removing.data;

}

}
 
  • #5
Hi Grouchy,

A clause from your AddTail... consider what this code does:

Code:
else if (head != null && tail != null)
{
	while(head != null) // Until the head pointer becomes equal to null,
		head = head.next; // Set the head pointer equal to the next item in the list.
	head = new Link(newData,head); // Set the head pointer to a new node, followed by the head pointer (which, after your last two lines will always be...?) 
	tail = head; // Set the tail pointer to the head pointer
}
 

What is a stack using linkedlist in Java?

A stack using linkedlist in Java is a data structure that follows the principle of last in, first out (LIFO). It is implemented using a linkedlist where the top element of the stack is always the first element of the linkedlist. This allows for efficient insertion and deletion of elements at the top of the stack.

How is a stack using linkedlist implemented in Java?

A stack using linkedlist in Java can be implemented by creating a linkedlist class and adding methods to push an element onto the top of the stack, pop an element from the top of the stack, peek at the top element without removing it, and check if the stack is empty. The top of the stack can be represented by the head of the linkedlist.

What are the advantages of using a stack using linkedlist in Java?

There are several advantages to using a stack using linkedlist in Java. Firstly, it allows for efficient insertion and deletion of elements at the top of the stack. Secondly, it is a dynamic data structure, meaning it can grow and shrink as needed. Additionally, a linkedlist-based stack does not have a fixed size, unlike an array-based stack, making it more flexible.

What are the disadvantages of using a stack using linkedlist in Java?

One disadvantage of using a stack using linkedlist in Java is that it requires more memory compared to an array-based stack. This is because a linkedlist stores additional pointers for each element. Additionally, accessing elements at the bottom of the stack is less efficient compared to accessing elements at the top, as it requires traversing through the entire linkedlist.

What is the time complexity of operations on a stack using linkedlist in Java?

The time complexity for push, pop, and peek operations on a stack using linkedlist in Java is O(1) as they only involve updating the top element of the stack. However, the time complexity for checking if the stack is empty is O(1), while the time complexity for accessing elements at the bottom of the stack is O(n), where n is the number of elements in the stack.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top