注册 登录
编程论坛 C++教室

这是关于链表的程序,出现了问题,不知哪出错,请求帮助!

安静的高调 发布于 2011-04-01 23:34, 417 次点击
#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;

}
    这个是写关于链表的程序,要对链表中的各个结点的数按从小到大的顺序排列,然后再删去一个数后输出,对排序部分有点乱,我采用的是输入一个排一个的方法。麻烦帮我看一下!
2 回复
#2
lintaoyn2011-04-02 09:05
程序代码:
#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;
}
排序出问题的一个原因是因为创建出的链表本身并不保证是有序的。我改了,大体没有错误,但是还是有内存泄漏的问题,你自己改改的。
#3
安静的高调2011-04-03 13:37
你好厉害啊!谢谢帮我改程序!
1