
程序代码:
#include <windows.h>
#include <iostream>
using namespace std;
class Staff
{
friend class Staffs;
public:
Staff(LPSTR id, LPSTR name, Staff *next=NULL);
~Staff();
private:
LPSTR ID;
LPSTR Name;
Staff *Next;
};
Staff::Staff(LPSTR id, LPSTR name, Staff *next)
{
HANDLE heapHandle;
//获取进程堆句柄
heapHandle = GetProcessHeap();
if (NULL == heapHandle)
{
cout << "GetProcessHeap failed!" << endl;
ExitProcess(0);
}
//初始化ID
ID = (LPSTR) HeapAlloc (heapHandle, HEAP_ZERO_MEMORY, lstrlen(id)+1);
CopyMemory(ID, id, lstrlen(id));
//初始化Name
Name = (LPSTR) HeapAlloc (heapHandle, HEAP_ZERO_MEMORY, lstrlen(name)+1);
CopyMemory(Name, name, lstrlen(name));
//初始化Next
Next = next;
}
Staff::~Staff()
{
HANDLE heapHandle;
//获取进程堆句柄
heapHandle = GetProcessHeap();
if (NULL == heapHandle)
{
cout << "GetProcessHeap failed!" << endl;
ExitProcess(0);
}
if (!HeapFree(heapHandle, HEAP_NO_SERIALIZE, ID))
{
cout << "HeapFree failed" << endl;
ExitProcess(0);
}
if (!HeapFree(heapHandle, HEAP_NO_SERIALIZE, Name))
{
cout << "HeapFree failed" << endl;
ExitProcess(0);
}
}
class Staffs
{
public:
Staffs();
~Staffs();
VOID Insert(Staff *node);
VOID Sort(VOID);
LPSTR Query(LPSTR id);
VOID Print(VOID);
private:
Staff *Head;
};
Staffs::Staffs()
{
Head = NULL;
}
Staffs::~Staffs()
{
Staff *temp = Head;
while (NULL != Head)
{
temp = Head;
Head = Head->Next;
delete temp;
}
Head = NULL;
}
VOID Staffs::Insert(Staff *node)
{//插入到头部
node->Next = Head;
Head = node;
}
VOID Staffs::Sort()
{//
Staff *temp = NULL, *p = NULL;
Staff *list = NULL;
BOOL flag = FALSE;
while (NULL != Head)
{
temp = Head;
if (temp == NULL || temp->Next == NULL)
{
temp->Next = list;
list = temp;
break;
}
else
{
}
}
}
VOID Staffs::Print()
{
Staff *temp = Head;
while (temp != NULL)
{
cout << "\tID:" << temp->ID << ' '
<< "\tName:" << temp->Name << endl;
temp = temp->Next;
}
}
LPSTR Staffs::Query(LPSTR id)
{
Staff *temp = Head;
while (temp != NULL)
{
if (lstrcmp(id, temp->ID)==0)
{
return temp->Name;
}
temp = temp->Next;
}
return NULL;
}
INT main(VOID)
{
Staffs staffs;
Staff *node;
CHAR NameBuffer[20] = {0};
CHAR IdBuffer[20] = {0};
for (SIZE_T sum=0; sum<2; ++sum)
{
cout << "ID: " ; cin >> IdBuffer;
cout << "Name: "; cin >> NameBuffer;
node = new Staff(IdBuffer, NameBuffer);
staffs.Insert(node);
}
cout << "\t查询ID:1234 " << ' ' << "查询结果: "
<< staffs.Query("1234") << endl;
staffs.Print();
return 0;
}
//排序没
