| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2155 人关注过本帖
标题:c语言链表问题
只看楼主 加入收藏
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
结帖率:57.14%
收藏
 问题点数:0 回复次数:6 
c语言链表问题
#include"stdio.h"
#include"stdlib.h"
#define LEN sizeof(struct Student)
struct Student
{
    long num;
    char name[20];
    char sex[5];
    int old;
    struct Student *next;
};
int n=0;
/*建立链表(不止一种方式)*/
struct Student *creat(void)
{
    struct Student *head,*p1,*p2;
    p1=p2=(struct Student *)malloc(LEN);
    printf("输入第%d学生的信息(学号(学号为零就结束),姓名,性别(man or woman),年龄):\n",n+1);
    scanf("%ld,%s,%s,%d",&p1->num,p1->name,p1->sex,&p1->old);
    head=NULL;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=malloc(LEN);
        printf("输入第%d学生的信息(学号,姓名,性别(man or woman),年龄):\n",n+1);
        scanf("%ld,%s,%s,%d",&p1->num,p1->name,p1->sex,&p1->old);
    }
    p2->next=NULL;
    return head;
}
struct Student *del(struct Student *head,int old)
{
    struct Student *p1,*p2;
    int i=1;
    p1=head;
    while(i==1)
    {
        while((old!=p1->old)&&(p1->next!=NULL))
        {
            p2=p1;p1=p1->next;
        }
        if(old==p1->old)
        {
            if(p1==head)head=p1->next;
            else p2->next=p1->next;
            i=1;
        }
        else
        {
            printf("%d not been found!\n",old);i=0;
        }

    }
    return (head);
}
void print(struct Student *head)
{
    struct Student *p;
    printf("\nnow these %d records are:\n",n);
    p=head;
    if(head!=NULL)
    {
        do
        {
            printf("%ld,%s,%s,%d",p->num,p->name,p->sex,p->old);
            p=p->next;
        }while(p!=NULL);
    }
}
int main(void)
{
    int old;
    struct Student *head;
    head=creat();
    printf("输入一个年龄:");
    scanf("%d",&old);
    head=del(head,old);
    print(head);
    return 0;
}
为什么结果不正确啊??
搜索更多相关主题的帖子: include c语言 信息 姓名 
2015-05-28 09:15
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
初步改了下。。。能力有限。。。
#include"stdio.h"
#include"stdlib.h"
#define LEN sizeof(struct Student)
typedef struct Student {
    long num;
    char name[20];
    char sex[5];
    int old;
    struct Student *next;
}Student;
int n=0;
/*建立链表(不止一种方式)*/
Student *creat(void){
    Student *head,*p;long num;
    p=head=(Student *)malloc(LEN);  
    while(1){
        p->next=(Student *)malloc(LEN);
        printf("输入第%d学生的信息(学号(学号为零就结束):",n+1);scanf("%ld",&num);
        if(num==0)break;p->num=num;
        printf("输入第%d学生的姓名:",n+1);scanf("%s",p->name);
        printf("输入第%d学生的性别(man or woman):",n+1);scanf("%s",p->sex);
        printf("输入第%d学生的年龄:",n+1);scanf("%d",&p->old);
        n++;
        p=p->next;
    }
    p=NULL;
    return head;
}
Student *del(Student *head,int old){
    Student *p=head,*q;int c=0;
    while(c<n){
        if(p->next->old==old){
            q=p->next;
            p->next=q->next;
        }
        p=p->next;c++;
    }  
    n--;
    return (head);
}
void print(Student *head){
    Student *p=head;int cc=0;
    printf("\nnow these %d records are:\n",n);
    while(cc<n){
        printf("%ld,%s,%s,%d\n",p->num,p->name,p->sex,p->old);
        p=p->next;cc++;
    }
}
int main(void){
    int old;
    Student *head;
    head=creat();
    printf("输入一个年龄:");
    scanf("%d",&old);
    head=del(head,old);
    print(head);
    return 0;
}

剑栈风樯各苦辛,别时冰雪到时春
2015-05-28 15:59
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 2楼 林月儿
怎感觉这个程序有bug,如果数据不在链表中,,那就编译出错了额!不过还是非常感谢啊!
2015-05-28 20:46
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
回复 3楼 鱼游海底
没仔细看,不过受源码框架的限制,建议做大的调整。

剑栈风樯各苦辛,别时冰雪到时春
2015-05-28 20:52
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 4楼 林月儿
好吧!你可以从新按照自己的思路把这个问题的程序编写出来吗?
2015-05-28 21:02
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
不是说非要什么固定的模板,只是说基础的链表处理要恰当,其次才是添加一些个性化的东西
如你做的结点内容的扩充,先解决的就是链表的增删改查其次才是枝枝叶叶的东西。
我做的可能有Bug的话,还是用LinkedList,HashMap试试,而且算法封装了处理简单。
要自己思考。。。。。。
#include<stdio.h>
#include<stdlib.h>
typedef struct Student{
     long num;
     char name[20];
     char sex[5];
     int old;
     struct Student *next;
}Studet;                  

Student *build(int k){
    int i;
    Student *head=(Student *)malloc(sizeof(Student));      
    Student *p=head;
    for(i=1;i<k;i++){
        printf("input the NO.%d element:",i);      
        p->next=(Student *)malloc(sizeof(Student));
        printf("输入第%d学生的信息\n学号:",i);scanf("%ld",&p->num);
        printf("输入第%d学生的姓名:",i);scanf("%s",p->name);
        printf("输入第%d学生的性别(man or woman):",i);scanf("%s",p->sex);
        printf("输入第%d学生的年龄:",i);scanf("%d",&p->old);  
        p=p->next;
    }               
    printf("输入第%d学生的信息\n学号:",i);scanf("%ld",&p->num);
    printf("输入第%d学生的姓名:",i);scanf("%s",p->name);
    printf("输入第%d学生的性别(man or woman):",i);scanf("%s",p->sex);
    printf("输入第%d学生的年龄:",i);scanf("%d",&p->old);            
    p->next=NULL;
    return head;
}      

void display(Student *h){
     Student *p=h;int c=0;
     while(p){
         p=p->next;c++;
     }p=h;
     printf("\nnow these %d records are:\n",c);
     while(p->next!=NULL){
        printf("%ld,%s,%s,%d\n",p->num,p->name,p->sex,p->old);
        p=p->next;
    }
    printf("%ld,%s,%s,%d\n",p->num,p->name,p->sex,p->old);   
}   

Student *del(Student *h,int old){
    Student *p=h,*q=h;
    if(p->old==old)h=h->next;
    else
    while(p->next){
        q=p->next;
        if(q->old==old&&q){
            if(q->next==NULL){p->next=NULL;break;}
            else p->next=q->next;
        }
        p=p->next;
    }   
    return h;
}
main(){
    Studet *head;
    int k,old;
    printf("input the length of Line:");
    scanf("%d",&k);
    head=build(k);
    display(head);
    printf("输入一个年龄:");
    scanf("%d",&old);
    head=del(head,old);
    display(head);
}

剑栈风樯各苦辛,别时冰雪到时春
2015-05-28 22:15
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 6楼 林月儿
好的!
2015-05-29 20:51
快速回复:c语言链表问题
数据加载中...
 
   



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

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