Thread: Using pointers and structures

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    2

    Using pointers and structures

    Hi everyone!

    I hav been using C for a while and want to learn more and how to use pointers and structures but I need some help.

    I have a structure like this:
    Code:
    struct Items
    {
    int* integerValue
    struct Items* Next
    };
    in main I have this:
    Code:
    struct Items* MyNewList;
    
    MyNewList = AddNewValue(9,NULL);
    MyNewList = AddNewValue(93,MyNewList);
    How do I add the new values in the function AddNewValue?
    I'm out of ideas.

    All help highly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    What you are trying to build is a Singly Linked list. If you're asking this question, I strongly encourage you to read the Wikipedia article.

    Singly linked lists are not "the most important data structure everrrrh" but they are a very effective data structure for teaching students the basics of pointers and dynamic memory in C. For this reason, it is essential that you figure this stuff out - it's the most basic form of everything else you'll do with C and data structures.

    In order to add new nodes to a list, you will need to do two things:
    • create a new node (probably using malloc)
    • insert the node in the list.


    Creating the node is the simplest and most direct thing. You probably know how to do this already. I encourage you to write a function that creates a node, initializes it with values from the parameters, and returns the result.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,376
    The simplest way is the following. This prepends new items, so printing the list will yield the data in the reverse order that it was added.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct Item {
        int data;
        struct Item *next;
    } Item; // typedef allows us to say Item instead of struct Item
     
    Item *listNewItem(int data, Item *next) {
        Item *item = malloc(sizeof *item);
        if (!item) { perror("listNewItem"); exit(EXIT_FAILURE); }
        item->data = data;
        item->next = next;
        return item;
    }
     
    Item *listAdd(Item *list, int data) {
        return listNewItem(data, list);
    }
     
    Item *listClear(Item *list) {
        for (Item *next = NULL; list; list = next) {
            next = list->next;
            free(list);
        }
        return NULL;
    }
     
    int main() {
        Item *list = NULL; // init list pointer to NULL
     
        for (int i = 0; i < 10; ++i)
            list = listAdd(list, i);
     
        for (Item *item = list; item; item = item->next)
            printf("%d ", item->data);
        printf("\n");
     
        list = listClear(list);
        return 0;
    }
    If you want to add the data to the end of the list, one possibility is to search for the end each time you add an item.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct Item {
        int data;
        struct Item *next;
    } Item;
     
    Item *listNewItem(int data, Item *next) {
        Item *item = malloc(sizeof *item);
        if (!item) { perror("listNewItem"); exit(EXIT_FAILURE); }
        item->data = data;
        item->next = next;
        return item;
    }
     
    Item *listAdd(Item *list, int data) {
        Item *item = listNewItem(data, NULL);
     
        // If the list is empty, return item
        if (!list) return item;
     
        // Otherwise find the end of the list and append item there
        Item *p = list;
        for ( ; p->next; p = p->next) ;
        p->next = item;
     
        return list;
    }
     
    Item *listClear(Item *list) {
        for (Item *next = NULL; list; list = next) {
            next = list->next;
            free(list);
        }
        return NULL;
    }
     
    int main() {
        Item *list = NULL; // init list pointer to NULL
     
        for (int i = 0; i < 10; ++i)
            list = listAdd(list, i);
     
        for (Item *item = list; item; item = item->next)
            printf("%d ", item->data);
        printf("\n");
     
        list = listClear(list);
        return 0;
    }
    However, searching for the end of the list every time you add an item is not very efficient. Instead, you can keep track of the "tail" of the list.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct Item {
        int data;
        struct Item *next;
    } Item;
     
    typedef struct List {
        Item *head;
        Item *tail;
    } List;
     
    Item *listNewItem(int data, Item *next) {
        Item *item = malloc(sizeof *item);
        if (!item) { perror("listNewItem"); exit(EXIT_FAILURE); }
        item->data = data;
        item->next = next;
        return item;
    }
     
    void listAdd(List *list, int data) {
        Item *item = listNewItem(data, NULL);
        if (list->tail)
            list->tail->next = item;
        else
            list->head = item;
        list->tail = item;
    }
     
    void listClear(List *list) {
        for (Item *item = list->head, *next = NULL; item; item = next) {
            next = item->next;
            free(item);
        }
        list->head = list->tail = NULL;
    }
     
    int main() {
        List list = {NULL, NULL}; // init head and tail pointers to NULL
     
        for (int i = 0; i < 10; ++i)
            listAdd(&list, i);
     
        for (Item *item = list.head; item; item = item->next)
            printf("%d ", item->data);
        printf("\n");
    
        listClear(&list); 
        return 0;
    }
    Last edited by john.c; 11-28-2021 at 01:02 PM. Reason: added listClear functions
    A long time ago being crazy meant something. - Charles Manson

  4. #4
    Registered User
    Join Date
    Nov 2021
    Posts
    2
    Than you aghast and john.c for your answers. They are highly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and pointers to structures
    By Fiskker in forum C Programming
    Replies: 40
    Last Post: 06-15-2015, 09:01 PM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread