0

I am a first time poster so, I don't exactly know the norms, forgive me if my formatting is bad.

I am new to C programming and recently picked up on linked lists, the below code was my crappy attempt at making one.. I was wondering if anyone would be kind enough to go through my code and explain why I am facing issues. Thanks in advance! I will try my best to answer any questions!

To explain some of my functions,

  1. createlinklist: just initializes struct linklist and adds an int value
  2. addlinkfirst: adds a node at the end
  3. removelinklast: removes a node from the end
  4. dispalylinklist: prints the contents of the linklist

Code:

#include<stdio.h>

struct linklist {

    int data;
    struct linklist* link;

};

void createlinklist (struct linklist* newlist, int data){

    newlist->data = data;
    newlist->link = NULL;

}

void addlinkfirst (struct linklist* list, int data){

    while(list->link != NULL)
        list = list->link;

    struct linklist *newlist = malloc(sizeof(struct linklist));
    newlist->link = NULL;
    list->link = &newlist;
    newlist->data = data;

}

void removelinklast (struct linklist* list){

    struct linklist* temp;

    while(list != NULL){

        temp = &list;
        list = list-> link;
    }

    temp->data = NULL;
    temp->link = NULL;

}

void displaylinklist (struct linklist* list) {
    printf("\n");

    while(list != NULL){
        printf(" %d-",list->data);
        list = list->link;
    }

    printf("\n");
}

int main () {
    int x = 1, data, x1;
    struct linklist test;

    while(x!=0){
        printf("Press 4 to create, Press 1 insert, Press 2 delete, Press 3 Display, Press 0 to Exit \n");
        scanf("%d",&x);
    //i assume you will be choosing 4 first then the other options 
        switch(x){
            case 4: printf("\nEnter data for the first element:"); scanf("%d",&data); createlinklist(&test,data); break;
            case 1: printf("\nEnter data:"); scanf("%d",&x1); addlinkfirst(&test,x1); break;
            case 2: printf("\nDeleted"); removelinklast(&test); break;
            case 3: displaylinklist(&test); break;
            case 0: break;
            default: printf("Invalid Input Try again\n");
        }
    }

    printf("\nThank you for using my linklist program;");
    return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
Cq47
  • 1
  • 2

1 Answers1

0

In main, this line:

struct linklist test;

creates a node on the stack, but doesn't initialize it. It has undefined data, and most importantly, the link member is not NULL. That's a bad place to start from.

You probably want to start with a pointer like this:

struct linklist *test = NULL;

This would represent the empty list. To add an element to it, call addlink like so: addlink(&test, 42);, with the following implementation:

void addlink(struct linklist **p_list, int data) {
    if (*p_list == NULL) {
        *p_list = makelist(data);
    }
    else {
        addlink(&(*p_list)->link, data);
    }
}

struct linklist *makelist(int data) {
    struct linklist *list = malloc(sizeof(struct linklist));
    list->link = NULL;
    list->data = data;
    return list;
}

Here, addlink is recursive, but you could just as easily write it without the reursion. In makelist, a list with a single element is created. I'll leave you the removelist function as an excercise, but it should also take a struct linklist ** argument, with the double pointers, and you'll call it like so: removelist(&test);.

Enno
  • 1,736
  • 17
  • 32