鱼C论坛

 找回密码
 立即注册
查看: 2697|回复: 1

c语言链表

[复制链接]
发表于 2020-2-19 17:41:56 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{
        int ornum;
        double score;
        struct student *next;
};
#define LEN sizeof(struct student)
struct student *create();
void print(struct student *head);
struct student *del(struct student *p,int n);
int n=0;
void main()
{
        int num;
        struct student *stu , *p;
        stu = create();
        p = stu ;
        print(p);
        printf("please input the order number you want to delete:");
        scanf("%d",&num);
        print(del(stu , num));
        printf("\n\n");
        system("pause");
}
struct student *create()
{
        struct student *p1,*p2,*head;
        p1 = p2 = (struct student *)malloc(LEN);
        printf("请输入第%d个学生的学号:",n+1);
        scanf("%d", &p1->ornum);
        printf("请输入第%d个学生的成绩:",n+1);
        scanf("%lf",&p1->score);
        while(p1->ornum)
        {
                if(!n)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                n++;
                p1 = p2 = (struct student *)malloc(LEN);
                printf("请输入第%d个学生的学号:",n+1);
                scanf("%d", &p1->ornum);
                printf("请输入第%d个学生的成绩:",n+1);
                scanf("%lf",&p1->score);
        }
        p2->next = NULL;
        return head;
}
struct student *del(struct student *head,int n)
{
        struct student *p1,*p2;
        if(!head)
        {
                printf("该链表为空表\n");
                goto end;
        }
       
        p1 = head ;
        while(p1->ornum != n&&p1->next !=NULL)
        {
                p2 = p1;
                p1 = p1->next ;
        }
        if(p1->ornum ==n)
        {
                if(p1 == head)
                {
                        head = p1->next ;
                }
                else
                {
                        p2->next = p1->next ;
                }
                printf("\ndelete No.%d succeed!\n");
                n--;
        }
        else
        {
                printf("找不到!");
        }
end:
        return head;
}
void print(struct student *head)
{
        struct student *p;
        printf("there are %d student in total!\n",n);
        p = head;
        if(head)
        {
                do
                {
                        printf("学生的学号为%d,成绩为%lf\n",p->ornum ,p->score );
                        p=p->next ;
                }while(p);
        }
}求助大家,这段代码怎么也不对,print函数只能打印出第一个学生的信息
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-19 22:58:20 | 显示全部楼层
本帖最后由 jackz007 于 2020-2-19 23:11 编辑
  1.                 p2 = p1;
  2.                 n++;
  3.                 p1 = p2 = (struct student *)malloc(LEN);  // 问题语句,前面的 p1、p2 在这里通通都丢掉了,也就是说,本节点会丧失与上一个节点的联系,所有节点都会成为孤立节点
复制代码

        下面是我修改的代码版本,谨供楼主参考
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>

  4. typedef struct student
  5. {
  6.         int ornum             ;
  7.         double score          ;
  8.         struct student * next ;
  9. } NODE , * PNODE              ;

  10. int n = 0                     ;

  11. PNODE create()
  12. {
  13.         PNODE p1 , p2 , head                                                    ;
  14.         int ornum                                                               ;
  15.         double score                                                            ;
  16.         char s[256]                                                             ;
  17.         for(head = NULL ;; n ++ , p1 = p2) {
  18.                 printf("请输入第%d个学生的学号:" , n + 1)                       ;
  19.                 fflush(stdin)                                                   ;
  20.                 gets(s)                                                         ;
  21.                 if(strlen(s) > 0 && sscanf(s , "%d" , & ornum) == 1 && ornum > 0) {
  22.                         printf("请输入第%d个学生的成绩:" , n + 1)               ;
  23.                         scanf("%lf" , & score)                                  ;
  24.                         if((p2 = (PNODE) malloc(sizeof(NODE))) != NULL) {
  25.                                 p2 -> ornum = ornum                             ;
  26.                                 p2 -> score = score                             ;
  27.                                 p2 -> next  = NULL                              ;
  28.                                 if(! n) head = p2                               ;
  29.                                 else p1 -> next = p2                            ;
  30.                         } else {
  31.                                 while(head) {
  32.                                         p1 = head                               ;
  33.                                         head = p1 -> next                       ;
  34.                                         free(p1)                                ;
  35.                                 }
  36.                                 fprintf(stderr , "Error : malloc() falure !\n") ;
  37.                                 break                                           ;
  38.                         }
  39.                 } else {
  40.                         break                                                   ;
  41.                 }
  42.         }
  43.         return head                                                     ;
  44. }

  45. void print(PNODE p)
  46. {
  47.         if(n > 0) {
  48.                 printf("链表中一共有 %d 个节点.\n" , n)                                                    ;
  49.                 for(; p ; p = p -> next) printf("学生的学号为%d , 成绩为%lf\n" , p -> ornum  , p -> score) ;
  50.         } else {
  51.                 printf("链表为空。\n")                                                                     ;
  52.         }
  53. }

  54. PNODE del(PNODE p , int ornum)
  55. {
  56.         PNODE pd , head = p                                             ;
  57.         bool f = false                                                  ;
  58.         if(n > 0) {
  59.                 if(head -> ornum == ornum) {
  60.                         pd = head                                       ;
  61.                         head = head -> next                             ;
  62.                         f = true                                        ;        
  63.                 } else {
  64.                         for (; ! f && p -> next ; p = p -> next) {
  65.                                 if(p -> next -> ornum == ornum) {
  66.                                         pd = p -> next                  ;
  67.                                         p -> next = p -> next -> next   ;
  68.                                         f = true                        ;
  69.                                 }
  70.                         }
  71.                 }
  72.                 if(f) {
  73.                         free(pd)                                        ;
  74.                         n --                                            ;
  75.                         printf("节点已经删除。\n")                      ;
  76.                 } else {
  77.                         printf("节点未找到。\n")                        ;
  78.                 }
  79.         } else {
  80.                 printf("链表为空。\n")                                  ;
  81.         }
  82.         return head                                                     ;
  83. }

  84. main(void)
  85. {
  86.         int num                                                         ;
  87.         PNODE stu , p                                                   ;
  88.         stu = create()                                                  ;
  89.         p = stu                                                         ;
  90.         print(p)                                                        ;
  91.         printf("请输入需要删除学生的学号:")                             ;
  92.         scanf("%d" , & num)                                             ;
  93.         print(del(stu , num))                                           ;
  94.         printf("\n\n")                                                  ;
  95.         system("pause")                                                 ;
  96. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 07:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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