| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 608 人关注过本帖
标题:帮我来看下这个程序!谢谢啦!
只看楼主 加入收藏
jiang891130
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-16
收藏
 问题点数:0 回复次数:7 
帮我来看下这个程序!谢谢啦!
题目是这样的

链表操作:
利用链表的插入运算建立线性链表,然后利用链表的查找、删除、计数、输出等运算反复实现链表的这些操作(插入、删除、查找、计数、输出单独写成函数的形式),并能在屏幕上输出操作前后的结果。
   我写了程序就是用TC不能运行!还有我写的对不对!~谁帮我看下啊!谢谢。。

      #include<stdio.h>
#include<malloc.h>
#include<string.h>
#define null 0
struct node/* The struct for the information of a student. */
{int num;
char name[8];
float score;
struct node *next;
};
typedef struct node node;
node *findnum(node *head,int snum) /* Finding the needed num from the database*/
{ node *t;
t=head->next;
while(t&&(t->num!=snum))
t=t->next;
return t;
}
node *findname(node *head,char s[]) /* Finding the needed name from the database*/
{node *t;
t=head->next;
while(t&&strcmp(t->name,s))
t=t->next;
return t;
}
void findnumout(node *head,int snum)/*Find the record according to num and print it out*/
{node *t;int sign=1;
t=head->next;
puts("\nFinding:");
while(t)
{if(t->num==snum)
{printf("The student you want to find:  num:%d,name:%s,score:%f\n",t->num,t->name,t->score);
sign=0;
}
t=t->next;
}
if(sign)
puts("It is wrong!Not find!\n");
}
void findnameout(node *head,char s[])/*Find the record according to name and print it out*/
{node *t;int sign=1;
t=head->next;
puts("\nNow it is finding:");
while(t)
{if(!strcmp(t->name,s))
{printf("The student you want to find:  num:%d,name:%s,score:%f\n",t->num,t->name,t->score);
sign=0;
}
t=t->next;
}
if(sign)
puts("It is wrong!Not find!\n");
}
node *findp(node *head,node *sp)
{node *t;
t=head;
while(t&&t->next!=sp)
t=t->next;
return t;
}
node *creatnode()   /* Creating a database .It is a linked-list with a header */
{node *head,*t,*p;char c;/* When the "name" received comes to "0",the creation is over.*/
head=(node*)malloc(sizeof(node));
head->next=null;
t=head;
p=(node*)malloc(sizeof(node));
p->next=null;
puts("Input the num:");
scanf("%d",&p->num);
puts("Input the name:(within 8 words)");
scanf("%s",p->name);c=getchar();
puts("Input the score:");
scanf("%f",&p->score);
while(p->name[0]!='0')/*The "name" equal to "0" stands for the creation-finished sign.*/
{t->next=p;p->next=null;
t=t->next;
p=(node*)malloc(sizeof(node));
puts("Input the num:");
scanf("%d",&p->num);
puts("Input the name:(within 8 words)");
scanf("%s",p->name);c=getchar();
puts("Input the score:");
scanf("%f",&p->score);
}
return head;
}
void insert(node *head,int snum)/* Inserting a record into the database. */
{node *p,*t;char c; /* The function inserts the record into the database according to "num". */
p=(node*)malloc(sizeof(node));
p->next=null;
t=findnum(head,snum); /* Find the record whose "num" equals to "snum".*/
puts("Inserting!\n");
puts("Input the num:");
scanf("%d",&p->num);
puts("Input the name:(within 8 words)");
scanf("%s",p->name);c=getchar();
puts("Input the score:");
scanf("%f",&p->score);
if(t)
{if(t->next!=null)
  {p->next=t->next;t->next=p;}
else
  t->next=p;
}
else puts("\nIt is wrong!\n");
}
void deletenode(node *head,int snum)/* Deleting a record from the database. */
{node *t;                      /*The function deletes the record according to "num". */
t=head;
while(t->next&&(t->next->num!=snum))/*Find the previous record of the "snum"-record */
t=t->next;
if(t->next)
t->next=t->next->next;
else puts("\nIt is wrong!\n");
}
void print(node *head)     /* Printing all the records. */
{node *t;t=head->next;
while(t)
{printf("The num:%d,name:%s,score:%f\n",t->num,t->name,t->score);t=t->next;}
free(t);
}
void sortnum(node *head)/*Sort all the nodes using the algorithm of bubble*/
{node *a,*b;int sign;   /*According to the nums */
while(a)
{a=head->next;
b=a->next;
sign=1;
while(b)
{if(a->num>b->num)
{findp(head,a)->next=b;a->next=b->next;b->next=a;b=a->next;sign=0;
}
else {a=b;b=b->next;}
}
if(sign)
break;
}
}
void sortscore(node *head)/*Sort all the nodes using the algorithm of bubble*/
{node *a,*b;int sign;     /*According to the score */
while(a)
{a=head->next;
b=a->next;
sign=1;
while(b)
{if(a->score>b->score)
{findp(head,a)->next=b;a->next=b->next;b->next=a;b=a->next;sign=0;
}
else {a=b;b=b->next;}
}
if(sign)
break;
}
}
void main()
{node *head;
head=creatnode();
puts("\nPrevious database is:\n");
print(head);
insert(head,12);/*Insert a record after the node whose num is 12*/
printf("After the action of inserting,the database is:\n");
print(head);
deletenode(head,11);/*Delete the record whose num is 11 */
puts("\nHaving been deleted,the database now is:\n");
print(head);
puts("\nTake the action of Sorting according to the num:\n");
sortnum(head);
print(head);
puts("\nTake the action of Sorting according to score:\n");
sortscore(head);
print(head);
findnumout(head,12);/*Check out the student whose num is 12. */
findnameout(head,"huhl");/*Check out the student whose name is huhl */
}
搜索更多相关主题的帖子: include null 
2008-06-16 18:16
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
你的代码写的好难看啊,连VC都对齐不了

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-06-16 18:20
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
先帮你整理一下
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define null 0

