![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Linked list insertion at the tail
This is so simple, yet I can't understand why it won't work.
I'm trying to implement a linked list where insertions occur at the tail. I have the constructors public SInfo() {
head = null;
tail = null;
}
public SInfo(SInfoNode m) {
head = m;
m.next = tail;
}Here is my insert method: public void insert(SInfoNode nodeToInsert) {
if (head == null) {
head = nodeToInsert;
nodeToInsert.next = tail;
} else {
tail = nodeToInsert;
nodeToInsert.next = tail;
}
}This code worked fine with insertion at the head (with a different insert method of course). I've tried a lot of different things for insertion at the tail, but when I run the program, it runs without ceasing. What am I missing? |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
You forget to set the current tail->next to be the new node.
If you want to add at the end, try something like this: tail->next = nodeToInsert; tail = nodeToInsert; |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I'm confused... why do you need a tail pointer in the first place?
public SInfo() {
head = null;
}
public SInfo(SInfoNode m) {
head = m;
m.next = null;
}
public void insert(SInfoNode nodeToInsert) {
while (head != null) {
head = head.next;
}
head = nodeToInsert;
head.next = null;
} |
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Makes sense, I guess.
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Quote:
I'll spare you the detailed algorithmic analysis. ![]() |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
The solution proposed by para does work. One small modification tho, my constructors had an error too
public SwipeInfo() {
head = null;
tail = null;
}
public SwipeInfo(SwipeInfoNode m) {
head = m;
tail = m;
} |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Quote:
__________________
HijackThis Team-SFDC |
|
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Quote:
When you add a series of numbers together like 5 + 6 + 7 + (n - 1) + n, where n is the highest number you will reach, the sum of all those numbers turns into the arithmetic series. That simplifies to order n. Thus, it take linear time to do the insertions, i.e. the time it takes to do the insertion is directly proportional to the size of the list...the list doubles in size, so does the time. Why do something in linear time when you can do much better in constant time with an extra tail pointer? :p |
|
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Quote:
![]()
__________________
HijackThis Team-SFDC |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|