gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:26:04 26 4
gpt4 key购买 nike

这个程序应该从链表中删除节点(在我的例子中是从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com