I'm writing a simple program to figure out how Linked List works, but for now I'm stucking. I don't know which line cause the error or I had a mistake in thinking. I run my program by my hand and it works fine. But when run, it gets error at all choices, didn't work or runtime error. Please help me figure things out!
I think the code explains itself. Thanks everyone who give my thread a look.
BTW, I'm using Pelles C 8 IDE on Windows 10.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct Node {
    int data;
    struct Node *next;
};

void insl(struct Node *, const int);
void del(struct Node *);
int clear(struct Node *);
void print(struct Node *);

int main(void) {
    struct Node *head = NULL;
    for ( ; ; ) {
        _clrscr();
        puts("LINK LIST DEMO");
        printf("%s", "Current list: ");
        print(head);
        puts("\n(0) Free list and exit.");
        puts("(1) Insert a node at last.");
        puts("(2) Delete last node.");
        puts("(3) Free list.");
        printf("%s", "Enter your choice: ");
        int choice = 0;
        scanf("%d", &choice);
        switch (choice) {
            case 0:
                if (clear(head))
                    puts("\nDone!");
                else
                    puts("\nError!");
                scanf("%d", &choice);
                return 0;
            case 1:
                printf("%s", "\nEnter data: ");
                int x;
                scanf("%d", &x);
                insl(head, x);
                break;
            case 2:
                del(head);
                break;
            case 3:
                clear(head);
                break;
        }
    }
}

void insl(struct Node *head, const int x) {
    struct Node *new = malloc(sizeof *new);
    new->data = x;
    new->next = NULL;
    if (!head) {
        head = new;
        return;
    }
    struct Node *temp = head;
    while (!temp->next)
        temp = temp->next;
    temp->next = new;
}
void del(struct Node *head) {
    if (!head)
        return;
    struct Node *temp = head;
    while (!temp->next)
        temp = temp->next;
    free(temp);
}
int clear(struct Node *head) {
    struct Node *temp = head, *save;
    if (!temp) {
        save = temp->next;
        free(temp);
        temp = save;
    }
    head = temp;
    if (!head)
        return 1;
    return 0;
}
void print(struct Node *head) {
    if (!head) {
        puts("empty");
        return;
    }
    struct Node *temp = head;
    while (!temp) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    putchar(10);
}

Recommended Answers

All 3 Replies

This, malloc(sizeof *new); should be malloc(sizeof(Node)). You are only allocating a chunk of memory the size of a pointer - not the size of the Node structure. Fix that and we will go from there.

This, malloc(sizeof *new); should be malloc(sizeof(Node)).

It can be, but doesn't need to be. That call to malloc is correct as it stands, and actually best practice. new is a pointer to Node. The operand to sizeof is the dereferenced new, which ends up being evaluated as Node rather than Node*. Because sizeof only evaluates the end type of the expression and not the expression itself, this dereference of an unintialized pointer is also quite safe.

% C# program that uses LinkedList %

C# program that uses LinkedList

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    //
    // Create a new linked list object instance.
    //
    LinkedList<string> linked = new LinkedList<string>();
    //
    // Use AddLast method to add elements at the end.
    // Use AddFirst method to add element at the start.
    //
    linked.AddLast("cat");
    linked.AddLast("dog");
    linked.AddLast("man");
    linked.AddFirst("first");
    //
    // Loop through the linked list with the foreach-loop.
    //
    foreach (var item in linked)
    {
        Console.WriteLine(item);
    }
    }
}

Output

first
cat
dog
man
commented: C is not C# -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.