Thread: simple linked lists am i doing anything wrong

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    simple linked lists am i doing anything wrong

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node { 
           int age;
           struct node *next;
           };
    
    struct node *add() {
           
           struct node *second = NULL;
           struct node *third = NULL;
    
           second = malloc(sizeof(struct node));
           third = malloc(sizeof(struct node));
           
           second->age = 20;
           second->next = third;
           third->age = 30;
           third->next = NULL;
    
           return second;
           
           }
    
    int count(struct node *head){
        
        struct node *current = head;
        int count=0;
        
        while(current!=NULL){
                             count++;
                             current = current->next;
                             }
                             return count;
                             }
        
    struct node* addz(){
         struct node *head;
         head = add();
         struct node *first;
         first = malloc(sizeof(struct node));
         
         first->age = 10;
         first->next = head;
         head = first;
         return head;
         }
         
         
    
    int show(struct node *head){
        struct node *current = head;
        while(current!=NULL){
                             printf("age: %d\n",current->age);
                             current = current->next;
                             }
                             }
           
    int main(void){
        struct node *head;
        head=addz();
        show(head);
        printf("%d\n",count(head));
        getchar();
        return 0;
    }
    my friend says that im doing something wrong, i dont seem to find anything wrong in my code

    i create 2 nodes second and third

    then i use a function to create the first node that will point to the second node, the second to the third and the head will point to the first

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you should have a single 'add' function that you call three times.

    struct node*add ( struct node*old_head, int value );

    Functionally, you get the right answer, but it's hardly code you could re-use elsewhere.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Salem View Post
    Well you should have a single 'add' function that you call three times.

    struct node*add ( struct node*old_head, int value );

    Functionally, you get the right answer, but it's hardly code you could re-use elsewhere.
    thanks as i see in my code i also forgot to free the allocated memory

    but im having trouble with it

    should i just use free(head) at the end?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, you need a free function, much like your show function, to traverse the list.

    But beware of the trap of simply deleting each node then moving on.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by Salem View Post
    No, you need a free function, much like your show function, to traverse the list.

    But beware of the trap of simply deleting each node then moving on.
    thanks I understand it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Freeing Linked Lists
    By mrpickle in forum C Programming
    Replies: 4
    Last Post: 01-21-2004, 07:57 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM