| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:过不去,过不去,忘各位帮帮看看怎么回事,谢谢
只看楼主 加入收藏
超人会飞
该用户已被删除
收藏
已结贴  问题点数:10 回复次数:5 
过不去,过不去,忘各位帮帮看看怎么回事,谢谢
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: head next include return 
2010-10-19 14:16
骑猪看日出
Rank: 1
等 级:新手上路
帖 子:9
专家分:2
注 册:2010-10-19
收藏
得分:2 
什么问题?哪里报错?错误什么提示?
2010-10-19 15:13
troy0215
Rank: 2
等 级:论坛游民
帖 子:16
专家分:71
注 册:2009-5-22
收藏
得分:2 
#include<stdio.h>
#include<malloc.h>
//#define NULL 0
#define LEN sizeof(struct student)
struct student
{
  int num;
  float score;
  struct student *next;
};
int n;
struct student*creat(void)
{
struct student*head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
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,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

/*输出链表*/
void print(struct student*head)
{
struct student*p;
printf("NOW,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
  {
   printf("%d%5.1f\n",p->num,p->score);
   p=p->next;
  }
while(p!=NULL);
}

  /*删除链表*/
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL) {printf("List null!\n");goto end;}
p1=head;
while(num!=p1->num && p1->next!=NULL)
{
  p2=p1;
  p1=p1->next;
}
  if(num==p1->num)
{
     if(p1==head) head=p1->next;
     else p2->next=p1->next;
     printf("delete :%d\n",num);
     n=n-1;
}
else printf("%d not been found!\n",num);
end:
return(head);
}

/*插入链表*/
struct student*insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
  while(p0->num>p1->num && p1->next!=NULL)
   {
    p2=p1;
    p1=p1->next;
   }
  if(p0->num<p1->num)
   {
    if(head==p1) head=p0;
    else p2->next=p0;
    p0->next=p1;
   }
  else
   {
    p1->next=p0;
    p0->next=NULL;
   }
}
return (head);
}

int main()
{
struct student *head,stu;
long del_num;
printf("input records:\n");
head=creat();                             /*建立链表返回头指针*/
print(head);                              /*输出全部节点*/
printf("\ninput the delete number:");
scanf("%ld",&del_num);                     /*输出要删除的学号*/
head=del(head,del_num);                    /*输出后链表的头地址*/
print(head);                              /*输出全部节点*/
printf("\ninput the insert number:");     /*输出要插入的节点*/
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
getchar();
return 0;
}
各种小错误,插入那部分功能可能不对~
2010-10-19 15:22
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:2 
程序代码:
#include<stdio.h>
#include<malloc.h>
struct linknode
{
  int data;
  struct linknode *next;
};
struct linknode *create()//创建单链表
{
    int d;
    int i=1;
    struct linknode *head,*s,*t;
    head=NULL;
   
    printf("建立一个单链表,data域数据已0结束:\n");
    while(1)
    {
        printf("%d:",i);
        scanf("%d",&d);
        if(d==0) 
             break;

        if(i==1)//建立第一个结点
        {
            head=(struct linknode *)malloc(sizeof(struct linknode));
            head->data=d;
            head->next=NULL;
            t=head;
        }
        else//建立其余结点
        {
            s=(struct linknode *)malloc(sizeof(struct linknode));
            s->data=d;
            s->next=NULL;
            t->next=s;
            t=s;
        }
        i++;
    }
    return head;
}
void disp(struct linknode *head)//输出结点数据
{
    struct linknode *p=head;
    printf("输出一个单链表:\n");
    if(p==NULL)
        printf("");
    while(p!=NULL)
    {
        printf("%4d",p->data);
        p=p->next;
    }
    printf("\n");
}
int len(struct linknode *head)//返回单链表的长度
{
    int pcount=0;//结点计数器
    struct linknode *p=head;
    while(p!=NULL)
    {
        p=p->next;
        pcount++;
    }
    return pcount;
}
struct linknode *find(struct linknode *head,int i)//返回第i个结点的指针
{
    struct linknode *p=head;
    int j=1;
    if(i>len(head)||i<0)
        return NULL;
    else
    {
        while(p!=NULL&&j<i)
        {
            j++;
            p=p->next;
        }
        return p;
    }
}
    /*在单链表第i个结点i>0之后插入一个data域为x的结点*/
struct linknode *insert(struct linknode *head,int i,int x)
{
    struct linknode *p,*s;
    s=(struct linknode *)malloc(sizeof(struct linknode));
    s->data=x;
    s->next=NULL;
    if(i==0)//插入头结点之前
    {
        s->next=head;
        head=s;
    }
    else
    {
        p=find(head,i);//查找第i 个结点并由p指向该结点
        if(p!=NULL)
        {
            s->next=p->next;
            p->next=s;
        }
        else
            printf("输入的i值不正确\n");
    }
    return head;
}
struct linknode *del(struct linknode *head,int i)//删除第i个结点
{
    struct linknode *p=head,*s;
    int j=i;
    if(i==1)
    {
        head=head->next;
        free(p);
    }
    else
    {
        p=find(head,i-1);//查找第i-1个结点并由p指向该结点
        if(p!=NULL&&p->next!=NULL)
        {
            s=p->next;//s指向要删除的结点
            p->next=s->next;
            free(s);
        }
        else
            printf("输入的i值不正确\n");
    }
    return head;
}
void dispose(struct linknode *head)
{
    struct linknode *pa=head,*pb;
    if(pa!=NULL)
    {
        pb=pa->next;
        if(pb==NULL)//只有一个结点的情况
        free(pa);
        else
        {
            while(pb!=NULL)
            {
                free(pa);
                pa=pb;
                pb=pb->next;
            }
            free(pa);
        }
    }
}
            

    
参考一下我的~
2010-10-19 15:24
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
  int num;
  float score;
  struct student *next;
};
int n;
struct student*creat(void)
{
struct student*head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
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,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

/*输出链表*/
void print(struct student*head)
{
struct student*p;
printf("NOW,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
  {
   printf("%d%5.1f\n",p->num,p->score);
   p=p->next;
  }
while(p!=NULL);
}

  /*删除链表*/
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL) {printf("List null!\n");
goto end;}
p1=head;
while(num!=p1->num && p1->next!=NULL)//!=打成!==
{
  p2=p1;
  p1=p1->next;
}
  if(num==p1->num)
{
     if(p1==head) head=p1->next;
     else p2->next=p1->next;
     printf("delete :%d\n",num);
     n=n-1;
}
else printf("%d not been found!\n",num);
end://改成冒号
return(head);
}

/*插入链表*/
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
  while(p0->num>p1->num && p1->next!=NULL)//!=打成!==
   {
    p2=p1;
    p1=p1->next;//少逗号
   }
  if(p0->num<p1->num)
   {
    if(head==p1) head=p0;
    else p2->next=p0;
    p0->next=p1;
   }
  else
   {
    p1->next=p0;
    p0->next=NULL;
   }
}
return (head);
}

void main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();                             /*建立链表返回头指针*/
print(head);  //少逗号                           /*输出全部节点*/
printf("\ninput the delete number:");
scanf("%ld",&del_num);                     /*输出要删除的学号*/
head=del(head,del_num);                    /*输出后链表的头地址*/
print(head);                              /*输出全部节点*/
printf("\ninput the insert number:") ;     /*输出要插入的节点*/
scanf("%d,%f",&stu->num,&stu->score);
head=insert(head,stu);
print(head);
} 
大致改了下~不抱错了~,具体没来得及调试
2010-10-19 15:32
jiangjj123
Rank: 1
等 级:新手上路
帖 子:15
专家分:5
注 册:2010-10-12
收藏
得分:2 
看不懂
2010-10-19 15:34
快速回复:过不去,过不去,忘各位帮帮看看怎么回事,谢谢
数据加载中...
 
   



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

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