标题:这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
取消只看楼主
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
结帖率:100%
已结贴  问题点数:20 回复次数:1 
这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
#include<iostream>
using namespace std;
#define NULL 0
struct student
{
    int num;
    student*next;
};
int n;

int main()
{ student*creat(void);
  student*insert(student*,student*);
  student*del(student*,int);
  void print(student*);

  student*head;
  student*stu;
  int del_num;
  cout<<"input num:"<<endl;
  head=creat();
  print(head);
  cout<<endl<<"input the inserted record:";
  stu=new student;
  cin>>stu->num;
  while(stu->num!=0)
  {
    head=insert(head,stu);
    print(head);
    cout<<endl<<"input the inserted record:";
    stu=new student;
    cin>>stu->num;
  }
  cout<<endl<<"input the deleted number:";
  cin>>del_num;
  while(del_num!=0)
  {
    head=del(head,del_num);
    print(head);
    cout<<endl<<"input the deleted number:";
    cin>>del_num;
  }
return 0;
}
/*……………………………………………………主函数………………………………………………………………*/


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


student*insert(student*head,student*stud)
{student*p0,*p1,*p2;
 p1=head;
 p0=stud;
while((p0->num>p1->num)&&(p1->next!=NULL))
 {
     p2=p1;
     p1=p1->next=p0;
 }
 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);
}


student*del(student*head,int num)
{student*p1,*p2;
 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;
     cout<<"delete:"<<num<<endl;
 }
 else cout<<"It doesn't exist!";
return(head);
}

         
void print(student*head)
{student*p;
  cout<<"Now these num are:"<<endl;
  p=head;
  if(p!=NULL)
     p=p->next;
   cout<<p->next;

}
    这个是写关于链表的程序,要对链表中的各个结点的数按从小到大的顺序排列,然后再删去一个数后输出,对排序部分有点乱,我采用的是输入一个排一个的方法。麻烦帮我看一下!
搜索更多相关主题的帖子: include insert record 
2011-04-01 23:34
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
得分:0 
你好厉害啊!谢谢帮我改程序!
2011-04-03 13:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-335441-1-1.html




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

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