在c ++中创建链表

时间:2021-07-30 07:21:57

I am trying to implement linked list through this code... I think the first node gets created and its takes data but pointer to the next node is not working for the second time it says segmentation fault... Please help

我试图通过这段代码实现链表...我认为第一个节点被创建并且它获取数据但是指向下一个节点的指针第二次没有工作它表示分段错误...请帮忙

#include<cstdio>
#include<cstdlib>

struct node {
    int data;
    struct node *next;
};
struct node *root;

void append(int num)
{
    struct node *conductor;

    if (root == NULL) {
        root = new node;
        root->data = num;
        root->next = NULL;
        conductor = conductor->next;
    } else {
        conductor = root;
        while (conductor != NULL)
            conductor = conductor->next;
        conductor->next = new node;
        conductor = conductor->next;
        conductor->next = NULL;
        conductor->data = num;
    }
}

void display()
{
    struct node *conductor;
    conductor = root;

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

int main()
{
    int choice, num;

    while (1) {
        printf("Enter the choice\n");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            scanf("%d", &num);
            append(num);
            break;
        case 2:
            display();
            break;
        case 3:
            exit(0);
        }
    }
    return (0);
}

1 个解决方案

#1


0  

Correct code:

#include<cstdio>
#include<cstdlib>

struct node {
    int data;
    struct node *next;
};
struct node *root;

void append(int num)
{
    struct node *conductor;

    if (root == NULL) {
        root = new node;
        root->data = num;
        root->next = NULL;
        // conductor = conductor->next; This was not needed at all.
    } else {
        conductor = root;
        while (conductor->next != NULL) // You were doing conductor != NULL
            conductor = conductor->next;
        conductor->next = new node;
        conductor = conductor->next;
        conductor->next = NULL;
        conductor->data = num;
    }
}

void display()
{
    struct node *conductor;
    conductor = root;
    if (conductor == NULL) return; // Returning if root is NULL
    while (conductor->next != NULL) {
        printf("%d->", conductor->data);
        conductor = conductor->next;
    }
    printf("%d\n", conductor->data);
}

int main()
{
    int choice, num;

    while (1) {
        printf("Enter the choice\n");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            scanf("%d", &num);
            append(num);
            break;
        case 2:
            display();
            break;
        case 3:
            exit(0);
        }
    }
    return (0);
}

#1


0  

Correct code:

#include<cstdio>
#include<cstdlib>

struct node {
    int data;
    struct node *next;
};
struct node *root;

void append(int num)
{
    struct node *conductor;

    if (root == NULL) {
        root = new node;
        root->data = num;
        root->next = NULL;
        // conductor = conductor->next; This was not needed at all.
    } else {
        conductor = root;
        while (conductor->next != NULL) // You were doing conductor != NULL
            conductor = conductor->next;
        conductor->next = new node;
        conductor = conductor->next;
        conductor->next = NULL;
        conductor->data = num;
    }
}

void display()
{
    struct node *conductor;
    conductor = root;
    if (conductor == NULL) return; // Returning if root is NULL
    while (conductor->next != NULL) {
        printf("%d->", conductor->data);
        conductor = conductor->next;
    }
    printf("%d\n", conductor->data);
}

int main()
{
    int choice, num;

    while (1) {
        printf("Enter the choice\n");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            scanf("%d", &num);
            append(num);
            break;
        case 2:
            display();
            break;
        case 3:
            exit(0);
        }
    }
    return (0);
}