C 带链表的多文件程序,删除节点

标签 c linked-list structure

这个程序应该从链表中删除节点(在我的例子中是从 10, 20, 30,..., 100 ),其数据与您输入的数字相等。它无法正常工作。它应该显示完整列表,但它会在 10 后停止,提供选择数字并中断。

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

#include "LinkedList.h"
#include "LinkedList.c"

int main() {
    int x;
    int change=0;
    LINK head, curr, currB, tail;
    head = NULL;
    curr = NULL;
    currB = NULL;
    tail = NULL;
    create_list(&head, &curr, &tail);
    print_list(head, curr);
    ask_for_value(&x);
    delete_node(head, curr, currB, tail, &change, x);
    if (0 == change)
        printf("\nValue %d is not on the list\n", x);
    print_list(head, curr);
    return 0;
}
<小时/>
#include <stdio.h>
#include <stdlib.h>

#include "LinkedList.h"

void create_list(LINK *head, LINK *curr, LINK *tail) {
    int i;
    for(i=10; i<100; i+=10) {
        (*curr)=(LINK)malloc(sizeof(ELEMENT));
        (*curr)->data = i;
        if(i==10) {
            (*curr)->next = NULL;
            (*tail)=(*curr);
            (*head)=(*curr);
        }
        else {
            (*curr)->next = NULL;
            (*tail)->next = (*curr);
            (*tail)=(*curr);
        }
    }
}

void delete_node(LINK head, LINK curr, LINK currB, LINK tail, int *change, int x) {
    int i;
    if(head->data==x) {
        curr=head;
        head=curr->next;
        free(curr);
        (*change)=1;
        exit(0);
    }
    if(tail->data==x){
        curr=head;
        while(curr->next!=tail)
        curr=curr->next;
        free(tail);
        tail=curr;
        tail->next=NULL;
        (*change)=1;
        exit(0);
    }
    curr=currB=head;
    while(curr->data!=x || curr->next!=NULL){
        currB=curr;
        curr=curr->next;
    }
    if(curr->data!=x)
        exit(0);
    if(currB==curr){
        head=curr->next;
        free(curr);
        (*change)=1;
        exit(0);
    }
    currB->next=curr->next;
    free(curr);
    (*change)=1;
}

void print_list(LINK head, LINK curr)
{
    curr=head;
    if (curr!=NULL){
        printf("%d >> ",curr->data);
        curr = curr->next;
    }
}

void ask_for_value(int *x) {
    printf("Enter value which should be removed from the list\n");
    scanf("%d", &x);
}
<小时/>
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_

struct linkedList{
    int data;
    struct linkedList *next;
    };
typedef struct linkedList ELEMENT;
typedef struct linkedList *LINK;

void create_list(LINK *head, LINK *curr, LINK *tail);
void delete_node(LINK head, LINK curr, LINK currB, LINK tail, int *listChange, int x);
void print_list(LINK head, LINK curr);
void ask_for_value(int *x);
#endif

第二个文件是 LinkedList.c,第三个文件是 LinkedList.h

<小时/>

编辑:我更改了delete_node,适用于除 10 之外的任何值。

void delete_node(LINK head, LINK curr, LINK currB, LINK tail, int x) {
    curr=currB=head;
    while(curr->data!=x && curr->next!=NULL) {
        currB=curr;
        curr=curr->next;
    }
    if(head->data==x) {
        head=curr->next;
        free(currB);
    }
    else if(tail->data==x) {
        tail=currB;
        tail->next=NULL;
        free(curr);
    }
    else if(curr->data!=x) {
        printf("Element with given value could not be found!\n");
    }
    else{
        currB->next=curr->next;
        free(curr);
    }
}

最佳答案

在您的 ask_for_value() 函数中,

scanf("%d", &x);

应该是

scanf("%d", x);

此外,请勿使用 #include .c 文件。它们是要编译的。

接下来,您的 delete_node() 函数不正确。它会通过遇到 exit(0) 来终止程序的执行,这可能不是我们想要的。您可以使用 return 0 来代替。

关于C 带链表的多文件程序,删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512407/

相关文章:

c++ - 类似C语言的数组大小分配

c - 指定编译器可以使用的函数的 simd 级别

java - 在链表数组中找到最长的链表

c - 使用结构单独计算每个员工工资的程序

c - C 结构中两个不同位置的位字段

我可以指向数组的特定地址吗?

c - 无论我做什么,While 循环都不会中断

objective-c - 比较浮点值有多危险?

java - 链接列表 - 无法弄清楚为什么此删除最后一个功能不起作用?

java - 我们可以用 Java 实现 XOR 链表吗?