鱼C论坛

 找回密码
 立即注册
查看: 1280|回复: 2

[已解决]关于链表的删除问题

[复制链接]
发表于 2019-3-10 15:17:32 | 显示全部楼层 |阅读模式

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

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

x
源码附上
运行到输入删除的学生信息时,输入完程序就直接死掉了
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)//student结构的大小//

struct student *creat();//创建链表

void print(struct student *head);//打印链表//
struct student *del(struct student *head,int num);//删除链表//

struct student
{
      int num;
      int score;
      struct student *next;
};//声明结构体的类型的同时定义变量//
int n;//定义为全局变量//
void main()
{
      struct student *stu,*p;
      int num;
      stu=creat();
      p=stu;
      print(p);
      printf("\n\n");
      printf("请输入您要删除的学生信息:");
      scanf("%d",num);
      print(del(p,num));
      
      system("pause");
      
}
struct student *creat()//创建链表同时在里面写出算法//
{
      system("color 0C");
      struct student *head;
      struct student *p1,*p2;
      p1=p2=(struct student *)malloc(LEN);//分配一个结构体大小的内存块,为p1,p2创建类型为struct student *类型的,长度为len的动态内存//
      
      printf("please enter the nuim:");
      
      scanf("%d",&(*p1).num);
      
      printf("please enter the score:");
      
      scanf("%d",&(*p1).score);
      
      head=NULL;
      n=0;
      while(p1->num)
      {
            n++;
            if(1==n)
            {
                  head=p1;
            }
            else
            {
                  p2->next=p1;
            }
            p2=p1;
            p1=(struct student *)malloc(LEN);
            
            printf("please enter the num:");
            
            scanf("%d",&p1->num);
            
            printf("please enter the score:");
            
            scanf("%d",&p1->score);
      }
      p2->next=NULL;
      return (head);//返回头指针//
      
}
void print(struct student *head)
{
      struct student *p;
      printf("\nThere are %d records!\n\n",n);
      p=head;
      int i=0;
      printf("共有%d个学生\n",n);
      if(head)
      {
            do
            {
                  
                  printf("学号为%d,他的分数为%d\n",p->num,p->score);   
                  p=p->next;
            }
            while(p);
      }   
}
struct student *del(struct student *head,int num)
{
      struct student *p1,*p2;
      
      if(NULL==head)//如果头结点指向NULL,这是一个空链表//
      {
            printf("您并没有输入学生信息");
            goto END;
      }
      p1=head;
      
      while(p1->num!=num&&p1->next!=NULL)
      {
            p2=p1;
            p1=p1->next;
      }
      if(num==p1->num)
      {   
            if(head=p1)
            {
                  head=p1->next;
            }
            else
            {
                  p2->next=p1->next;
                  
            }
            n=n-1;
            printf("删除成功\n");
      }
      else
      {
            printf("没有找到该学生的信息\n");
      }
      
            printf("succee\n,现在共有%d个学生的信息",n);
END:
      printf("并没有创建学生信息");
      return head;
}
最佳答案
2019-3-10 18:01:07
     楼主的代码很乱,读起来费劲,重写了大部分代码,供参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

typedef struct student
{
      int num               ;
      int score             ;
      struct student * next ;
} node                      ;

int n                       ;

node * creat(void)
{
        int num , score                                          ;
        node * head , * p1 , * p2                                ; 

        head = NULL                                              ;
        n = 0                                                    ;
        system ("color 0C")                                      ;
        for(;;) {
                printf("  please enter the num : ")              ;
                scanf("%d" , & num)                              ;
                if (! num) break                                 ; // 输入 0 结束录入
                printf("please enter the score : ")              ; 
                scanf("%d" , & score)                            ;
                p2 = (node *) malloc(sizeof(node))               ;
                if(p2 != NULL) {
                        p2 -> num   = num                        ;
                        p2 -> score = score                      ;
                        p2 -> next  = NULL                       ;
                        if(! n) {
                                head = p2                        ;                                
                        } else {
                                p1 -> next = p2                  ;
                        }
                        p1 = p2                                  ;
                        n ++                                     ;
                } else {
                        fprintf(stderr , "malloc() failure !\n") ;
                        break                                    ;
                }
        }
        return head                                              ;
}

