#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initList(){
Node* list = (Node*)malloc(sizeof(Node));
list -> data=0;
list -> next=NULL;
return list;
}
void headInsert(Node* list,int data){
Node* node=(Node*)malloc(sizeof(Node));
node->data=data;
node->next=list->next;
list->next=node;
list->data++;
}
void tailInsert(Node* list,int data){
Node* head=list;
Node* node=(Node*)malloc(sizeof(Node));
node->data=data;
node->next=NULL;
list=list->next;
while(list->next){
list=list->next;
}
list->next=node;
head->data++;
}
void delete(Node* list,int data){
Node* pre = list;
Node* current = list->next;
while(current){
if(current->data == data){
pre -> next = current -> next;
free(current);
//break; 删除的数没有相同值时加break
}
pre = current;
current = current -> next;
}
list -> data--;
}
void loop(Node* list){
list=list->next;
while(list){
printf("%d",list->data);
list=list->next;
}
printf("\n");
}
int main()
{
Node* list = initList();
headInsert(list,1);
headInsert(list,2);
headInsert(list,3);
tailInsert(list,4);
loop(list);
delete(list,2);
loop(list);
return 0;
}
delete 是C关键字,删除函数名需做修改,void Delete(Node* list, int data) 函数里也做了修改,改动处见注释处,修改完善如下,供参考:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node;
Node* initList()
{
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;
list->next = NULL;
return list;
}
void headInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->next = node;
list->data++;
}
void tailInsert(Node* list, int data)
{
Node* head = list;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
//list = list->next; 修改
while (head->next) { //while (list->next) 修改
head = head->next; //list = list->next; 修改
}
head->next = node; //list->next = node; 修改
list->data++; //head->data++; 修改
}
void Delete(Node* list, int data) //void delete(Node* list, int data) 修改
{
Node* pre = list;
Node* current = list->next;
while (current) {
if (current->data == data) {
pre->next = current->next;
free(current);
list->data--;
current = pre->next; // 修改
//break; 删除的数没有相同值时加break
}
else { // 修改
pre = current;
current = current->next;
} // 修改
}
// list->data--; 修改
}
void loop(Node* list)
{
list = list->next;
while (list) {
printf("%d", list->data);
list = list->next;
}
printf("\n");
}
int main()
{
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
tailInsert(list, 4);
loop(list);
Delete(list, 2); //delete(list, 2); 修改
loop(list);
return 0;
}
【以下回答由 GPT 生成】
问题:为什么删除后没有输出了怎么改应该
问题分析: 根据代码分析,问题出现在delete函数中,当删除的数没有相同值时没有加上break语句,导致遍历链表后继续执行。这会导致在调用loop函数时,打印的是已经删除了某个元素后的链表,但是链表的长度没有更新,所以不会输出任何内容。
解决方案: 在delete函数中,当删除的数没有对应的相同值时,应该加上break语句跳出循环。
修改后的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* initList() {
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;
list->next = NULL;
return list;
}
void headInsert(Node* list, int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->next = node;
list->data++;
}
void delete(Node* list, int data) {
Node* pre = list;
Node* current = list->next;
while (current) {
if (current->data == data) {
pre->next = current->next;
free(current);
break; // 添加break语句
}
pre = current;
current = current->next;
}
list->data--;
}
void loop(Node* list) {
Node* current = list->next;
while (current) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* list = initList();
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
delete(list, 2);
loop(list);
return 0;
}
可以看到,我们在delete函数中加入了一个break语句,当找到要删除的元素时,就跳出循环,不再继续遍历链表。这样,在调用loop函数时,就能正确地输出链表的内容了。
希望对你有帮助!
【相关推荐】
这段代码中存在一个小错误。在 tailInsert(Node* list,int data) 函数中,最后一行的 list=list->next; 会导致链表中的最后一个节点丢失,因为它将 list 指向了下一个节点。正确的应该是 list=head; ,这样就可以保留对最后一个节点的引用。