您的位置:首页 > 其它

链表的操作

2013-09-15 21:59 274 查看
#include "iostream"
#include "string"
#include "windows.h"

using namespace std;

typedef class stuff
{
public:
int number;
string name;
class stuff *Next;
}Node;//,*LinkTable;

Node *CreateLink();
void OutPut(Node *Head);

int main()
{
Node *Head; //整个链表的头
Head = CreateLink();

OutPut(Head);

system("pause");
return 0;
}

Node *CreateLink()
{
int num;//保存输入的编号
string na;//保存输入的姓名
Node *head, *curr, *tail;

head = (Node *)malloc(sizeof(Node));//创建头节点
tail = head;
cout<<"please input the number:"<<endl;
cin>>num;
cout<<"please input the name:"<<endl;
cin>>na;

//创建新节点来保存输入的数据
while(10 != num)//&&(NULL != na))
{
curr = (Node *)malloc(sizeof(Node));
curr->number = num;
//		curr->name = na;

tail->Next = curr;
tail = curr;

cout<<"please input the number:"<<endl;
cin>>num;
cout<<"please input the name:"<<endl;
cin>>na;

}
tail->Next = NULL;

return head;
}

void OutPut(Node *Head)
{
Node *p;
p = Head->Next;
if('\0' == p)
cout<<"Linktable is NULL!"<<endl;
else
{
do
{
cout<<p->number<<endl;
cout<<p->name<<endl;
p = p->Next;
}while(NULL != p);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: