| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 417 人关注过本帖
标题:这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
只看楼主 加入收藏
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
#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
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:20 
程序代码:
#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;
    n=0;
    head = new student;
    head->next = NULL;
    while(cin>>head->num && head->num == 0)
    {
        cout << "不能创建空链表" << endl;
    }

    student*insert(student*,student*);

    p1 = new student;
    p1->next = NULL;
    while(cin>>p1->num && p1->num != 0)
    {   
        head  = insert(head,p1);//使creat()出的链表是有序的。
        p1 = new student;
        p1->next = NULL;
    }
    delete p1;//养成好习惯,虽然是个小程序,但是出现内存泄漏总是不好的。
    return(head);
}


student*insert(student*head,student*stud)
{
    student*p0,*p1,*p2;
    p1=head;
    p0=stud;
    p2 = NULL;
    while((p1 != NULL)&&(p0->num>p1->num))
    {
         p2=p1;
         p1=p1->next;
    }
    if(p2 == NULL)//head->num <= strud->num时p2==NULL
    {
        stud->next = head;
        head = stud;
    }
    else
    {//这是怎样在链表中插入一个新节点。要先做保存
        student* temp = p2->next;
        p2->next = stud;
        stud->next = temp;
    }
    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)//自己慢慢看
        {
            student* temp = head;
            head = head->next;
            delete temp;
        }
        else
        {
            student* temp = p2->next;
            p2->next=p1->next;
            delete temp;
        }
        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;
    while(p!=NULL)//
    {
        cout << p->num << ' ';
        p=p->next;
    }
    cout << endl;
}
排序出问题的一个原因是因为创建出的链表本身并不保证是有序的。我改了,大体没有错误,但是还是有内存泄漏的问题,你自己改改的。
收到的鲜花

迭代的是人,递归的是神。
2011-04-02 09:05
安静的高调
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-3-26
收藏
得分:0 
你好厉害啊!谢谢帮我改程序!
2011-04-03 13:37
快速回复:这是关于链表的程序,出现了问题,不知哪出错,请求帮助!
数据加载中...
 
   



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

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