Thread: Why my singly linked-list program doesn't run?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    12

    Why my singly linked-list program doesn't run?

    I'm trying to write a program that user can create linked-list by entering the number of lists they want to create but when i ran this code it doesn't work.

    Can someone please help me?

    Code:
    #include <stdio.h>
    #include <malloc.h>>
    #include <stdlib.h>
    
    
    void createList();
    struct node *cList;
    
    
    int main()
    {
        void createList();
        cList = NULL;
    
    
        struct node
        {
        int studentId, age, choice, cList;
        char studentName[30];
        struct node *next;
        };
    
    
        int choice;
    
    
        printf("Please select an option: ");
        printf("1. Create\n");
    /*    printf("2. Display\n");
        printf("3. Insert\n");
        printf("4. Remove\n");
        printf("5. Search");
        printf("6. Exist");*/
        scanf("%d", &choice);
    
    
        switch(choice)
        {
        case 1: createList();
    
    
        }
    
    
    }
    
    
    void createList()
    {
        char list;
    
    
        if(cList == NULL)
        {
            struct *newNode, *current;
            newNode = (struct node *)malloc(sizeof(struct node));
    
    
            printf("Enter the number of list: \n");
            scanf("%d", &newNode -> data);
            newNode ->next = NULL;
    
    
            if(cList == NULL)
            {
                cList = newNode;
                current = newNode;
            }
            else
            {
                current ->next = newNode;
                current = newNode;
            }
    
    
            printf("Would you like to create another list?\n");
            list = getch();
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you're confusing the notion of a linked list with that of the nodes of the linked list. What you are trying to do is to have one linked list with multiple nodes, and so you ask the user how many students should be in the list.

    Note that <malloc.h> is pre-standard; just #include <stdlib.h> for malloc and free.

    I suggest that you work along these lines:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct student
    {
        int id, age, choice;
        char name[30];
    };
    
    struct node
    {
        struct student student_data;
        struct node *next;
    };
    
    struct node *prependNode(struct node *head);
    /*void removeNextNode(struct node *node);*/
    
    struct node *createList(void);
    
    int main(void)
    {
        struct node *head = NULL;
        int choice;
    
        printf("Please select an option: ");
        printf("1. Create\n");
    /*  printf("2. Display\n");
        printf("3. Insert\n");
        printf("4. Remove\n");
        printf("5. Search");
        printf("6. Exist");*/
        scanf("%d", &choice);
    
        switch(choice)
        {
        case 1:
            head = createList();
            break;
        }
    
        return 0;
    }
    
    /* ... */
    The idea is that you have a function like prependNode whose sole purpose is to prepend a node to the linked list given the head of the linked list, and return a pointer to the new node (i.e., the new head node of the linked list). This function could work even when the head node is a null pointer, i.e., it can be used to implement createList. createList itself is not concerned with the nitty gritty of getting the linked list functionality to work, no, it is concerned with requesting input from the user and using that to create the linked list with the help of prependNode. It finally returns the head node of the linked list so that it can be updated in the main function.

    Doing things this way, you have a function for each of your options, but (except maybe for display) they never directly deal with the linked list, instead calling a function that doesn't do interactive I/O whose sole purpose is to deal with the linked list through the head node or some other node.

    Oh, and remember to free what you malloc: this could mean having a destroyLinkedList function that takes the head node pointer and repeatedly calls free on the nodes of the linked list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    12
    Thank you for helped, this code simplify the program and makes it easier to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Singly Linked List !!
    By thebenman in forum C Programming
    Replies: 3
    Last Post: 10-18-2014, 10:59 PM
  2. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  3. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM

Tags for this Thread