Thread: Singly Linked List (different operations / a lot of errors returned)

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    1

    Singly Linked List (different operations / a lot of errors returned)

    Hey guys!
    So I've only recently started learning more about linked lists and have written code for operations like deleting the entire list, or inserting an element to the list, etc. for practice.

    My objective is to implement the bits of code to make an executable program that does all of the operations and prints the progress as it goes.

    I tried to run the C-file as it is now, but received a lot of errors. My assumption is that I would need a main() function, along with the libraries, but I have no clue what the main function would do, or if it's possible to do nested functions in C.

    I hope someone can help me convert this code into an executable (working) program and explain how things work when doing so! Thank you a lot.

    (The code I have so far is written below.)


    Code:
    //Declaration for a linked list
    
    struct le {
        int value;            
        struct le * next;    
    };
    typedef struct le LinkedList;
    
    //Pointer to the first element (the head):
    typedef LinkedList * list; 
    // 'list' is a node
    
    //To insert a list element as a new element 
    void insert(int v, list *l){
        LinkedList * new;
        new = malloc(sizeof(LinkedList));
        new -> value = v;
        new -> net = *l;
        *l = new;
    }
    
    //Delete the first element of the list - the head 
    int delete_head(list *l){
        list old = *l;
        *l = old -> next;
        free(old);
        return 0;
    }
    
    //Delete the entire list 
    void delete_all(list l){
        list next;
        while(l != NULL){
            next = l -> next;
            free(1);
            l = next;
        }
    }
    
    //Return the length of the list 
    int length(list l){
        while(l != NULL){
            count++;
            l = l -> next;
        }
        return count;
    }
    
    //To return all elements in a list 
    void print_list(list l){
        if (l == NULL) printf("EMPTY");
        else
        while(l != NULL){
            printf("%d", 1->value);
            l = l -> next;
        }
    }
    Last edited by rikkyd; 05-23-2019 at 10:37 AM.

  2. #2
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    72
    Quote Originally Posted by rikkyd View Post
    I tried to run the C-file as it is now, but received a lot of errors.
    What kind of errors? You should be more specific.

    [quote]My assumption is that I would need a main() function, along with the libraries, but I have no clue what the main function would do, or if it's possible to do nested functions in C.[quote]

    You need to go through the tutorial and understand, at a basic level, how C syntax works. Every C program requires a main function.

    Start with a hello world:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello, world\n");
        return 0;
    }
    I hope someone can help me convert this code into an executable (working) program and explain how things work when doing so! Thank you a lot.
    In C, you need to compile your program into an executable by running: gcc program.c -o program.

    If you plan to post more questions in the future, consider reading this.

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. Need help with singly linked list
    By saldar05 in forum C Programming
    Replies: 6
    Last Post: 03-08-2013, 12:30 AM
  3. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  4. Singly Linked List
    By devarishi in forum C Programming
    Replies: 4
    Last Post: 12-24-2008, 12:00 AM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM

Tags for this Thread