利用c语言里的链表删除大于k的节点,以0为结尾,输入k和链表,输出删除后的链表。(只会c语言,还没有学习c++)
不知道为什么代码运行错误,我是根据书上的链表删除的公式写的删除函数的,但是完全没发删除,感觉很苦恼
例子:
输入:2000
2001 2002 1999 1998 2021 1997 0
输出:1999 1998 1997
```c
#include
#include
struct cell
{
int year;
struct cell*next;
};
struct cell*build(void)
{
struct cell*head,*p,*tmp;
int n;
head=p=tmp=NULL;
scanf("%d",&n);
if(n==0)
{
return head;
}
p=(struct cell*)malloc(sizeof(struct cell));
p->year=n;
p->next =NULL;
head=p;
scanf("%d",&n);
while(n!=0)
{
tmp=(struct cell*)malloc(sizeof(struct cell));
tmp->year=n;
tmp->next=NULL;
p->next=tmp;
p=p->next;
scanf("%d",&n);
}
return head;
};
struct cell*delect(struct cell*head,int k)
{
struct cell*p,*p0,*p1;
p=head;
p0=NULL;
if(head==NULL){return head;}
while(p->year>k)
{p=p->next;free(p0->next);p0->next=p;
}
head=p;
while(p!=NULL)
{
if(p->year>k){p=p->next;free(p0->next);p0->next=p;}
p0=p;p=p->next;
}
return head;
};
void print(struct cell*head)
{
struct cell*p;
p=head;
if(head==NULL){printf("NULL");return;}
while(p!=NULL)
{
printf("%d",p->year);
if(p->next!=NULL){printf(" ");}
}
}
void release(struct cell*head)
{
struct cell*p0,*p;
p=head;
while(p!=NULL)
{
p0=p;p=p->next;free(p0);
}
}
int main()
{
struct cell*head;int k;
scanf("%d",&k);
head=build();
head=delect(head,k);
print(head);
release(head);
return 0;
}
您的代码中存在一些问题:
在 delect() 函数中,当链表头的值大于 k 时,您直接让 head 指向当前节点 p,但是却没有释放原来的头节点空间,导致内存泄漏。可以先将当前节点 p 存到 p1 中,再释放 p0。
在 print() 函数中,每输出一个节点的值后都应该将 p 指向下一个节点,否则会造成死循环输出。
在输出节点的值时,应该加上换行符,否则多个值会输出在同一行,不易阅读。
下面是修改后的代码,您可以参考一下:
#include <stdio.h>
#include <stdlib.h>
struct cell {
int year;
struct cell *next;
};
struct cell *build(void) {
struct cell *head, *p, *tmp;
int n;
head = p = tmp = NULL;
scanf("%d", &n);
if (n == 0) {
return head;
}
p = (struct cell*)malloc(sizeof(struct cell));
p->year = n;
p->next = NULL;
head = p;
scanf("%d", &n);
while (n != 0) {
tmp = (struct cell*)malloc(sizeof(struct cell));
tmp->year = n;
tmp->next = NULL;
p->next = tmp;
p = p->next;
scanf("%d", &n);
}
return head;
}
struct cell *delect(struct cell *head, int k) {
struct cell *p, *p0, *p1;
p = head;
p0 = NULL;
if (head == NULL) {
return head;
}
while (p->year > k) {
p1 = p;
p = p->next;
free(p1);
}
head = p;
while (p != NULL) {
if (p->year > k) {
p1 = p;
p = p->next;
free(p1);
p0->next = p;
} else {
p0 = p;
p = p->next;
}
}
return head;
}
void print(struct cell *head) {
struct cell *p;
p = head;
if (head == NULL) {
printf("NULL\n");
return;
}
while (p != NULL) {
printf("%d\n", p->year);
p = p->next;
}
}
void release(struct cell *head) {
struct cell *p0, *p;
p = head;
while (p != NULL) {
p0 = p;
p = p->next;
free(p0);
}
}
int main() {
struct cell *head;
int k;
scanf("%d", &k);
head = build();
head = delect(head, k);
print(head);
release(head);
return 0;
}
改动处见注释,供参考:
#include<stdio.h>
#include<malloc.h>
struct cell
{
int year;
struct cell* next;
};
struct cell* build(void)
{
struct cell* head, * p, * tmp;
int n;
head = p = tmp = NULL;
//scanf("%d", &n);
//if (n == 0)
//{
// return head;
//}
//p = (struct cell*)malloc(sizeof(struct cell));
//p->year = n;
//p->next = NULL;
//head = p;
//scanf("%d", &n);
while (1) //(n != 0) //修改
{
scanf("%d", &n); //修改
if (n == 0) break; //修改
tmp = (struct cell*)malloc(sizeof(struct cell));
tmp->year = n;
tmp->next = NULL;
if (head == NULL)
head = tmp;
else
p->next = tmp;
p = tmp; //p = p->next; 修改
//scanf("%d", &n); 修改
}
return head;
}//;
struct cell* delect(struct cell* head, int k)
{
struct cell* p, * p0, * p1;
p = head;
p0 = NULL;
if (head == NULL) { return head; }
//while (p->year > k) 修改
//{
// p = p->next; free(p0->next); p0->next = p;
//}
//head = p;
while (p != NULL)
{
if (p->year > k) {
if (p == head){
head = head->next;
free(p);
p = head;
}
else {
p0->next = p->next;
free(p);
p = p0;
}
}
else {
p0 = p;
p = p->next;
}
//p = p->next; free(p0->next); p0->next = p; } 修改
}
return head;
}//;
void print(struct cell* head)
{
struct cell* p;
p = head;
if (head == NULL) {
printf("NULL");
return;
}
while (p != NULL)
{
printf("%d", p->year);
if (p->next != NULL) { printf(" "); }
p = p->next; //修改
}
}
void release(struct cell* head)
{
struct cell* p0, * p;
p = head;
while (p != NULL)
{
p0 = p;
p = p->next;
free(p0);
}
}
int main()
{
struct cell* head;
int k;
scanf("%d", &k);
head = build();
head = delect(head, k);
print(head);
release(head);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: