鱼C论坛

 找回密码
 立即注册
查看: 2526|回复: 4

删除链表

[复制链接]
发表于 2015-12-19 20:35:03 | 显示全部楼层 |阅读模式
2鱼币
我的问题是,这个程序中有一个运行不了。就是要删除的序号不是链表中有的,然后windows就会停止运行
#include
#include
#include
#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;
}
}
}

最佳答案

查看完整内容

代码写错了! while (num != p1 -> num && p1 -> num != NULL) { p2 = p1; p1 = p1 -> next; } 红色部分 应该是 p1->next 大概是你的 笔误吧! 这种问题,一般 自己 把程序跑起来,然后 局部断点调试,很容易发现的! 你的 goto 有些多了! 一般来说 正常情况下 很少使用goto的,正式的代码里面我 基本没使用过 goto,不方便 日后的阅读,和其他人的阅读,只是个人 建议,楼主怎么习惯怎么使!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-19 20:35:04 | 显示全部楼层
代码写错了!
while (num != p1 -> num && p1 -> num != NULL)
{
p2 = p1;
p1 = p1 -> next;
}
红色部分 应该是 p1->next  大概是你的 笔误吧!
这种问题,一般 自己 把程序跑起来,然后 局部断点调试,很容易发现的!
你的  goto 有些多了! 一般来说  正常情况下 很少使用goto的,正式的代码里面我
基本没使用过 goto,不方便 日后的阅读,和其他人的阅读,只是个人 建议,楼主怎么习惯怎么使!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-20 13:08:32 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>

  4. struct yg
  5. {
  6.   char name[20];
  7.   int gz;
  8.   struct yg *next;

  9. }s;
  10. int size=sizeof(struct yg);


  11. int  main()
  12. {
  13. struct yg *list,*p1,*p2;
  14. void printList(struct yg *list );
  15. struct yg *del_list(struct yg *list,int salary);
  16. char name[20];
  17. int i,gz,n=0,salary;
  18. list=NULL;
  19. printf("请输入职工姓名和基本工资: \n");
  20.         scanf("%s%d",name,&gz);
  21. while(gz!=0)
  22. {
  23.   n++;
  24.   p1=(struct yg*)malloc(size);
  25.   strcpy(p1->name,name);p1->gz=gz;p1->next=NULL;
  26.   if(n==1)
  27.           list=p1;
  28.   else
  29.           p2->next=p1;

  30.   p2=p1;
  31. scanf("%s%d",name,&gz);
  32. }
  33. printf("请输入要删除工资节点\n");
  34.    scanf("%d",&salary);
  35. for(i=1;i<=n;i++)
  36. list=del_list(list,salary);
  37. printf("删除节点后的员工信息表\n");
  38.   printList(list);
  39.   
  40.     return 0;
  41. }

  42. struct yg *del_list(struct yg *list, int salary)
  43. {
  44.      struct yg *p1=list,*p2=list;
  45.      while((p1->gz!=salary) && (p1->next!=NULL))
  46.          {
  47.           p2=p1;
  48.           p1=p1->next;
  49.          
  50.          }
  51.          
  52.     if(p1->gz==salary)
  53.         {   if(list==p1)
  54.                 list=p1->next;
  55.             else
  56.                         p2->next=p1->next;
  57.            free(p1);
  58.         }
  59.          
  60.    
  61.          
  62.         return list;
  63.   
  64. }
  65. void printList(struct  yg *list ){
  66.     struct yg *p = list;
  67.     while (p != NULL){  
  68.         printf("%s%6d\n", p->name,p->gz);
  69.         p = p->next;
  70.     }
  71.     printf("\n");
  72. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-12-24 10:56:22 | 显示全部楼层
谢谢,我会注意的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-1-11 16:08:22 | 显示全部楼层
看到你的提问和大神的回答,我也学到了很多,谢谢大家。 还有 这不是数据结构的问题么?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 20:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表