DataStore.h
struct Student { int id; char name[16]; Student* next; }; class DataStore { public: DataStore(); ~DataStore(); public: void Add(const Student* data); Student* Find(int id); void Print(); private: Student m_head; };
DataStore.cpp
#include <stdio.h> #include <stdlib.h> #include "DataStore.h" DataStore::DataStore() { m_head.next = NULL; } DataStore::~DataStore() { Student* p = m_head.next; while(p) { Student* next = p->next; free(p); p = next; } } void DataStore::Add(const Student* data) { // 创建对象、复制数据 Student* copy = (Student*)malloc(sizeof(Student)); *copy = *data; // 插入一个对象到链表中 Student* cur = m_head.next; // 当前节点current Student* pre = &m_head; // 上一个节点previous while(cur) { if(copy->id < cur->id) // 找到这个位置 break; pre = cur; cur = cur->next; // 找到最后一个对象 } // 插入到pre节点的后面 copy->next = pre->next; pre->next = copy; } Student* DataStore::Find(int id) { Student* p = m_head.next; while(p) { if(p->id == id) return p; p = p->next; // 下一个对象 } return NULL; } void DataStore::Print() { Student* p = this->m_head.next; while(p) { printf("ID: %d, name: %s\n", p->id, p->name); p = p->next; // 下一个对象 } }