Thread: Linked List confusion

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Linked List confusion

    Code:
    /**
    AUTHOR: Kevin Layfield
    FILE: hw3.c
    ASSIGNMENT: Homework 2
    DATE: 9/14/2006
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include "hw3.h"
    //initialization of a list.  program crashes here.  I don't know why.
    void init_list(list_t *list){
         list->head=NULL;
         list->count=0;
         list->index=0;
         }
    //add a new node to list
    void add(list_t *list, int i, void *item)
    {
         //node to be added
         node_t *newnode;
         //increment count
         list->count++;
         //assign the third argument as data for the node
         newnode->data=item;
         //if the list is empty, create a head
         if(list->count==0)
         {
              list->head = malloc(sizeof(node_t));
              list->head->data = newnode->data;
              list->head->next = NULL;
              
         }
         //else, traverse the list until desired position is reached
         else
         {
             list->traverse = list->head;
             while(list->index<i){
                            list->traverse=list->traverse->next;
                            list->index++;
                            }
             item = malloc(sizeof(node_t));
             //assign the new node's next value = to traverse's next value
             newnode->next = list->traverse->next;  
             //essentially place the newnode in the position ahead of traverse                     
             list->traverse->next = newnode;
             //reset index
             list->index=0;
         }
                   
    }
    
    void *get(list_t *list, int i)
    {
             void *returned;        
             list->traverse = list->head;
             //traverse list until you reach desired position
             while(list->index<i){
                            list->traverse=list->traverse->next;
                            list->index++;
                            }
             list->index=0;
             //assign the data value to a new variable
             returned = list->traverse->data;
             //return new variable
             return returned;
    }
    
    int size(list_t *list)
    {
         //return the value that gets incremented each time a new node is added
         return list->count;
    }
    the .h file.....
    Code:
    typedef struct{
     void *info;
     void *next;
    }node_t;
    
    
    typedef struct{
      node_t  *front;
      int size;
    }ll_t;
    
    void init_ll(ll_t *l);
    
    void add(ll_t *list, int i, void *item);
    void *get(ll_t *list, int i);
    int size(ll_t *list);
    first off, i never used a linked list in C, only in Java and since its built into Util, it was a lot easier. Im trying to make a driver class in which i can put in any numbers i want, but i cant quite figure out how to do that, dont worry about the code above, i know its not all right, but i wanna fix it myself, just i have no idea where to go on the driver class. I know im gonna need a loop to go to the next node and keep gonig till the list is at its end. Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    BTW, there are no classes in C.

    So you want to create a main() function that will test-drive your linked list? Something like this?
    Code:
    int main(void) {
        int x, i, c;
    
        for(x = 0; x < 10; x ++) {
            printf("Enter a number: ");
            while(scanf("%d", &i) != 1) {
                while((c = getchar()) != '\n' && c != EOF);
                printf("Invalid number, try again: ");
            }
            add_number(x);
        }
    
        for(x = 0; x < 10; x ++) {
            i = remove_last_number();
            printf("%d\n", i);
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Your header file conflicts with the code file on several points

    - typedef name of list structure
    - wierd count variable
    - wierd index variable
    - really wierd transverse variable

    If you're gonna fix those, then okay! Good things to watch out while you're doing so I s'pose.

  4. #4
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by airj56
    Code:
    void init_list(list_t *list){
         list->head=NULL;
          list->count=0;
         list->index=0;
         }
    //add a new node to list
    void add(list_t *list, int i, void *item)
    {
         //node to be added
         node_t *newnode;
         //increment count
          list->count++;
         //assign the third argument as data for the node
         newnode->data=item;
         //if the list is empty, create a head
          if(list->count==0)
          {
              list->head = malloc(sizeof(node_t));
              list->head->data = newnode->data;
              list->head->next = NULL;
              
         }
         //else, traverse the list until desired position is reached
         else
    the malloc() will never be executed as count is being bumped to early.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no point in that malloc anyway, when all you have to do is assign "list->head = newnode", and be done with it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM