Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 6th, 2006, 4:30 PM   #1
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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?
elford is offline   Reply With Quote
Old Jan 6th, 2006, 4:53 PM   #2
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
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;
para is offline   Reply With Quote
Old Jan 6th, 2006, 8:50 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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;
	}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 6th, 2006, 9:12 PM   #4
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
Quote:
Originally Posted by Ooble
I'm confused... why do you need a tail pointer in the first place?
Faster to add to the end, you don't have to go through the whole list to find the last node.
para is offline   Reply With Quote
Old Jan 6th, 2006, 11:09 PM   #5
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
Quote:
Originally Posted by para
Faster to add to the end, you don't have to go through the whole list to find the last node.
Exactly it. In lists where order of the nodes matters, such as a queue, it allows you to insert in constant time, as opposed to looping through the list every time when you need to add at the end, which takes n^2 time.

I'll spare you the detailed algorithmic analysis.
elford is offline   Reply With Quote
Old Jan 6th, 2006, 9:23 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Makes sense, I guess.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 6th, 2006, 11:13 PM   #7
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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;
	}
elford is offline   Reply With Quote
Old Jan 6th, 2006, 11:30 PM   #8
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
Quote:
as opposed to looping through the list every time when you need to add at the end, which takes n^2 time.
I must have missed something. How do you figure going through a list once is order n^2? There are only n elements, and you only need to iterate through them once.
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Jan 7th, 2006, 12:50 AM   #9
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
Quote:
Originally Posted by groovicus
I must have missed something. How do you figure going through a list once is order n^2? There are only n elements, and you only need to iterate through them once.
I wasn't clear, and I made a mistake. It takes n time when doing insertions, not n^2. Figure that you start with a list of 5 nodes, and then you insert another node at the end (w/o the tail pointer). The loop must excute 5 times to insert. Then you insert a second element. The loop must execute 6 times. Then 7 time...etc.

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
elford is offline   Reply With Quote
Old Jan 7th, 2006, 10:04 AM   #10
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
Quote:
I wasn't clear, and I made a mistake.
I already knew that. We know how to do asymptotic analysis too
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC