回 帖 发 新 帖 刷新版面

主题:帮帮忙

#include<iostream>
using namespace std;
#define NULL 0
struct number
{
    int num;
    number*next;
};
int n,temp;



int main()
{ number*creat(void);
  number*del(number*,long);

  void print(number*);
  number*head;
  long del_num;
  cout<<"input number:"<<endl;
  head=creat();      
                  //返回头指针
  print(head);                           //输出全部结点
  
  cout<<endl<<"input the deleted number:";
  cin>>del_num;
  while(del_num!=0)
     {
      head=del(head,del_num);
      print(head);
      cout<<"input the deleted number:";
      cin>>del_num;
  }
  return 0;


 
/*................................................creat.................................................................................*/

number *creat(void)                   //建立链表函数
{number *head;
number *p1,*p2;
n=0;
p1=p2=new number;
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 number;
     cin>>p1->num;
     p2->next=NULL;
     }
return(head);
}

/*............................................delete................................................................................*/
number *del(number *head,long num)               //删除数字
{number *p1,*p2;
if(head==NULL)
  {cout<<"list null!"<<endl;return(head);}
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;
    }
return(head);
}

/*..............................................print order...............................................................................*/

 print(number *head)
{
    number *p,*p1,*p2;


    p=head;
    p1=head;
    while((p->num>p1->num)&&(p1->next!=NULL))
    {p2=p1;
    p1=p1->next;
    }
    if(p->num>p1->num)
    { temp=p->num;
      p->num=p1->num;
      p1->num=temp;
    }
    else{p1->next=p;p->next=NULL;

    if(head!=NULL)
        do{ 
        cout<<p->num<<" "<<endl;
        p=p->next;
        }while(p!=NULL);
   
return 0;
}

回复列表 (共1个回复)

沙发

我建议你把你的问题提出来,只给程序,很难让人知道你的问题处在。最好把程序功能也表达一下。

我来回复

您尚未登录,请登录后再回复。点此登录或注册