#include "DataStore.h"
#include<stdlib.h>
#include<stdio.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;
Student* pre = &m_head;
while (cur) {
if (copy->id < cur->id)
break;
pre = cur;
cur = cur->next;
}
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 = m_head.next;
while (p) {
printf("ID:%d,name:%s\n", p->id, p->name);
p = p->next;
}
}
struct Student {
int id;
char name[32];
Student* next;
};
class DataStore {
public:
DataStore();
~DataStore();
public:
void Add(const Student* data);
Student* Find(int id);
void Print();
private:
Student m_head;
};
#include<stdio.h>
#include<string.h>
#include"DataStore.h"
int main() {
DataStore store;
Student stu;
stu.id = 2018;
strcpy(stu.name, "2018");
store.Add(&stu);
store.Print();
return 0;
}