Home > Mobile >  How to access linked list node from other function?
How to access linked list node from other function?

Time:03-27

Hey im trying to make a linked list. I created linked list from other function called CreateLibrary. but then how can I access the head of the node of the linked list from other function?

int *HeadGlobal;

struct node {
    char judul[50], author[30];
    struct node *link;
};

int CreateLibrary(struct node *head) {
    struct node *head = malloc(sizeof(struct node));
    HeadGlobal = head;
}

int ViewData() {    
    char judul[50], author[50];
    struct node *head = malloc(sizeof(struct node));
    head = &HeadGlobal;
    printf("%s dan %s", head->judul, head->author);
}

I tried to make HeadGlobal Pointer and save the head pointer into this global pointer, therefore accessing it from other function,called ViewData(). but I think its not the correct solution. how to solve this?

CodePudding user response:

The name CreateLibrary implies that you should be able to create any number of libraries. You do not need any global variables to achieve that. You are also confusing the type needed to maintain a library. Your linked list should maintain a linked list of struct nodes - not ints.

Here's an example of how that may look:

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct node node; // makes it possible to use `node` instead of `struct node`
struct node {
    char judul[50], author[30];
    node *link;
};

typedef struct {
    node* head;
    node* tail;
    size_t count;
} library;        // library is the type maintaining info about one library instance

// here's the function to create one instance of a library:
library *CreateLibrary() {
    library *newlibrary = malloc(sizeof *newlibrary);
    if(newlibrary == NULL) return newlibrary;

    // initialize this new empty library:
    newlibrary->head = NULL;
    newlibrary->tail = NULL;
    newlibrary->count = 0;
    return newlibrary;
}

// a function to clean up:
void DestroyLibrary(library *lib) {
    for(node *curr = lib->head, *next; curr != NULL; curr = next) {
        next = curr->link;
        free(curr);
    }
    free(lib);
}

// a helper function to create a new node:
node *CreateNode(const char *judul, const char *author) {
    node *new_node = malloc(sizeof *new_node);
    if(new_node == NULL) return new_node; // oups - this should rarely happen

    new_node->link = NULL; 

    // copy the supplied information into the new node:
    strncpy(new_node->judul, judul, sizeof new_node->judul);
    new_node->judul[sizeof new_node->judul - 1] = '\0'; // just a precaution
    
    strncpy(new_node->author, author, sizeof new_node->author);
    new_node->author[sizeof new_node->author - 1] = '\0'; // just a precaution

    return new_node;
}

// a helper function to add a node to the library:
bool AddToLibrary(library *lib, node *new_node) {
    if(new_node == NULL) return false; // we won't add node pointers pointing at NULL 

    if(lib->head == NULL) { // this is the first node added to library
        lib->head = new_node;
    } else { // there is already at least one node in the library
        lib->tail->link = new_node;
    }
    lib->tail = new_node; // and the new node is the new tail
    lib->count  ;         // increase the number of nodes in the library

    return true;          // we successfully added the node
}

// print the information about one specific node:
void ViewNode(const node *node) {
    printf("%s dan %s\n", node->judul, node->author);
}

// print the information for all nodes in one specific library:
void ViewLibrary(const library *lib) {
    for(node *curr = lib->head; curr != NULL; curr = curr->link) {
        ViewNode(curr);
    }
}

Example usage:

int main() {
    library *lib = CreateLibrary();
    if(lib == NULL) return 1; // failure :-(

    AddToLibrary(lib, CreateNode("foo", "Hello"));
    AddToLibrary(lib, CreateNode("bar", "World"));

    ViewLibrary(lib);

    DestroyLibrary(lib);
}

Demo

  •  Tags:  
  • c
  • Related