Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,556 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,603 people online right now. Registration is fast and FREE... Join Now!




Linked lists and functions issue

 
Reply to this topicStart new topic

Linked lists and functions issue

Nidza
25 Nov, 2007 - 03:51 AM
Post #1

New D.I.C Head
*

Joined: 18 Mar, 2007
Posts: 7



Thanked: 1 times
My Contributions
CODE

#include <stdio.h>

struct entry
{int value;
struct entry *next;
};

int main()
{
struct entry n1,n2,n3;
struct entry *list_pointer= &n1;
int i=2;

n1.value=100;
n1.next=&n2;

n2.value=200;
n2.next=&n3;

n3.value=300;
n3.next=(struct entry *) 0;


while( list_pointer !=(struct entry*) 0 ) {

printf("%d\n",list_pointer->value);
list_pointer=list_pointer->next;
}
getchar();
return 0;
}


As you can see code is very simple and understandable.Now I was wondering...If i want to make a void function that recieves a number of list members from a main() and links them inside, how ? I just can't link them inside the function, not sure how... The problem comes when i want to write the index of the member (n1.next=&n2). Lets say the integer "i" determines where we are at the moment.It should be n(i).next=&n(i+1), and it just doesn't go that way.

Thanks in advance
Nidza
User is offlineProfile CardPM
+Quote Post

Bench
RE: Linked Lists And Functions Issue
25 Nov, 2007 - 04:36 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Linked lists don't have "indexes", because a linked list structure is not random access. You can find out the position of an element in your linked list by iterating through it with a linear search, but what you need to remember is the memory address of your node which you wish to perform an operation on (eg, insert/delete) - any other value is completely worthless.

Handling a linked list is a real test of how well you understand memory management and pointers. You cannot do what you're asking without using a pointer variable to remember your "current" node.



Typically, creating a linked list involves dynamically created memory. Its almost unheard of to create nodes as named variables. Here's a brief example of creation/insertion to a linked list.

CODE
#include <stdlib.h>

struct node
{
    int data;
    struct node* next;
};

/*
* push_back() - Take a pointer to the list head's placeholder.
*  and the data to be inserted to the list
*
* Create a new node with malloc -
*  If the node is created successfully then store data in the
*  node.  The new node becomes the head of the list.
*
*/
void push_back(struct node** head, int i)
{
    struct node* newnode = malloc( sizeof( struct node ) );
    if( newnode != NULL )
    {
        newnode->next = *head;
        newnode->data = i
        *head = newnode;
    }
}

int main(void)
{
    /* Create a pointer to the list's head.
     *  The list starts out empty, so the head starts as NULL.
     *
     * This is essentially a placeholder to remember where
     *  the list head resides in memory.
     */
    struct node* list_head = NULL;


    /* Add 3 elements to the list, with data 1, 2 and 3 */
    push_back( &list_head, 1 );
    push_back( &list_head, 2 );
    push_back( &list_head, 3 );

    /* TODO: Clean the list up using free() */
}


This post has been edited by Bench: 25 Nov, 2007 - 04:48 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month