struct node/* The struct for the information of a student. */
{
    int num;
    char name[8];
    float score;
    struct node *next;
};
typedef struct node node;

node *findnum(node *head, int snum) /* Finding the needed num from the database*/
{
    node *t;
    t = head->next;
    while(t&&(t->num != snum))
        t = t->next;
    return t;
}

node *findname(node *head, char s[]) /* Finding the needed name from the database*/
{
    node *t;
    t = head->next;
    while(t&&strcmp(t->name, s))
        t = t->next;
    return t;
}

void findnumout(node *head, int snum)/*Find the record according to num and print it out*/
{
    node *t;
    int sign = 1;
    t = head->next;
    puts("\nFinding:");
    while(t)
    {
        if(t->num == snum)
        {
            printf("The student you want to find:  num:%d, name:%s, score:%f\n", t->num, t->name, t->score);
            sign = 0;
        }
        t = t->next;
    }
    if(sign)
        puts("It is wrong!Not find!\n");
}

void findnameout(node *head, char s[])/*Find the record according to name and print it out*/
{
    node *t;int sign = 1;
    t = head->next;
    puts("\nNow it is finding:");
    while(t)
    {
        if(!strcmp(t->name, s))
        {
            printf("The student you want to find:  num:%d, name:%s, score:%f\n", t->num, t->name, t->score);
            sign = 0;
        }
        t = t->next;
    }
    if(sign)
        puts("It is wrong!Not find!\n");
}

node *findp(node *head, node *sp)
{
    node *t;
    t = head;
    while(t&&t->next != sp)
        t = t->next;
    return t;
}

node *creatnode()   /* Creating a database .It is a linked-list with a header */
{
    node *head, *t, *p;char c;/* When the "name" received comes to "0", the creation is over.*/
    head = (node*)malloc(sizeof(node));
    head->next = null;
    t = head;
    p = (node*)malloc(sizeof(node));
    p->next = null;
    puts("Input the num:");
    scanf("%d", &p->num);
    puts("Input the name:(within 8 words)");
    scanf("%s", p->name);c = getchar();
    puts("Input the score:");
    scanf("%f", &p->score);
    while(p->name[0] != '0')/*The "name" equal to "0" stands for the creation-finished sign.*/
    {
        t->next = p;p->next = null;
        t = t->next;
        p = (node*)malloc(sizeof(node));
        puts("Input the num:");
        scanf("%d", &p->num);
        puts("Input the name:(within 8 words)");
        scanf("%s", p->name);c = getchar();
        puts("Input the score:");
        scanf("%f", &p->score);
    }
    return head;
}

void insert(node *head, int snum)/* Inserting a record into the database. */
{
    node *p, *t;char c; /* The function inserts the record into the database according to "num". */
    p = (node*)malloc(sizeof(node));
    p->next = null;
    t = findnum(head, snum); /* Find the record whose "num" equals to "snum".*/
    puts("Inserting!\n");
    puts("Input the num:");
    scanf("%d", &p->num);
    puts("Input the name:(within 8 words)");
    scanf("%s", p->name);c = getchar();
    puts("Input the score:");
    scanf("%f", &p->score);
    if(t)
    {
        if(t->next != null)
        {
            p->next = t->next;t->next = p;
        }
        else
            t->next = p;
    }
    else puts("\nIt is wrong!\n");
}

void deletenode(node *head, int snum)/* Deleting a record from the database. */
{
    node *t;                      /*The function deletes the record according to "num". */
    t = head;
    while(t->next&&(t->next->num != snum))/*Find the previous record of the "snum"-record */
        t = t->next;
    if(t->next)
        t->next = t->next->next;
    else puts("\nIt is wrong!\n");
}

void print(node *head)     /* Printing all the records. */
{
    node *t;t = head->next;
    while(t)
    {
        printf("The num:%d, name:%s, score:%f\n", t->num, t->name, t->score);t = t->next;
    }
    free(t);
}

void sortnum(node *head)/*Sort all the nodes using the algorithm of bubble*/
{
    node *a, *b;int sign;   /*According to the nums */
    while(a)
    {
        a = head->next;
        b = a->next;
        sign = 1;
        while(b)
        {
            if(a->num>b->num)
            {
                findp(head, a)->next = b;a->next = b->next;b->next = a;b = a->next;sign = 0;
            }
            else {
                a = b;b = b->next;
            }
        }
        if(sign)
            break;
    }
}

void sortscore(node *head)/*Sort all the nodes using the algorithm of bubble*/
{
    node *a, *b;int sign;     /*According to the score */
    while(a)
    {
        a = head->next;
        b = a->next;
        sign = 1;
        while(b)
        {
            if(a->score>b->score)
            {
                findp(head, a)->next = b;a->next = b->next;b->next = a;b = a->next;sign = 0;
            }
            else {
                a = b;b = b->next;
            }
        }
        if(sign)
            break;
    }
}

void main()
{
    node *head;
    head = creatnode();
    puts("\nPrevious database is:\n");
    print(head);
    insert(head, 12);/*Insert a record after the node whose num is 12*/
    printf("After the action of inserting, the database is:\n");
    print(head);
    deletenode(head, 11);/*Delete the record whose num is 11 */
    puts("\nHaving been deleted, the database now is:\n");
    print(head);
    puts("\nTake the action of Sorting according to the num:\n");
    sortnum(head);
    print(head);
    puts("\nTake the action of Sorting according to score:\n");
    sortscore(head);
    print(head);
    findnumout(head, 12);/*Check out the student whose num is 12. */
    findnameout(head, "huhl");/*Check out the student whose name is huhl */
}

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-06-16 18:24
jiang891130
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-16
收藏
得分:0 
要用TC运行啊!VC好像能运行的!
2008-06-16 18:28
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
至少我在VC6.0 + WinXP SP2下编译成功,运行正常,调试后没发现问题

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-06-16 18:43
jiang891130
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-6-16
收藏
得分:0 
谁帮我用TC看下啊!谢谢了!
2008-06-16 19:11
coming
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2008-4-20
收藏
得分:0 
嘿嘿,3#的编程书写习惯和我的一样,看上去舒服~~~
2008-06-16 21:50
bgtech
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-6-14
收藏
得分:0 
用TC试过了,没问题
2008-06-16 22:54
快速回复:帮我来看下这个程序!谢谢啦!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027619 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved