帮帮忙 链表问题
#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...............................................................................*/
void print(number *head)
{
number *p,*p1,*p2;
p=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;
}
if(head!=NULL)
do{
cout<<p->num<<" "<<endl;
p=p->next;
}while(p!=NULL);
}