Thread: Linked List problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Linked List problem

    Dear Programmers,

    i have a problem with linked lists. I have an character array. This will look like this:

    array[0] = 1
    array[1] = 4
    array[2] = 8

    Now, i will make an structure:
    Code:
    typedef struct node node;
       struct node{
            char value;
            node* p;
    };
    Now, i would like to make a linked list, which looks like

    node1->value= '1'
    node2->value= '4'
    node3->value= 8



    Now, i have it like this:
    Code:
    	node *temp;
    	node *temp1=NULL;
    	node *temp2=NULL;
    	temp = (node*)malloc(sizeof(node));
    	temp->value = p[0];
    	temp->next = temp1;
    
    	temp1 = (node*)malloc(sizeof(node));
    	temp1->value = p[1];
    	temp1->next = temp2;
    	
    	temp2 = (node*)malloc(sizeof(node));
    	temp2->value = p[2];
    	temp2->next = NULL;
    But it will look better if i can make a loop of it (e.g. because if i have a larger array for example). But how can i do that?

    Best Peter

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    While building the list, you can maintain a pointer to the last node. This makes appending nodes easier:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node node;
    
    struct node {
        char value;
        node *next;
    };
    
    int main(void)
    {
        char a[] = {'1','4','8'};
        node *head = NULL;
        node *end;
        size_t i;
    
        for (i = 0; i < sizeof a / sizeof *a; i++) {
            node *new = malloc(sizeof *new);
    
            if (new == NULL)
                break;
    
            new->value = a[i];
            new->next = NULL;
    
            if (head == NULL)
                head = end = new;
            else {
                end->next = new;
                end = end->next;
            }
        }
    
        while (head != NULL) {
            node *save = head->next;
            printf("%c\n", head->value);
            free(head);
            head = save;
        }
    
        return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Thanks for your answer. I have 5 compile errors, but i suppose it's because i have a c++ compiler...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have 5 compile errors, but i suppose it's because i have a c++ compiler...
    Very likely seeing as how new is keyword in C++ and C++ requires the result of malloc to be cast from void*. If you post in the C forum, expect the assumption that you want examples in C.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by Prelude View Post
    >I have 5 compile errors, but i suppose it's because i have a c++ compiler...
    Very likely seeing as how new is keyword in C++ and C++ requires the result of malloc to be cast from void*. If you post in the C forum, expect the assumption that you want examples in C.
    oh yes, you're right. I make a c program. But i coudn;t find a good compiler, that's why i use visual c++ (do you otherwise know a good and simple one? )

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by peterderijp View Post
    oh yes, you're right. I make a c program. But i coudn;t find a good compiler, that's why i use visual c++ (do you otherwise know a good and simple one? )
    smorgasbordet - Pelles C

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Visual C++ understands C. Try giving your file a *.c extension, or specifying the target language as C instead of C++ in your project properties. Any decent C++ compiler will be able to compile at least C90.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Linked list ()
    By caraie in forum C Programming
    Replies: 7
    Last Post: 08-30-2010, 01:56 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM