Thread: C programming Linked List

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    1

    C programming Linked List

    Hi,
    I am new in learning linked list.
    I try to make a program using a function
    int sorted_list_insert(struct tstr_node **head , struct tstr_node *pstr_node) with a double pointer to the head.
    It is important for me to keep the prototype as it is well. Function return 1 in case of succes and o otherwise. Thank you.

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    struct tstr_node
    {
        char dataString[10][30];
        struct tstr_node *next;
    };
    
    
    struct tstr_node *head;
    
    
    int sorted_list_insert(head , pstr_node);
    
    
    int main()
    {
        int nod;
        struct tstr_node *pstr_node;
        nod = sorted_list_insert(*head , pstr_node);
    }
    
    
    int sorted_list_insert(struct tstr_node **head , struct tstr_node *pstr_node)
    {
        char dataString[30];
        pstr_node = (struct tstr_node *)malloc(sizeof(struct tstr_node));
        if(pstr_node == NULL)
        {
            printf("\n Unable to allocate memory! \n");
            exit(EXIT_FAILURE);
        }
        else
        {
            printf("\n Enter a string for the first node : ");
            gets(dataString);
    
    
            strcpy(pstr_node->dataString, dataString);
            pstr_node->next = *head;
            *head = pstr_node;
        }
        return 1;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    This should be declared within the main function:
    Code:
    struct tstr_node *head;
    This is not correct:
    Code:
    int sorted_list_insert(head , pstr_node);
    You should include the parameter types in the function prototype, so it should have been:
    Code:
    int sorted_list_insert(struct tstr_node **head , struct tstr_node *pstr_node);
    In the main function, this is not correct:
    Code:
    nod = sorted_list_insert(*head , pstr_node);
    The reason is that head is a pointer to struct tstr_node, but the head parameter expects a pointer to a pointer to struct tstr_node. Therefore, you should write:
    Code:
    nod = sorted_list_insert(&head , pstr_node);
    Looking at what you're trying to do in sorted_list_insert, I'd say that this is wrong:
    Code:
    struct tstr_node
    {
        char dataString[10][30];
        struct tstr_node *next;
    };
    You probably want dataString to be, as the name say, a string rather than an array of strings. So it should have been:
    Code:
    struct tstr_node
    {
        char dataString[30];
        struct tstr_node *next;
    };
    Speaking of which, I'd say that you should change sorted_list_insert to look like this:
    Code:
    int sorted_list_insert(struct tstr_node **head , const char *dataString)
    The reason is that this insert function should focus on inserting the data as a node into the linked list. What you're doing right now is giving it two tasks:
    • Read the data from the user.
    • Insert the data as a node.

    It is better to split each of these tasks into its own function. Furthermore, you aren't actually making use of the pstr_node parameter as a parameter: you're using it as an ordinary local variable.

    Oh, and one more thing: do not use gets as it is inherently vulnerable to buffer overflow. Try fgets as an alternative, though remember that fgets stores the trailing newline from reading the line into the string if there is space for it.
    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
    Sep 2020
    Posts
    79

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a linked list in programming ?
    By abhi143 in forum C Programming
    Replies: 11
    Last Post: 10-21-2017, 11:41 AM
  2. Replies: 1
    Last Post: 01-08-2014, 12:13 AM
  3. Replies: 2
    Last Post: 09-15-2013, 01:16 PM
  4. C programming - Doubly linked list
    By pghpens91 in forum C Programming
    Replies: 5
    Last Post: 07-12-2012, 07:56 PM
  5. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM

Tags for this Thread