1、#include #include #include #include #include #include class CSalary private:char szName20; /姓名double dlSalary; /工资public:CSalary(); /默认构造CSalary(char *name,double salary); / 构造函数void SetSalary(char *name,double salary); /置工资与姓名double GetSal(); /返回工资int Compare(const CSalary /比较姓名,供查找用bool Find(char
2、*name); /寻找名字void Show(ostream ;class CNode /定义节点类private:CSalary *pData;/用于指向数据类的指针CNode *pNext; /指向链表的指针public:CNode()pData = NULL; pNext = NULL; /节点构造函数CNode(const CNode pNext = node.pNext; /用于拷贝的构造函数void InputData(CSalary *pSal)pData = pSal; /输入数据void ShowNode(ostream /显示CSalary *GetData()return
3、 pData;friend class CList; /定义 链表类的友元类;class CList /定义链表类protected:CNode *pHead; /链表头结点指针int num;public:CList()pHead = NULL; num = 0; /链表构造函数CList()DeleteList(); /链表的析构函数void AddNode(CNode *pnode); /在首部添加结点CNode *DeleteNode(CNode *pnode); /删除一个指定的结点,返回该结点的指针CNode *LookUp(CSalary /查找一个指定的数据,返回该数据所在结点
4、的链表中的指针void ShowList(ostream /打印整个链表void DeleteList(); /删除整个链表CNode *GetListHead(); /返回链表的首结点CNode *GetListNextNode(CNode *pnode); /返回链表的指定结点的下一个结点void Insert(CNode *pnode); /按工资顺序插入节点CSalary *Exist(char *name); /该员工信息;static char str256;/* CSalary的函数 */CSalary:CSalary() /默认构造函数strcpy (szName,“0“);
5、dlSalary = 0.0;CSalary:CSalary(char *name,double salary) /构造函数strcpy (szName,name);dlSalary = salary;void CSalary:SetSalary(char *name,double salary) / 置姓名与工资的函数strcpy (szName,name);dlSalary = salary;bool CSalary:Find(char *name) /用于查找是否有此人函数if (strcmp (szName,name) = 0)return true;elsereturn false;
6、double CSalary:GetSal() /返回工资的函数return dlSalary;void CSalary:Show(ostream elsereturn strcmp (szName,other.szName);/* 链表的函数 */ void CList:AddNode(CNode *pnode) /增加节点CNode *temp = pHead;pnode-pNext = temp;pHead = pnode;num +;CNode *CList:DeleteNode(CNode *pnode) /删除节点CNode *temp = pHead;if (temp = pno
7、de)pHead = pHead-pNext;delete temp-pData;delete temp;num -;return pnode;while (temp != NULL)if (temp-pNext = pnode)delete temp-pNext-pData;temp-pNext = pnode-pNext;delete pnode;num -;return pnode;temp = temp-pNext;return NULL;CNode *CList:LookUp(CSalary while (temp != NULL)if (temp-pData-Compare(oth
8、er) = 0)return temp;temp = temp-pNext;return NULL;void CList:ShowList(ostream while (temp != NULL)temp-pData-Show(os);os pNext;void CList:DeleteList() /删除链表CNode *temp = pHead;while (temp != NULL)pHead = pHead-pNext;delete temp-pData;delete temp;temp = pHead;pHead = NULL;CNode *CList:GetListHead() /
9、返回链表首节点 return pHead;CNode *CList:GetListNextNode(CNode *pnode)return pnode-pNext;void CList:Insert(CNode *pnode) /按工资顺序插入节点CNode *temp = pHead;if (pHead = NULL)AddNode(pnode);return;if (pHead-pData-Compare(*(pnode-pData) 0)AddNode(pnode);return;while (temp-pNext != NULL)if (temp-pNext-pData-Compare
10、(*(pnode-pData) 0)pnode-pNext = temp-pNext;temp-pNext = pnode;num +;return;temp = temp-pNext;temp-pNext = pnode;pnode-pNext = NULL;num +;CNode *MakeList(char *name,double salary) /创建出一个链表CNode *p = new CNode;CSalary *pp = new CSalary;pp-SetSalary(name,salary);p-InputData(pp);return p;CSalary *CList:
11、Exist(char *name) /链表,该员工信息CNode *temp = pHead;while (temp != NULL)if (temp-pData-Find(name)return temp-pData;temp = temp-pNext;return NULL;/* 主函数 */int main()CList head;CNode *temp;CSalary *one;fstream fin;fstream fout;char file1256,file2256;char name24;double salary;char str256;cout name salary;if
12、 (head.Exist(name) != NULL) continue;temp = MakeList(name,salary);head.Insert(temp);cout i;if(!cin.good() /判断输入问题,如字母等char str1100;cin.clear();cin.getline(str1,100);cout 5) /判断输入非规定数字char str1100;cin.clear();cin.getline(str1,100);cout salary;temp = MakeList(name,salary);head.Insert(temp);cout ShowNo
13、de();cout salary;while(!cin.good() /判断char buffer80;cout salary;cin.getline(buffer1,24);cout “确定要修改吗?(Y/y)“ endl;cin.getline(buffer1,24);if (strcmp (buffer1,“Y“) temp = head.LookUp(*one);head.DeleteNode(temp);temp = MakeList(name,salary);head.Insert(temp);cout “信息已经修改“ endl;system(“pause“); break;ca
14、se 5: cin.getline(buffer1,24);cout “输入你要删除的姓名:“;cin.getline(name,24);one = head.Exist(name);if (one = NULL)cout “无此人信息。“ endl;elsecout “确定要删除吗?(Y/y)“ endl;cin.getline(str,256);if (strcmp (str,“Y“) temp = head.LookUp(*one);head.DeleteNode(temp);cout “信息已经删除“ endl;system(“pause“);break;case 0:flag = false;break;default:cout “输入有误。“ endl;break;if(!flag) break;cin.getline(str,256);cout “是否保存到文件?(Y/y)“ ; /保存信息cin.getline(str,256);if (!(strcmp (str,“Y“) cin.getline (file2,256);fout.open(file2,ios:out);head.ShowList(fout);fout.close();cout “信息保存完毕。“ endl;head.DeleteList();return 0;