---
title: "动态链表"
author: "smallearth"
date: "2012-03-03T19:09:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2013-11-30T20:10:52Z"
id: 1983345
url: "https://smallearth.iteye.com/blog/1983345"
---

// 动态链表0.cpp : 定义控制台应用程序的入口点。  
//

#include "stdafx.h"

#define NULL 0  
#include&lt;iostream&gt;

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&lt;&lt;"开始建表，依次输入要num pay sex ，以0 0 0结束。"&lt;&lt;endl;  
head=creat();

cout&lt;&lt;"输出所建链表"&lt;&lt;endl;  
print (head);

cout&lt;&lt;"输入要查找的节点"&lt;&lt;endl;

cin&gt;&gt;reserchsum;

reserch(head,reserchsum);

cout&lt;&lt;"输入要插入的节点："&lt;&lt;endl;  
wage=new work;  
cin&gt;&gt;wage-&gt;num &gt;&gt;wage-&gt;pay&gt;&gt;wage-&gt;sex ;  
while(wage-&gt;num!=0)

{  
head= insert(head,wage);  
print(head);  
cout&lt;&lt;"输入要插入的节点，以0 0 0结束。"&lt;&lt;endl;  
wage=new work;  
cin&gt;&gt;wage-&gt;num &gt;&gt;wage-&gt;pay&gt;&gt;wage-&gt;sex ;  
}

cout&lt;&lt;"请输入要删除的节点"&lt;&lt;endl;  
cin&gt;&gt;del\_num;  
head=del(head,del\_num);  
print(head);

do  
{  
cout&lt;&lt;"输入要输出的节点"&lt;&lt;endl;  
cin&gt;&gt;a;  
putout(head,a);  
head=head-&gt;next;  
}while(a!=0);

return 0;  
}

work \*creat( ) //建表  
{ work \*head;

work \*p1,\*p2;

n=0;

p1=p2=new work;

cin&gt;&gt;p1-&gt;num&gt;&gt;p1-&gt;pay&gt;&gt;p1-&gt;sex;

head=NULL;  
while(p1-&gt;num!=0)  
{  
n=n+1;  
if(n==1)  
head=p1;  
else  
p2-&gt;next=p1;  
p2=p1;

p1=new work;

cin&gt;&gt;p1-&gt;num&gt;&gt;p1-&gt;pay&gt;&gt;p1-&gt;sex;  
}  
p2-&gt;next=NULL;

return(head);}

void \*print (work \*head)//输出  
{work \*p1;

p1=head;  
do  
{cout&lt;&lt;p1-&gt;num&lt;&lt;" "&lt;&lt;p1-&gt;pay&lt;&lt;" "&lt;&lt;p1-&gt;sex&lt;&lt;endl;  
p1=p1-&gt;next;}  
while(p1!=NULL);  
![](http://hi.csdn.net/attachment/201203/3/0_1330773134G8Yg.gif)

return 0;  
}

work \*reserch( work \*head,int reserchnum)//查找  
{work \*p1;  
p1=head;  
while(reserchnum!=p1-&gt;num )  
{  
p1=p1-&gt;next ;  
}

if(reserchnum==p1-&gt;num)  
{  
cout&lt;&lt;p1-&gt;num&lt;&lt;" "&lt;&lt;p1-&gt;pay&lt;&lt;" "&lt;&lt;p1-&gt;sex&lt;&lt;endl;

}  
else

{

cout&lt;&lt;"can't find"&lt;&lt;endl;  
}

return (head);}

work \*insert(work \*head ,work \*wage )//插入

{  
work \*p1,\*p2,\*p0;  
p0=wage;  
p1=head;

while ((p0-&gt;num&gt;p1-&gt;num)&&(p1-&gt;next!=NULL) )

{p2=p1;  
p1=p1-&gt;next;}

if(p0-&gt;num&lt;=p1-&gt;num)

{ if(head==p1)  
head=p0;  
else;  
p2-&gt;next=p0;  
p0-&gt;next=p1;  
}

else  
{p1-&gt;next=p0;p0-&gt;next =NULL;}  
n=n+1;

return(head);  
}

work \*del(work \*head,int del\_num)//删除  
{  
work \*p1,\*p2;  
p1=head;  
while(del\_num!=p1-&gt;num&&p1-&gt;next!=NULL)  
{ p2=p1;p1=p1-&gt;next ;  
}  
if(del\_num==p1-&gt;num )  
{if(del\_num==head-&gt;num )

head=p1-&gt;next ;

else  
p2-&gt;next=p1-&gt;next ;  
cout&lt;&lt;"delete"&lt;&lt;del\_num&lt;&lt;endl;  
n=n-1;  
}  
else  
cout&lt;&lt;"can't find"&lt;&lt;endl;  
return (head);}

work \*putout(work* head,int a)//遍历  
{  
work \*p;  
p=head;  
while(p-&gt;next!=0)  
{  
if(p-&gt;num==a)  
{ cout&lt;&lt;p-&gt;num&lt;&lt;p-&gt;pay&lt;&lt;p-&gt;sex&lt;&lt;endl;break; }  
else  
{cout&lt;&lt;" can't find."&lt;&lt;endl;break;}

}

return head;  
}