鱼C论坛

 找回密码
 立即注册
查看: 3168|回复: 6

程序求改善

[复制链接]
发表于 2012-3-23 14:42:30 | 显示全部楼层 |阅读模式
8鱼币
本帖最后由 lyq19900123 于 2012-3-24 12:38 编辑

这个程序比较长,我自己看晕了,哪位大侠帮忙看看,点评一下,帮我找找哪里有漏洞?程序实现用链表存储个人信息,以及对链表的删除和插入!主要是我改到最后在插入链表的时候会自动退出!灰常感谢!

#include<stdio.h>
#include<malloc.h>
#include<string.h>

#define NULL 0
#define LEN sizeof(struct student)

struct student *creat(void);                    //创建链表
void print(struct student *p);                  // 打印链表
struct student *dele(struct student *p);        //删除链表
struct student *insert(struct student *head);   // 插入链表
void command(struct student *head);                             //用于输入操作指令


struct student                                //定义结构体
{
    int number;
    char name[10];
    long telephone;
    struct student *next;
};

void main()                              
{
    struct student *head;
    printf("欢迎使用XX学校个人信息录入系统!\n");
    head=creat();
    print(head);
    command(head);
    printf("\n请退出系统!\n");

}

struct student *creat(void)                 // 创建链表
{
struct student *head,*p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(LEN);
printf("学号:");
scanf("%d",&(p1->number));
printf("\n姓名:");
scanf("%s",&(p1->name));
printf("\n电话号码:");
scanf("%ld",&(p1->telephone));

    while(p1->number!=0)
        {
            n+=1;
           if(n==1)
               head=p1;

           else
              p2->next=p1;
           p2=p1;
           p1=(struct student *)malloc(LEN);
           printf("\n学号:");
           scanf("%d",&(p1->number));
           printf("\n姓名:");
           scanf("%s",&(p1->name));
           printf("\n电话号码:");
           scanf("%ld",&(p1->telephone));
  
         }
        p2->next=NULL;
        return head;
}


void print(struct student *p)                       //打印链表
{
      do
     {
       printf("\n%d\t%s\t%ld",p->number,p->name,p->telephone);
       p=p->next;
     }
     while(p!=NULL);
}

struct student *dele(struct student *head)          // 通过输入学号实现对链表数据的删除

{
       int x;
       printf("\n请输入要删除的学号:");
       scanf("%d",&x);
       struct student *p1,*p2;
       if(head==NULL)
      {
            printf("nolist!");
            goto end;
       }
        p1=head;
       while(p1->number!=x)
       {
             if(p1->next==NULL)
             {
                  printf("找不到对应学号!");
                  return head;
             }
             p2=p1;
             p1=p1->next;
        }
        if(p1==head)
              head=p1->next;
        else
              p2->next=p1->next;
end: return head;
}

struct student *insert(struct student *head)       // 通过输入学号实现对链表数据的插入
{
        printf("\n请输入要插入的学号:");
        struct student *p0,*p1,*p2;
        p0=(struct student *)malloc(LEN);
        p1=head;
        scanf("%d",&p0->number);
        if(head==NULL)
            {
                  head=p0;
                  p0->next=NULL;
                  return head;
            }
        else
               while(p0->number>=p1->number)
                   {
                          if(p1->next==NULL)
                          {
                                 p1->next=p0;
                                 p0->next=NULL;
                                 return head;
                           }
                           p2=p1;
                           p1=p1->next;
                    }                    
               if(p1==head)
                    {
                            p0->next=head;
                            head=p0;
                            return head;
                     }

                else
                     {
                          p2->next=p0;
                          p0->next=p1;
                      }
                  return head;

}

void command(struct student *head)
{
       char instruction[10];
      printf("\n请输入操作指令:");           //输入操作指令,控制插入和删除
      scanf("%s",&instruction);
      if(strcmp(instruction,"delete")==0)
          {
                 head=dele(head);                     //调用删除函数
                 print(head);
                command(head);
          }
       else if(strcmp(instruction,"insert")==0)
          {
                   head=insert(head);                   //调用插入函数
                   print(head);
                   command(head);
          }
       else if(strcmp(instruction,"stop")==0)
                   return;
       else
          {
                  printf("\n无效指令!请重新输入!\n");
                  command(head);
          }
}

最佳答案

查看完整内容

