// 动态链表0.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define NULL 0
#include<iostream>
using namespace std;
struct work
{
int num;
float pay;
char sex;
work *next;
};
int n;
int main()
{
work *creat(void);
void *print (work * );
work *reserch(work *,int);
work *insert(work *,work *);
work *del(work *,int);
work *putout(work *, int);
work *head, *wage;
int reserchsum,del_num,a;
cout<<"开始建表,依次输入要num pay sex ,以0 0 0结束。"<<endl;
head=creat();
cout<<"输出所建链表"<<endl;
print (head);
cout<<"输入要查找的节点"<<endl;
cin>>reserchsum;
reserch(head,reserchsum);
cout<<"输入要插入的节点:"<<endl;
wage=new work;
cin>>wage->num >>wage->pay>>wage->sex ;
while(wage->num!=0)
{
head= insert(head,wage);
print(head);
cout<<"输入要插入的节点,以0 0 0结束。"<<endl;
wage=new work;
cin>>wage->num >>wage->pay>>wage->sex ;
}
cout<<"请输入要删除的节点"<<endl;
cin>>del_num;
head=del(head,del_num);
print(head);
do
{
cout<<"输入要输出的节点"<<endl;
cin>>a;
putout(head,a);
head=head->next;
}while(a!=0);
return 0;
}
work *creat( ) //建表
{ work *head;
work *p1,*p2;
n=0;
p1=p2=new work;
cin>>p1->num>>p1->pay>>p1->sex;
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new work;
cin>>p1->num>>p1->pay>>p1->sex;
}
p2->next=NULL;
return(head);}
void *print (work *head)//输出
{work *p1;
p1=head;
do
{cout<<p1->num<<" "<<p1->pay<<" "<<p1->sex<<endl;
p1=p1->next;}
while(p1!=NULL);
return 0;
}
work *reserch( work *head,int reserchnum)//查找
{work *p1;
p1=head;
while(reserchnum!=p1->num )
{
p1=p1->next ;
}
if(reserchnum==p1->num)
{
cout<<p1->num<<" "<<p1->pay<<" "<<p1->sex<<endl;
}
else
{
cout<<"can't find"<<endl;
}
return (head);}
work *insert(work *head ,work *wage )//插入
{
work *p1,*p2,*p0;
p0=wage;
p1=head;
while ((p0->num>p1->num)&&(p1->next!=NULL) )
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{ if(head==p1)
head=p0;
else;
p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next =NULL;}
n=n+1;
return(head);
}
work *del(work *head,int del_num)//删除
{
work *p1,*p2;
p1=head;
while(del_num!=p1->num&&p1->next!=NULL)
{ p2=p1;p1=p1->next ;
}
if(del_num==p1->num )
{if(del_num==head->num )
head=p1->next ;
else
p2->next=p1->next ;
cout<<"delete"<<del_num<<endl;
n=n-1;
}
else
cout<<"can't find"<<endl;
return (head);}
work *putout(work* head,int a)//遍历
{
work *p;
p=head;
while(p->next!=0)
{
if(p->num==a)
{ cout<<p->num<<p->pay<<p->sex<<endl;break; }
else
{cout<<" can't find."<<endl;break;}
}
return head;
}