Thread: Please help me with Linklist problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Smile Please help me with Linklist problem

    I write a link list program of three functions 1. "ref.h" 2. "main.c" 3. "create.c"
    "create.c" accepts parameters from main.c and add an new item to the link list
    I use pointer to pass the parameters to create.c, but I found my "front" variable in "create.c" is always NULL even though I have assigned new to it in the first item to be added.
    Could anyone help me to find why? I have stuck in this question for hours =.=
    Thank you very much!!

    ps. I run my program in Linux environment

    In ref.h
    Code:
    #include <stdio.h>
    struct data{
            int postcode;
            struct data *next;
    };
    In main.c
    Code:
    #include "ref.h"
    void main()
    {
    
            struct data *front=NULL;
            struct data *end=NULL;
    
            int a;
            while(1)
            {
                    printf("Add item to link list:\n");
                    printf("Enter 1 to add link list\n");
                    scanf("%d",&a);
                    switch(a)
                    {
                            case 1:{                        
                                        create(front,end);
                                        break;
                                    }               
                            default:
                                    exit(0);
                    }
            }
    }
    In create.c
    Code:
    #include "ref.h"
    
    void create(struct data *front,struct data *end)
    {
            struct data *new=(struct data *)malloc(sizeof(struct data));
            printf("Please enter the postcode:\n");
            int i;
            scanf("%d",&i);
            new->postcode=i;
            new->next=NULL;
            if(front==NULL)               each time the create function only runs in if 
            {
                    front=new;
                    end=new;
                    printf("First create!\n");
            }
            else
            {
                    end->next=new;
                    end=new;
                    printf("In create!\n");
            }
    
    }
    Last edited by ericwu; 05-13-2009 at 06:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in link list
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 07-16-2008, 01:54 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM