免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1399 | 回复: 5
打印 上一主题 下一主题

[C] 求助C语言高手——一个链表的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-24 10:10 |只看该作者 |倒序浏览
我编了一个程序,可是运行时总是有问题,得不到想要的结果,还请诸位高手指教。
(可能是输出函数和删除函数有问题)
#include<malloc.h>
#define NULL 0
#define LEN  sizeof(struct student )
int n;
struct student
{int num;
int score;
struct student *next;
};
struct student *creat()        /*链表的创建*/
{struct student *p1,*p2;
struct student *head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d,%d",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
  if(n==1)head=p1;
  else p2->next=p1;
  p2=p1;
  p1=(struct student *)malloc(LEN);
  scanf("%d,%d",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)   /*链表的输出*/   
{struct student *p;
printf("\nNow,These %d record are:\n",n);
p=head;
if(head!=NULL)
  do
    {printf("%d,%d\n",p->num,p->score);
     p=p->next;
   }while(p!=NULL);
}
struct student *insert(struct student *head,struct student *q,int num)      /*链表的后插入*/
{struct student *p0,*p1,*p2;
p1=head;p0=q;
while((p1->num!=num)&&(p1!=NULL))
   {p2=p1;p1=p1->next;}
if(p1==head){p0->next=head->next;head->next=p0;}
else{p2->next=p0;p0->next=p1;}
n=n+1;
return(head);
}
struct student *delete(struct student *head,int NUM)   /*链表的删除*/
{struct student *p1,*p2;
p1=head;
while((p1->num!=NUM)&&(p1!=NULL))
{p2=p1;p1=p1->next;}
if(p1==NULL)printf("The List null!\n");
else {p2->next=p1->next;free(p1);n=n-1;}
return(head);
}
main()
{struct student *head,*q;
int ins_num,del_num;/*创建要插入和删除的学生的学号*/
q=(struct student*)malloc(LEN);
scanf("%d,%d",q->num,q->score);
head=creat();
print(head);
printf("please input the ins_num:\n");
scanf("%d",&ins_num);
head=insert(head,q,ins_num);
print(head);
printf("please input the del_num:\n");
scanf("%d",&del_num);
head=delete(head,del_num);
print(head);
}


[ 本帖最后由 niqiumao 于 2006-10-24 10:12 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-10-24 10:40 |只看该作者
能不能把你的结果打印一下,
不确定你输入数据的时候num和score之间有没有用‘,’分隔,另外在循环中使用scanf的话,有可能也会出些问题

论坛徽章:
0
3 [报告]
发表于 2006-10-24 11:04 |只看该作者
真的挺讨厌这样乱七八糟的把代码一贴,然后人家来找错,挺不负责任的感觉

论坛徽章:
0
4 [报告]
发表于 2006-10-24 11:19 |只看该作者

回复 1楼 niqiumao 的帖子

#include
#include

#define NULL 0

int n;
typedef struct student
{
        int num;
        int score;
        struct student *next;
}student_t;

static student_t *creat()        /*链表的创建*/
{
        student_t *p1;
        student_t *head;
        n=0;
        head = (student_t *)calloc(sizeof(student_t), 1);
        p1 = head; p1->next = NULL;
        cin >> p1->num >> p1->socre ;
        while(p1->num != 0)
        {
            n += 1;
            p1->next = (student_t *)calloc(sizeof(student_t), 1);
            p1->next->next = NULL;
            cin >> p1->num >> p1->socre ;
            p1 = p1->next;
        }

        return(head);
}

static void print(struct student *head)   /*链表的输出*/   
{
        printf("\nNow,These %d record are:\n",n);

        for(student_t *p = head; p; p=p->next)
            printf("%d,%d\n",p->num,p->score);
}

static student_t *insert(student_t *head, student_t *q,int num)      /*链表的后插入*/
{
    student_t *p0, *p1;

    if(num <= 0 || !q) return NULL;

    for(p0 = p1 = head; p0; p1 = p0, p0 = p0->next)
        ;
   
    if(p1)
    {
        p1->next = q;
        n += num;
    }

        return(head);
}

static student_t *delete(student_t *head, const int NUM)   /*链表的删除*/
{
    student_t *p = NULL;
    int  count = NUM;
    do
    {
        p = head;
        if(!p) break;
        else
        {
            head = head->next;
            free(p);
        }
    }while(count--);
    n = n-NUM+count;

    return(head);
}

void main()
{
    //do somegthing
}


改了一点实在没心情改了

[ 本帖最后由 chzht001 于 2006-10-24 11:22 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-10-24 18:27 |只看该作者
怎么还不行吗?我原来给你发了个不行吗?在我机子上运行对的啊?



  1. #include <malloc.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define LEN  sizeof(struct student)

  5. int n;
  6. struct student
  7. {
  8.         long num;
  9.         float score;
  10.         struct student *next;
  11. };

  12. /*            creat()        */
  13. struct student *creat()
  14. {
  15.         struct student *p1,*p2;
  16.         struct student *head;

  17.         n=0;
  18.         p1=(struct student *)malloc(LEN);
  19.         p2 = p1;
  20.         printf("Input num and score:");
  21.         scanf("%ld,%f",&p1->num,&(p1->score));
  22.         head=NULL;

  23.         while (p1->num != 0)
  24.         {
  25.                 printf("num=%ld,score=%f\n",p1->num,p1->score);
  26.                 n=n+1;
  27.                 if(n == 1) head = p1;
  28.                 else p2->next = p1;
  29.                 p2 = p1;
  30.                 p1=(struct student *)malloc(LEN);
  31.                 printf("Input num and score:");
  32.                 scanf("%ld,%f",&p1->num,&p1->score);
  33.         }

  34.         p2->next=NULL;
  35.         return(head);
  36. }

  37. /*      print()      */
  38. void print(struct student *head)
  39. {
  40.         struct student *p;
  41.         printf("\nNow,These %d record are:\n",n);
  42.         p = head;

  43.         if(head!=NULL)
  44.         {
  45.                 do
  46.                 {
  47.                         printf("%ld,%5.1f\n",p->num, p->score);
  48.                         p=p->next;
  49.                 }while(p!=NULL);
  50.         }
  51. }

  52. /*      insert()     */
  53. struct student *insert(struct student *head,int i)
  54. {
  55.         struct student *p1=head,*p;
  56.         struct student *q = NULL;
  57.         int k = 1;

  58.         q = (struct student *)malloc(sizeof(struct student));
  59.         if (!q) printf("OVERFLOW!"),exit(0);
  60.         printf("Input num and score:");
  61.         scanf("%ld,%f", &q->num, &q->score);

  62.         for (; k<i&&p1; k++)
  63.         {
  64.                 p1 = p1->next;
  65.         }
  66.         if (!p1 && i<1) printf("input error!\n"),exit(0);

  67.         q->next = p1->next;
  68.         p1->next = q;

  69.         return head;
  70. }

  71. struct student *delete(struct student *head, int num)
  72. {
  73.         struct student *p1,*p2;
  74.         p1=head;

  75.         while((p1->num != num) && (p1!=NULL))
  76.         {
  77.                 p2=p1;
  78.                 p1=p1->next;
  79.         }

  80.         if (!p1) printf("The List null!\n"),exit(0);
  81.         else
  82.         {
  83.                 p2->next = p1->next;
  84.                 free(p1);
  85.          }

  86.         return(head);
  87. }

  88. int main()
  89. {
  90.         struct student *head,*q;
  91.         long del_num;
  92.         int ins_location;

  93.         head=creat();
  94.         print(head);

  95.         printf("Input the ins_location:");
  96.         scanf("%d",&ins_location);
  97.         head = insert(head, ins_location);
  98.         print(head);

  99.         printf("delete:please input the num:");
  100.         scanf("%ld",&del_num);
  101.         head=delete(head,del_num);
  102.         print(head);
  103.         return 0;
  104. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2006-10-24 18:28 |只看该作者
如果你那里运行不对,建议你去看一下C语言输入输出格式!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP