删除动态链表
我的问题是,这个程序中有一个运行不了。就是要删除的序号不是链表中有的,然后windows就会停止运行#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;//全局变量来记录链表里的个数
void main()
{
struct student *pt, *p;
int num;
struct student *creat(void); //创建一个动态链表
void print(struct student *head);//打印链表
struct student *deletes(struct student *head, int num);//删除链表
pt = creat();
p = pt;
print(pt);
printf("要删除的序号\n");
scanf("%d", &num);
print (deletes (p , num));
}
struct student *creat(void) //定义结构体类型,返回指针值,无形参的函数
{
struct student *p1, *p2, *head;
p1 = p2 = (struct student*)malloc(LEN); //开辟一个新的结点
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while (p1 -> num)
{
n++;
if(1 == n) //注意要倒过来写
{
head = p1;
}
else
{
p2 -> next = p1; //p2 和 next 均为指针,类比二维数组,只取一次*,还是表示的地址
}
p2 = p1;
p1 = (struct student*)malloc(LEN);
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
}
p2 -> next = NULL;
return head;
}
struct student *deletes(struct student *head, int num)
{
struct student *p1, *p2;
if (head == NULL)
{
printf("\nThis list is null!!\n");
goto END;
}
p1 = head;
while (num != p1 -> num && p1 -> num != NULL)
{
p2 = p1;
p1 = p1 -> next;
}
if (num == p1 -> num)
{
if (head == p1)//删除的是头指针
{
head = p1 -> next;
}
else
{
p2 -> next = p1 -> next;
}
printf("\nDelete No: %d succeed!\n", num);
n = n-1;
}
else
{
printf("%d not been found!\n", num);//这里有问题,在删除的序号不是链表中的序号是,无法运行
}
END:
return head;
}
void print(struct student *head) //打印链表
{
struct student *p;
printf("\nThere are %d records!\n\n", n);
p = head;
if (head != NULL)
{
while(p != NULL)
{
printf("学号为%d的成绩是: %f\n", p -> num, p -> score);
p = p -> next;
}
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct yg
{
char name;
int gz;
struct yg *next;
}s;
int size=sizeof(struct yg);
intmain()
{
struct yg *list,*p1,*p2;
void printList(struct yg *list );
struct yg *del_list(struct yg *list,int salary);
char name;
int i,gz,n=0,salary;
list=NULL;
printf("请输入职工姓名和基本工资: \n");
scanf("%s%d",name,&gz);
while(gz!=0)
{
n++;
p1=(struct yg*)malloc(size);
strcpy(p1->name,name);p1->gz=gz;p1->next=NULL;
if(n==1)
list=p1;
else
p2->next=p1;
p2=p1;
scanf("%s%d",name,&gz);
}
printf("请输入要删除工资节点\n");
scanf("%d",&salary);
for(i=1;i<=n;i++)
list=del_list(list,salary);
printf("删除节点后的员工信息表\n");
printList(list);
return 0;
}
struct yg *del_list(struct yg *list, int salary)
{
struct yg *p1=list,*p2=list;
while((p1->gz!=salary) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p1->gz==salary)
{ if(list==p1)
list=p1->next;
else
p2->next=p1->next;
free(p1);
}
return list;
}
void printList(structyg *list ){
struct yg *p = list;
while (p != NULL){
printf("%s%6d\n", p->name,p->gz);
p = p->next;
}
printf("\n");
} 给你参考下发现两处错误 自己参考一下吧 你删除那个函数没有设置p2初值为head 还有创建链表那是p1>next=NULL;最后那个指向空 删除函数 while (num != p1 -> num && p1 ->next != NULL)
{
p2 = p1;
p1 = p1 -> next;//最后一次执行p1是空地址;
}
修改这样就可以了 本帖最后由 麦田管理中心 于 2016-1-10 18:48 编辑
/*上次编辑有些错误(溢出),这次已经更改*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;//全局变量来记录链表里的个数
void main()
{
struct student *pt, *p;
int num;
struct student *creat(void); //创建一个动态链表
void print(struct student *head);//打印链表
struct student *deletes(struct student *head, int num);//删除链表
pt = creat();
p = pt;
print(pt);
printf("要删除的序号\n");
scanf("%d", &num);
print(deletes(p, num));
}
struct student* creat(void) //定义结构体类型,返回指针值,无形参的函数
{
struct student *p1, *p2, *head;
p1 = p2 = (struct student*)malloc(LEN); //开辟一个新的结点
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while (p1->num)//学号为0代表结束,所以要删除最后的节点
{
n++;
if (1 == n)
{
head = p1;
}
else
{
p2 = (struct student*)malloc(LEN);
printf("Please enter the num :");
scanf("%d", &p2->num);
printf("Please enter the score :");
scanf("%f", &p2->score);
p1->next = p2;
p1 = p1->next;//值得注意!!!!!
}
}
p2->next = NULL;
p1 = head;
for (; p1->next != p2; p1 = p1->next);
free(p2);
p1->next = NULL;
return head;
}
struct student* deletes(struct student *head, int num)
{
struct student *p1, *p2;
if (head == NULL)
{
printf("\nThis list is null!!\n");
goto END;
}
p1 = head;
for (; p1 != NULL&&p1->num != num; p1 = p1->next);//值得注意!!!!!
if (p1 == NULL)
printf("未找到!\n");
else
{
p2 = p1;//值得注意!!!!!
for (p1 = head; p1->next != p2; p1 = p1->next);//值得注意!!!!!
p1->next = p2->next;//值得注意!!!!!
free(p2);
}
END:
return head;
}
void print(struct student *head) //打印链表
{
struct student *p;
printf("\nThere are %d records!\n\n", n-1);
p = head;
if (head != NULL)
{
while (p != NULL)
{
printf("学号为%d的成绩是: %f\n", p->num, p->score);
p = p->next;
}
}
}
页:
[1]