Thread: How to print the values of a part of a linked list

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    How to print the values of a part of a linked list

    I'm learning how to use linked lists and can't figure out how to access the elements of the structure I am currently pointing to with my *next pointer.

    Also, "next" isn't a keyword for these types of operations, is it?

    If anyone has an example of a linked list I could see I would be grateful.

    This is my code thus far (not very much):

    Code:
    struct list
    {
    struct list *ptrnext;
    char letter;
    int number;
    };
    We've been working on this for the last few hours, and none of our approaches are working, Any tips/tutorials please?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, when you build your linked list, one of the things you have to do (ok, the only thing you have to do) is make sure that the nextptr field of object A points to object B (and so on down the line).

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Yeah, basically you have a head pointer and you continually add items. So when you add them you go to the last node in you list and assign to the ptrnext the address of the new node to be added while the new node just has NULL in it's field.

    Try this file if you want. I dont have comments but I think it 's pretty easy to figure out
    Last edited by Phoenix_Rebirth; 02-05-2009 at 08:09 AM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    here's an example of a linked list.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        char letter;
        struct node *next;
    };
    
    typedef struct node node;
    
    node *strtolist(char *s);
    
    int main()
    {
        node *list = strtolist("How to print the values of a part of a linked list");
        node *temp, *next;
        for (temp = list; temp; temp = next)
        {
            next = temp->next;
            putchar(temp->letter);
            free(temp);
        }
        putchar('\n');
        return EXIT_SUCCESS;
    }
    
    node *strtolist(char *s)
    {
        node *list = NULL;
        node *temp;
        for (; *s; s++)
        {
            if (!list)
            {
                list = malloc(sizeof(node));
                list->letter = *s;
                list->next = NULL;
            }
            else
            {
                for (temp = list; temp->next; temp = temp->next) ;
                temp->next = malloc(sizeof(node));
                temp->next->letter = *s;
                temp->next->next = NULL;
            }
        }
        return list;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Linked List print out
    By axon in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2003, 07:15 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM