Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-07-2003, 05:11 PM   #1 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
Linked Lists!

Ah yes, those darn linked lists. Linked lists are quite simple data structures, which work kind of like arrays but with one major difference, they can grow and shrink as needed, which is good because it makes an efficient use of memory. A linked list consists of a node, which contains a key with a value and a link to the next node. Here is how you define a node datatype:

Code:
struct node
    { 
     int key;
     node* next;
     };
Now, let's construct a nice simple linked list. Now, in common application, some find it easy to create "dummy nodes" for the beginning of the list and the end of the list. These nodes have a null value, the root node pointing to the first item, the last node pointing to itself. Here we go...

Code:
typedef node* node_ptr; // makes it easy for parameter passing

int main(void)
     {
      node_ptr root = NULL, z = NULL, t = NULL, tmp = NULL;
As you noticed, I initialized each node to null. You don't need to do this, I just do this to avoid some problems that could happen. Now, we have defined 3 pointers, root, which will point to the beginning, z, which will signify the last node, and t, which is the pointer we will use to add new nodes to the list and cycle through the list. Now, let's create nodes for the pointers to point to(rather than pointing into the depths of NULL):

Code:
root = new node;
z = new node;

/** Now let's have them point to something **/

root->next = z;
z->next = z;
Now we have two empty nodes, one pointing to z, and z pointing to itself. You don't need to use these, and some of us rarely do, but some consider it good practice.

Alright, now lets begin adding a few things to the list. First let's add the number 12 to the list:

Code:
t = new node;
t->key = 12;

/** now to insert it into the list **/

root->next = t;
t->next = z;
There, now we have a single item in the list. Now, let's say you want to add more to the end of the list, leave t pointing at the last position, and all you need to do is add to it:

Code:
tmp = new node; 
tmp->key = 5; 
tmp->next = z;

t->next = tmp; //points t's link to the new node
t = t->next; //moves the pointer to the next node.
that's simple enough. Now let's print the nodes out for our linked list:
Code:
t = root->next;

cout << "The first item in the list is " << t->key << endl;

t = t->next;

cout << "The 2nd item in the list is " << t->key <<endl;
Pretty easy huh? Now let's delete an item. let's say I want to delete the 1st item.

Code:
tmp = root->next; // selects the first node
root->next = tmp->next; //points root to the node after tmp 
delete tmp; //removes the node from memory

t = root->next; //traverse to the 1st node
cout << "The value of the first node is " << t->key << endl;
Now, you may say "why not just point the link to the node after it? It can't be accessed, so by all means, isn't it gone?" Not neccisarily. Sure, you may not be able to access it directly from the list, but it still exists in memory. Therefore, it's a good idea to delete it before moving on. I think java has "automatic garbage removal" for nodes that are removed, though I am unsure.

Well, that concludes this tutorial. Keep in mind that the above code snippets are the most simple way to make it easy to understand. A more efficient way would be to create functions that add and remove nodes, so the whole process could take only about 5 or 6 lines of code to create a huge list of nodes and remove any node at any point. Play around with nodes and and loops, and keep in mind that things you can do with arrays can sort of be done with linked lists, and sorts for arrays can be converted to linked lists easily. Good luck!
carrja99 is offline   Reply With Quote
Reply

Bookmarks

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve Nodes from Sorted Linked Lists Banan Standard C, C++ 1 07-12-2004 07:56 AM
New Tutorial: Python Lists Overview sde Code Newbie News 3 03-24-2004 09:54 AM


All times are GMT -8. The time now is 01:50 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting