| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 286 人关注过本帖
标题:insert 函数编写错了,刚学链表不是很熟,看不出错误。求指教,另外推荐点学 ...
只看楼主 加入收藏
教皇
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2011-10-6
结帖率:95.65%
收藏
已结贴  问题点数:5 回复次数:2 
insert 函数编写错了,刚学链表不是很熟,看不出错误。求指教,另外推荐点学习链表指针的书!!!
#include<iostream.h>
#include<stdlib.h>
#define LEN sizeof(stud)
typedef struct student
{
 int num;
 int score;
 struct student  *next;
} stud;
int n;
void main()
{
 stud *creat(void);
 void print(stud *head);
 stud *insert(stud *head, stud *student);
 stud *del(stud *head,int num);
 stud *head,*student;
 int num;

 head=creat();
 print(head);
 
 cout<<"input the student";
 cin>>student->num>>student->score;
 head=insert(head,student);
 print(head);
 cout<<"input the student'num:";
 cin>>num;
 head=del(head,num);
 print(head);
}


 stud *creat(void)
 {
  stud *head,*p1,*p2;
  n=0;
  p1=p2=(stud *)malloc(LEN);
  head=NULL;
  cin>>p1->num>>p1->score;
  while(p1->num!=0)
  {
   n=n+1;
   if(n==1) head=p1;
   else p2->next=p1;
   p2=p1;
   p1=(stud*)malloc(LEN);
   cin>>p1->num>>p1->score;
  }
   p2->next=NULL;
   return head;
 }

 void print(stud *head)
 {
  stud *p;
  p=head;
  if(head!=NULL)
   while(p!=NULL)
   {
    cout<<p->num<<"   "<<p->score<<endl;
    p=p->next;
   }
 }

 stud *insert(stud *head,stud *student)
 {
  stud *p1,*p2,*p0;
  p0=student;
  p1=head;
  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;
   }
  
  }
 n=n+1;
   return(head);
}

 stud *del(stud *head,int num)
 {
  stud *p1,*p2;
  p1=head;
  if(head==NULL)
  {
   cout<<"list is empty.";
   return(head);
  }
  while(num!=p1->num&&p1->next!=NULL)
  {p2=p1;p1=p1->next;}
  if(num==p1->num)
  {
   if(head==p1)head=p1->next;
   else p2->next=p1->next;
   cout<<"delete"<<num;
   n=n-1;
  }
  else
   cout<<"it is not exsit";
  return (head);
 }




搜索更多相关主题的帖子: head void include insert 
2012-03-29 16:48
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:4 
stud *insert(stud *head,stud*student)
{
  stud *p1,*p2,*p0;
  p0=student;
  p1=head;
  if(head=NULL)  //判断相等是用什么?
  {   
      head=p0;

总有那身价贱的人给作业贴回复完整的代码
2012-03-29 17:05
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:1 
毛病不少,main函数的指针student没有分配内存就赋值

总有那身价贱的人给作业贴回复完整的代码
2012-03-29 17:07
快速回复:insert 函数编写错了,刚学链表不是很熟,看不出错误。求指教,另外推 ...
数据加载中...
 
   



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

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