• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Linked List

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you make a for loop that starts at the end of a linked list? I am asking this because I am writing a method to reverse the order of the linked list in another new list. I want to start from the end of the list.
 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going down the wrong track with this.

Just think about like this: If you have linked list  1 -> 2 -> 3 -> 4 -> 5 then to create a reversed list from this,

1
2 -> 1
3 -> 2 -> 1
4 -> 3 -> 2 -> 1
5 -> 4 -> 3 -> 2 -> 1

See how that's done? You only have to traverse the original list the normal way and <do what?> to create a new list that's reversed.

It shouldn't be hard to figure out what <do what?> is...
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your requirement (based on what you posted in the other thread) says "and creates a new list..." -- this means that the original list should not be modified; you should create a new list that is the reverse of the original list provided.
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You're going down the wrong track with this.

Just think about like this: If you have linked list  1 -> 2 -> 3 -> 4 -> 5 then to create a reversed list from this,

1
2 -> 1
3 -> 2 -> 1
4 -> 3 -> 2 -> 1
5 -> 4 -> 3 -> 2 -> 1

See how that's done? You only have to traverse the original list the normal way and <do what?> to create a new list that's reversed.

It shouldn't be hard to figure out what <do what?> is...


This is what I have so far:

The method header is what the teacher gave us.
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Note that your requirement (based on what you posted in the other thread) says "and creates a new list..." -- this means that the original list should not be modified; you should create a new list that is the reverse of the original list provided.


The problem is I don't know how to go backwards.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the problem. You really dont need to go backwards. Besides, that kind if linked list probably doesn't support going backwards. Look at my first reply carefully. Go back over your notes and see what list operation you can use to build up a list the way I showed.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Please don't quote entire posts if you don't need to.

2. The problem is not going backwards on the original list. I already told you and showed you that's not the way to solve this.  Besides, the original list probably doesn't support going backwards anyway because I don't think it's a doubly-linked list, it's just a single-linked list which means you can only from head to tail.

If I must give it to you step by step, then I will, but you have to think about this a little.

Create an empty list, reversed.
Starting from original.head, traverse the original list.
For each node traversed in original, create a new node and <do what???> it to the <what???> of reverse list.

That's all I will give you. You have to figure out what <do what???> and <what???> are.
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make sure I'm not making false assumptions here, your instructor did already discuss or show how to do some basic operations on linked lists, right? Like search for a node, delete a node, append a node, and insert a node, right? Among those four operations, which one do you think is going to be useful to complete this exercise?
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that we should add it to the beginning of reverse list.
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the correct syntax: reverse.setValue(addFirst());
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Just to make sure I'm not making false assumptions here, your instructor did already discuss or show how to do some basic operations on linked lists, right? Like search for a node, delete a node, append a node, and insert a node, right? Among those four operations, which one do you think is going to be useful to complete this exercise?


We never really went over the LinkedList methods only the ListNode methods(getValue(),getNext(),setValue(),setNext() )
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:We never really went over the LinkedList methods only the ListNode methods(getValue(),getNext(),setValue(),setNext() )


It doesn't matter that you haven't gone over the LinkedList methods. You can solve this exercise with what you know about ListNode (getValue(), setValue(), getNext(), setNext()) -- you just have to keep track of which node is the "head" node in any list that you're working with.

That's all I can tell you without depriving you of the satisfaction of solving this problem yourself. I would advise you to sit down and review your notes and maybe draw what you're trying to do using a pencil and paper.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . You really dont need to go backwards. . . .

Nor do you need access to any list nodes. You need a proper add(int, E) method, like this. I would make the list node class a private nested class of the linked list class. I would also make a method to create a reversed list an instance method of the linked list class:-Remember the importance of the documentation comments, which tell you what you what that method does.

By the way: how did you get on counting letters?
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems the way they're being taught, Campbell, is not very OO. What I can infer from the requirements is that the "List" is identified by the ListNode that is the "head" node. There is no concept of a List that encapsulates operations on ListNode objects as you probably have in mind.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . is not very OO. . . .

Do you mean not at all OO? I agree that outside code shouldn't be able to access a node in a linked list. The add(int, E) method I mentioned does however need access to the appropriate nodes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!