你那个insert函数里面的 if 判断都用了赋值。。。再改改。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-3-23 14:42:31 | 显示全部楼层
你那个insert函数里面的 if 判断都用了赋值。。。再改改。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-24 09:50:12 | 显示全部楼层
楼主 ,删除功能能实现吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-3-24 12:37:53 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-24 18:34:01 | 显示全部楼层
你的链表没有头结点,进行边界操作的时候很容易出现问题,因为不统一!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-24 18:34:38 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student *creat(void);                    //创建链表
void print(struct student *p);                  // 打印链表
struct student *dele(struct student *head);        //删除链表
struct student *insert(struct student *head);      // 插入链表
void command(struct student *head);                             //用于输入操作指令


struct student                                //定义结构体
{
    int number;
    char name[10];
    long telephone;
    struct student *next;
};

void main()                              
{
    struct student * head;
    printf("欢迎使用XX学校个人信息录入系统!\n");
    head=creat();
    print(head);
    command(head);
    printf("\n请退出系统!\n");

}

struct student *creat(void)                 // 创建链表
{
        struct student *head, *p1;
        int n=0;
        head = (struct student*)malloc(sizeof(struct student));
        struct student * pTail = head;
        pTail->next = NULL;

        p1 = (struct student*)malloc(sizeof(struct student));
        printf("\n学号:");
        scanf("%d",&(p1->number));
        printf("姓名:");
        scanf("%s",&(p1->name));
        printf("电话号码:");
        scanf("%ld",&(p1->telephone));
       
    while(p1->number!=0)
        {
                pTail->next = p1;
                pTail = p1;
                p1->next = NULL;
                p1=(struct student *)malloc(sizeof(struct student));
                printf("\n学号:");
                scanf("%d", &(p1->number));
                printf("姓名:");
                scanf("%s", &(p1->name));
                printf("电话号码:");
                scanf("%ld", &(p1->telephone));
               
        }
        pTail->next=NULL;

        return head;
}


void print(struct student *p)                       //打印链表
{
        p = p->next;        //p指向了首节点
        if (p == NULL)
        {
                printf("空链表!\n");
                return ;
        }
        printf("\n学号\t\t姓名\t\t电话号码");
      do
     {
       printf("\n%d\t\t%s\t\t%ld",p->number,p->name,p->telephone);
       p=p->next;
     }
     while(p!=NULL);
}

struct student *dele(struct student *head)          // 通过输入学号实现对链表数据的删除
{
        int x;
        printf("\n请输入要删除的学号:");
        scanf("%d",&x);
        struct student *p1,*p2;
        if(head==NULL)
        {
                printf("nolist!");
                exit(-1);
        }
        p1=head->next;
        p2 = head;
        while(p1->number!=x)
        {
                if(p1->next==NULL)
                {
                        printf("找不到对应学号!");
                        return head;
                }
                p2 = p1;                //记录p1的前一个节点
                p1=p1->next;
        }
        p2->next = p2->next->next;
        free(p1);                        //p1是要删除的节点
       
        return head;
}

struct student *insert(struct student *head)       // 通过输入学号实现对链表数据的插入(学号按升序)
{
        printf("\n请输入要插入的学号:");
        struct student *p0, *p1, *p2;
        p0=(struct student *)malloc(sizeof(struct student));
        p1=head;
               
        scanf("%d",&p0->number);
                printf("姓名:");
                scanf("%s", &(p0->name));
                printf("电话号码:");
                scanf("%ld", &(p0->telephone));

                while (p1->next != NULL)
                {
                        p2 = p1;
                        p1 = p1->next;
                        if (p0->number < p1->number)
                                {
                                        p0->next = p2->next;
                                        p2->next = p0;
                                        return head;
                                }
                       
                }
                p1->next = p0;
                p0->next = NULL;

                return head;
        
}

void command(struct student *head)
{
      char instruction[10];
      printf("\n请输入操作指令:");           //输入操作指令,控制插入和删除
      scanf("%s",&instruction);
      if(strcmp(instruction,"delete")==0)
          {
                 head=dele(head);                     //调用删除函数
//                                 printf("删除的元素:%d  %s  %ld", val.name, val.);
                 print(head);
                command(head);
          }
       else if(strcmp(instruction,"insert")==0)
          {
                   head=insert(head);                   //调用插入函数
                   print(head);
                   command(head);
          }
       else if(strcmp(instruction,"stop")==0)
                   return;
       else
          {
                  printf("\n无效指令!请重新输入!\n");
                  command(head);
          }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-24 18:35:29 | 显示全部楼层
看看我上面的程序 符不符合你的要求,边界问题我都试过,基本上没有什么问题!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-14 19:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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