C++编程(单链表)

来源:百度知道 编辑:UC知道 时间:2024/06/08 06:17:20
编写一个程序实现如下功能:建立一个单链表,每个结点数据要有职工号、工资。用一个creat函数来建立链表,用list函数输出数据(数据自定)。
大家不要想当然了!

#include <iostream>
using namespace std;

struct WORKER
{
int num;//职工号
double wage; //工资
};
struct NODE
{
WORKER worker;
NODE *next;
};

void addData(NODE *(&list))
{
int num;
double wage;
bool flag=true;
while(flag)
{
cout<<"请分别输入职工号和工资:";
cin>>num;
cin>>wage;
NODE *pNode=new NODE;
pNode->worker.num=num;
pNode->worker.wage=wage;

if(list==NULL)
{
list=pNode;
list->next=NULL;
}
else
{
NODE *p;
p=list;
while(p->next)
{
p=p->next;
}
p->next=pNode;
pNode->next=NULL;

}

char ch;
cout<<"继续添加信息吗?(Y or N)"<<endl;
cin >>ch;
if(ch=='Y' || ch == 'y')
flag=true;
e