node * del(node * head , const int num)
{
        node * p1 , * p2                                                 ;
        bool f                                                           ;

        f = false                                                        ;
        if (head != NULL) {
                p1 = head                                                ;
                if (head -> num == num) {
                        head = head -> next                              ;
                        f = true                                         ; 
                } else {
                        p2 = p1 -> next                                  ;
                        while(p2 != NULL) { 
                                if (p2 -> num == num) {
                                        p1 -> next = p2 -> next          ;
                                        p1 = p2                          ;
                                        f = true                         ;
                                        break                            ;
                                }
                                p1 = p2                                  ;
                                p2 = p1 -> next                          ;
                        }
                }
                if(f) {
                        free(p1)                                         ;
                        n --                                             ;
                        printf("删除成功\n")                             ; 
                        printf("succee , 现在共有 %d 个学生的信息\n", n) ;
                } else {
                        printf("没有找到该学生的信息\n")                 ;
                }
        } else {
                printf("链表为空!\n")                                   ;
        }
        return head                                                      ;
}

void print(node * head)
{
        node * p                                                                   ;

        printf("\n")                                                               ;
        printf("There are %d records!\n" , n)                                      ;
        printf("共有%d个学生\n" , n)                                               ;
        p = head                                                                   ;
        if(head != NULL) {
                while(p != NULL) {
                        printf("学号为%d,他的分数为%d\n" , p -> num , p -> score) ;    
                        p = p -> next                                              ;
                }
        } else {
                printf("链表为空!\n")                                             ;        
        }
}

main(void)
{
      node * stu , * p                    ;
      int num                             ;
      stu = creat()                       ;
      p = stu                             ;
      print(p)                            ;
      printf("\n\n")                      ;
      printf("请输入您要删除的学生信息:") ;
      scanf("%d", & num)                  ; // 注意,原来的代码这里有误
      print(del(p , num))                 ;
      
      system("pause")                     ;
      
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-10 18:01:07 | 显示全部楼层    本楼为最佳答案   
     楼主的代码很乱,读起来费劲,重写了大部分代码,供参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

typedef struct student
{
      int num               ;
      int score             ;
      struct student * next ;
} node                      ;

int n                       ;

node * creat(void)
{
        int num , score                                          ;
        node * head , * p1 , * p2                                ; 

        head = NULL                                              ;
        n = 0                                                    ;
        system ("color 0C")                                      ;
        for(;;) {
                printf("  please enter the num : ")              ;
                scanf("%d" , & num)                              ;
                if (! num) break                                 ; // 输入 0 结束录入
                printf("please enter the score : ")              ; 
                scanf("%d" , & score)                            ;
                p2 = (node *) malloc(sizeof(node))               ;
                if(p2 != NULL) {
                        p2 -> num   = num                        ;
                        p2 -> score = score                      ;
                        p2 -> next  = NULL                       ;
                        if(! n) {
                                head = p2                        ;                                
                        } else {
                                p1 -> next = p2                  ;
                        }
                        p1 = p2                                  ;
                        n ++                                     ;
                } else {
                        fprintf(stderr , "malloc() failure !\n") ;
                        break                                    ;
                }
        }
        return head                                              ;
}

node * del(node * head , const int num)
{
        node * p1 , * p2                                                 ;
        bool f                                                           ;

        f = false                                                        ;
        if (head != NULL) {
                p1 = head                                                ;
                if (head -> num == num) {
                        head = head -> next                              ;
                        f = true                                         ; 
                } else {
                        p2 = p1 -> next                                  ;
                        while(p2 != NULL) { 
                                if (p2 -> num == num) {
                                        p1 -> next = p2 -> next          ;
                                        p1 = p2                          ;
                                        f = true                         ;
                                        break                            ;
                                }
                                p1 = p2                                  ;
                                p2 = p1 -> next                          ;
                        }
                }
                if(f) {
                        free(p1)                                         ;
                        n --                                             ;
                        printf("删除成功\n")                             ; 
                        printf("succee , 现在共有 %d 个学生的信息\n", n) ;
                } else {
                        printf("没有找到该学生的信息\n")                 ;
                }
        } else {
                printf("链表为空!\n")                                   ;
        }
        return head                                                      ;
}

void print(node * head)
{
        node * p                                                                   ;

        printf("\n")                                                               ;
        printf("There are %d records!\n" , n)                                      ;
        printf("共有%d个学生\n" , n)                                               ;
        p = head                                                                   ;
        if(head != NULL) {
                while(p != NULL) {
                        printf("学号为%d,他的分数为%d\n" , p -> num , p -> score) ;    
                        p = p -> next                                              ;
                }
        } else {
                printf("链表为空!\n")                                             ;        
        }
}

main(void)
{
      node * stu , * p                    ;
      int num                             ;
      stu = creat()                       ;
      p = stu                             ;
      print(p)                            ;
      printf("\n\n")                      ;
      printf("请输入您要删除的学生信息:") ;
      scanf("%d", & num)                  ; // 注意,原来的代码这里有误
      print(del(p , num))                 ;
      
      system("pause")                     ;
      
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-11 19:29:17 From FishC Mobile | 显示全部楼层
谢谢啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 10:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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