LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2004, 11:40 PM   #1
|2ainman
Member
 
Registered: Mar 2004
Distribution: Slackware current, DSL 0.9.2
Posts: 133

Rep: Reputation: 15
linked lists w/ nodes?


I'm having a bit of a problem with my hw. Yes, I know that I'm not supposed to ask for help on the actual assignment, thats why I will not be mentioning it. If people could just give me a little background on linked lists with nodes, ie how to create them, manipulate them, etc. I dont think my teacher did a very good job explaining, and the book is rather confusing too :-/
Thx to everyone and anyone in advance.
 
Old 05-19-2004, 01:32 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
struct my_struct
{
  int num;
  struct my_struct *next;
};

struct my_struct *list_head = NULL;

struct my_struct *new_node(void)
{
  struct my_struct *new;

  new = malloc(sizeof(my_struct));
  // Possibly do some initialization here for new nodes.

  new->next = list_head;
  list_head = new;

  return new;
}

int main(void)
{
  struct my_struct *ptr;

  ptr = new_node();
  ptr->num = 86;

  ptr = new_node();
  ptr->num = 17;

  for(ptr = list_head;ptr != NULL;ptr = ptr->next)
    printf("%d\n", ptr->num);

  return 0;
}
Now, this is just one way to implement a linked list and it's really not very useful in this little example program. There are an uncountable number of uses for linked lists and there are also several different kinds.

The one in my example is a very simple one known as a singly-linked list or just linked list. The start of the list is marked by the variable list_head. The end of the list is marked by a NULL value. This makes cycling through the list very simple and straight-forward, as you can see from the example. Every time a node is created, it's added to the head of the list. The output from the program above would show 17 before 86.

There are also implementations where the tail of the list (the last node) points to the head of the list instead of pointing to NULL. This is known as a circularly-linked list (just try to picture it and you can see why).

Then you can also have doubly-linked lists. In that case there would be a prev pointer in addition to the next pointer inside the structure. The prev pointer would (duh) point to the node before it. If you're planning on doing a lot of node removal this makes it a lot easier. It also adds the ability to cycle through the list backwards. The next pointer of the list tail would point to NULL and the prev pointer of the list head would also point to NULL.

Then you have your circular doubly-linked lists. This is where the next pointer of the list tail points to the list head and the prev pointer of the list head points to the list tail.

You can also have different combinations of what's listed above or even have more than just a next and prev pointer or they might have names like left and right instead (as is the case usually for binary trees). But the styles I listed above are common.

The bottom line, I guess, is linked lists are highly flexible and can look however you want, but if you have a structure with a member that's a pointer to the same structure then you can bet it's used for a linked list. Each instance of the structure in the list is a node.

Linked lists are very useful in situations where you might normally use an array, but you'll be doing a lot of insertions and deletions. With an array you'd have to do a lot of value copying to make it happen, but with a linked list you just change a couple of pointers. They're also used in hash tables (basically an array of linked lists), vitual memory mangers, and compression algorithms, just to name a few examples.

There's enough information on linked lists that you could write whole book on them (and I'm sure there's a few out there already!) so I don't want to get into too much detail. Hopefully this information gives you an understandable overview, but I'll be happy to answer any specific questions you might have.

Last edited by itsme86; 05-19-2004 at 01:44 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 01:59 AM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
queue and linked lists Palamides Programming 2 03-09-2005 08:08 PM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration