linked list

This is a discussion on linked list within the C Programming forums, part of the General Programming Boards category; hi .i am wirtting a linked list and i tried testing my add method.I observed that my values are not ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    linked list

    hi .i am wirtting a linked list and i tried testing my add method.I observed that my values are not beeing written in the right place.for e.g my test value 5 should be in node 1 node 1 and my test value 6 should be in node 2
    Code:
    /*  * File:   main.c
     * Author: onwugbej
     *
     * Created on November 23, 2011, 10:24 PM
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /*
     * 
     */
    struct node{
        int key;
        struct node *next;
        
    };
    
    
    struct listset{
        int length;  
        struct node *head;
    };
    struct listset * litset_new(){
        struct listset *p = malloc(sizeof(*p));
        struct node *dummy;  // creating a dummy for my node to point to
       
        p->head=malloc(sizeof(*dummy)); //creating space for my node head in the list
        dummy=malloc(sizeof(*dummy));
       
        p->head->next=dummy;
        p->head->key=0;
        dummy->next=dummy;  //pointing to itself
        
        
        return p;
        
    }
    void listset_add(struct listset * t, int item){
        //struct listset *a=t;
        struct node *p= malloc(sizeof(*p));
       
        p->key=item;
        p->next = t->head;
        t->head->next=p;    //let my nlist set head point to my my new node
        //p->key=item;  //settinh my item
    }
    
    
    /* Given a linke list head pointer 
     *  this function to counts the number of nodes in a list
     */
    int Length(struct node *head) {
    
    
    struct node* current =head; 
    int count = 0;
    while (current != NULL) 
    { count++;
    current = current->next;
    }
    return count;
    }
    
    
    int main(int argc, char** argv) {
        struct listset  *s;
         s=litset_new();
         listset_add(s,5);
         listset_add(s,6);
         printf("%d\n",s->head->next->key);
          printf("%d\n",s->head->next->next->key);
    
    
        return (EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    The problem is you "listset_add" function. In fact, what you do is attaching the last node to the first (which is ok if you want to do circular linked list), and then attaching the first one to the last, which means that your linked list will only be able to contains 2 elements, since you're always overwriting the "->next" of the head. So actually when you're printing "s->head->next->next->key" you are printing the first element. What you have to do in your "listset_add" function is :
    ->iteratate to the last element (for a circular linked list you iterate until the "->next" is equal to the head).
    ->link the last element to the new node.
    ->link the new element to the head (for circular list again).

    Also, why are you using this "dummy" node in your initialization? Do you really want to insert this useless node each time you create a list set? You could set the head to NULL and check in you're "list_add" function if the head is NULL, and create it if yes.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Tibo-88 View Post
    The problem is you "listset_add" function. In fact, what you do is attaching the last node to the first (which is ok if you want to do circular linked list), and then attaching the first one to the last, which means that your linked list will only be able to contains 2 elements, since you're always overwriting the "->next" of the head. So actually when you're printing "s->head->next->next->key" you are printing the first element. What you have to do in your "listset_add" function is :
    ->iteratate to the last element (for a circular linked list you iterate until the "->next" is equal to the head).
    ->link the last element to the new node.
    ->link the new element to the head (for circular list again).

    Also, why are you using this "dummy" node in your initialization? Do you really want to insert this useless node each time you create a list set? You could set the head to NULL and check in you're "list_add" function if the head is NULL, and create it if yes.
    hi i have changed my initiliasation of my listset struct to

    Code:
    struct listset * litset_new(){    struct listset *p = malloc(sizeof(*p)); 
        p->head=malloc(sizeof(p->head)); //creating space for my node head in the list
        p->head->next=NULL;
        p->head->key=0;
        
        
        return p;
         
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21