Thread: Link List with the Tail pointer.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Link List with the Tail pointer.

    We are trying to make the linklist work with a tail and a head pointer. I think there is an error @ line 26.

    " tail->next = new;"

    We are trying to change whatever the tail pointer is pointing next to the new node. It's probably a syntax error.

    Thanks for your help.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    struct node {
        int id;
        struct node* next;
    };
    /* if it's the first item, pass in HEAD
     * else pass in the TAIL
     */
    void addNode(struct node** head, struct node** tail, int id)
    {
        struct node* new = malloc(sizeof(struct node* ));
        if(head == NULL)
        {
            new->id = id;
            new->next = NULL;
            *head = new;
            *tail = new;
        }
        else
        {
            new->id = id;
            new->next = NULL;
            tail->next = new;
            *tail = new;
        }
    
    
    
    
    }
    void printList(struct node* head)
    {
        
        while(head != NULL)
        {
            printf("I M INHERE!!! \n");
            printf("ID %d", head->id);
            head = head->next;
    
    
        }
    
    
    }
    
    
    
    
    
    
    int main()
    {
        struct node* head;
        struct node* tail;
        head = malloc(sizeof(struct node*));
        tail = malloc(sizeof(struct node*));
        head = NULL;
        tail = head;
    
    
        int i;
        for(i = 0; i < 5; i++)
        {
            addNode(&head,&tail, i);
    
    
        }
        printList(head);
    
    
    
    
        return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Delete lines 59 and 60 from main:
    Code:
        head = malloc(sizeof(struct node*));
        tail = malloc(sizeof(struct node*));
    You shouldn't allocate memory and then throw it away like that.

    Line 27 (not 26) is wrong because you can't use -> on a double-pointer. First dereference it to get to a single pointer, i.e. (*tail) and then use the arrow.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(head == NULL)
    Should be
    if(*head == NULL)

    > tail->next = new;
    Should be
    (*tail)->next = new;

    And the two malloc calls in main are redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Thanks sooooo much!
    It makes sense..
    I forgot about the
    head in ___ if(head == NULL);
    should be a head pointer.. for that I need to dereference to get the head pointer...


    Thanks agains for the help.
    Now I am going to implement delete fcn or dequeue()
    and an insert sort fcn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list tail
    By cable in forum C Programming
    Replies: 3
    Last Post: 10-08-2011, 12:48 PM
  2. Linked List - Add to Tail
    By beatbox32 in forum C Programming
    Replies: 9
    Last Post: 08-08-2011, 03:43 PM
  3. doubly linked list tail pointer in struct
    By bazzano in forum C Programming
    Replies: 15
    Last Post: 06-11-2007, 12:31 PM
  4. Another List Q - Seg fault on head-tail list
    By JimpsEd in forum C Programming
    Replies: 11
    Last Post: 05-10-2006, 12:53 AM
  5. tail pointer - linked list
    By Space_Cowboy in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2002, 03:05 AM