鱼C论坛

 找回密码
 立即注册
查看: 3401|回复: 3

[已解决]删除动态链表

[复制链接]
发表于 2015-12-19 18:13:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我的问题是,这个程序中有一个运行不了。就是要删除的序号不是链表中有的,然后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;
                }
        }
}
最佳答案
2016-1-8 23:28:50
本帖最后由 麦田管理中心 于 2016-1-10 18:48 编辑
  1. /*上次编辑有些错误(溢出),这次已经更改*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #define LEN sizeof(struct student)
  6. struct student
  7. {
  8.         int num;
  9.         float score;
  10.         struct student *next;
  11. };

  12. int n;//全局变量来记录链表里的个数

  13. void main()
  14. {
  15.         struct student *pt, *p;
  16.         int num;

  17.         struct student *creat(void); //创建一个动态链表
  18.         void print(struct student *head);//打印链表
  19.         struct student *deletes(struct student *head, int num);//删除链表

  20.         pt = creat();
  21.         p = pt;
  22.         print(pt);

  23.         printf("要删除的序号\n");
  24.         scanf("%d", &num);

  25.         print(deletes(p, num));
  26. }

  27. struct student* creat(void) //定义结构体类型,返回指针值,无形参的函数
  28. {
  29.         struct student *p1, *p2, *head;
  30.         p1 = p2 = (struct student*)malloc(LEN); //开辟一个新的结点

  31.         printf("Please enter the num :");
  32.         scanf("%d", &p1->num);
  33.         printf("Please enter the score :");
  34.         scanf("%f", &p1->score);

  35.         head = NULL;
  36.         n = 0;

  37.         while (p1->num)//学号为0代表结束,所以要删除最后的节点
  38.         {
  39.                 n++;
  40.                 if (1 == n)
  41.                 {
  42.                         head = p1;
  43.                 }
  44.                 else
  45.                 {
  46.                         p2 = (struct student*)malloc(LEN);
  47.                         printf("Please enter the num :");
  48.                         scanf("%d", &p2->num);
  49.                         printf("Please enter the score :");
  50.                         scanf("%f", &p2->score);
  51.                         p1->next = p2;
  52.                         p1 = p1->next;//值得注意!!!!!
  53.                 }
  54.         }
  55.         p2->next = NULL;
  56.         p1 = head;
  57.         for (; p1->next != p2; p1 = p1->next);
  58.         free(p2);
  59.         p1->next = NULL;
  60.         return head;
  61. }

  62. struct student* deletes(struct student *head, int num)
  63. {
  64.         struct student *p1, *p2;


  65.         if (head == NULL)
  66.         {
  67.                 printf("\nThis list is null!!\n");
  68.                 goto END;
  69.         }
  70.         p1 = head;
  71.         for (; p1 != NULL&&p1->num != num; p1 = p1->next);//值得注意!!!!!
  72.         if (p1 == NULL)
  73.                 printf("未找到!\n");
  74.         else
  75.         {
  76.                 p2 = p1;//值得注意!!!!!
  77.                 for (p1 = head; p1->next != p2; p1 = p1->next);//值得注意!!!!!
  78.                 p1->next = p2->next;//值得注意!!!!!
  79.                 free(p2);
  80.         }
  81. END:
  82.         return head;
  83. }

  84. void print(struct student *head) //打印链表
  85. {
  86.         struct student *p;
  87.         printf("\nThere are %d records!\n\n", n-1);
  88.         p = head;
  89.         if (head != NULL)
  90.         {
  91.                 while (p != NULL)
  92.                 {
  93.                         printf("学号为%d的成绩是: %f\n", p->num, p->score);
  94.                         p = p->next;
  95.                 }
  96.         }
  97. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-25 15:36:00 | 显示全部楼层
  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. } 给你参考下  发现两处错误 自己参考一下吧   你删除那个函数没有设置p2初值为head 还有创建链表那是p1>next=NULL;最后那个指向空
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-1-3 18:01:38 | 显示全部楼层
删除函数    while (num != p1 -> num && p1 ->next != NULL)
        {
                p2 = p1;                                 
                p1 = p1 -> next;//最后一次执行p1是空地址;

        }

修改这样就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-8 23:28:50 | 显示全部楼层    本楼为最佳答案   
本帖最后由 麦田管理中心 于 2016-1-10 18:48 编辑
  1. /*上次编辑有些错误(溢出),这次已经更改*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #define LEN sizeof(struct student)
  6. struct student
  7. {
  8.         int num;
  9.         float score;
  10.         struct student *next;
  11. };

  12. int n;//全局变量来记录链表里的个数

  13. void main()
  14. {
  15.         struct student *pt, *p;
  16.         int num;

  17.         struct student *creat(void); //创建一个动态链表
  18.         void print(struct student *head);//打印链表
  19.         struct student *deletes(struct student *head, int num);//删除链表

  20.         pt = creat();
  21.         p = pt;
  22.         print(pt);

  23.         printf("要删除的序号\n");
  24.         scanf("%d", &num);

  25.         print(deletes(p, num));
  26. }

  27. struct student* creat(void) //定义结构体类型,返回指针值,无形参的函数
  28. {
  29.         struct student *p1, *p2, *head;
  30.         p1 = p2 = (struct student*)malloc(LEN); //开辟一个新的结点

  31.         printf("Please enter the num :");
  32.         scanf("%d", &p1->num);
  33.         printf("Please enter the score :");
  34.         scanf("%f", &p1->score);

  35.         head = NULL;
  36.         n = 0;

  37.         while (p1->num)//学号为0代表结束,所以要删除最后的节点
  38.         {
  39.                 n++;
  40.                 if (1 == n)
  41.                 {
  42.                         head = p1;
  43.                 }
  44.                 else
  45.                 {
  46.                         p2 = (struct student*)malloc(LEN);
  47.                         printf("Please enter the num :");
  48.                         scanf("%d", &p2->num);
  49.                         printf("Please enter the score :");
  50.                         scanf("%f", &p2->score);
  51.                         p1->next = p2;
  52.                         p1 = p1->next;//值得注意!!!!!
  53.                 }
  54.         }
  55.         p2->next = NULL;
  56.         p1 = head;
  57.         for (; p1->next != p2; p1 = p1->next);
  58.         free(p2);
  59.         p1->next = NULL;
  60.         return head;
  61. }

  62. struct student* deletes(struct student *head, int num)
  63. {
  64.         struct student *p1, *p2;


  65.         if (head == NULL)
  66.         {
  67.                 printf("\nThis list is null!!\n");
  68.                 goto END;
  69.         }
  70.         p1 = head;
  71.         for (; p1 != NULL&&p1->num != num; p1 = p1->next);//值得注意!!!!!
  72.         if (p1 == NULL)
  73.                 printf("未找到!\n");
  74.         else
  75.         {
  76.                 p2 = p1;//值得注意!!!!!
  77.                 for (p1 = head; p1->next != p2; p1 = p1->next);//值得注意!!!!!
  78.                 p1->next = p2->next;//值得注意!!!!!
  79.                 free(p2);
  80.         }
  81. END:
  82.         return head;
  83. }

  84. void print(struct student *head) //打印链表
  85. {
  86.         struct student *p;
  87.         printf("\nThere are %d records!\n\n", n-1);
  88.         p = head;
  89.         if (head != NULL)
  90.         {
  91.                 while (p != NULL)
  92.                 {
  93.                         printf("学号为%d的成绩是: %f\n", p->num, p->score);
  94.                         p = p->next;
  95.                 }
  96.         }
  97. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 00:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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