#pragma once
#ifndef _STOREDCHAIN_H_
#define _STOREDCHAIN_H_
#include <assert.h>
struct Student
{
long ID_number;
char name[12];
Student& operator=(long k1)
{
ID_number=k1;
return *this;
}
bool operator<(long k1) {return (ID_number<k1) ? true : false;}
bool operator>(long k1) {return (ID_number>k1) ? true : false;}
bool operator==(long k1) {return (ID_number==k1) ? true : false;}
bool operator<(const Student& pr) {return (ID_number<pr.ID_number) ? true : false;}
bool operator>(const Student& pr) {return (ID_number>pr.ID_number) ? true : false;}
bool operator==(const Student& pr) {return (ID_number==pr.ID_number) ? true : false;}
};
struct ChainNode
{
Student data;
ChainNode *link;
ChainNode() : link(NULL) { }
ChainNode(Student& e1, ChainNode *next=NULL) : data(e1), link(next) { }
};
class StoredChain
{
public:
StoredChain() {first=new ChainNode; assert(first!=NULL);}
~StoredChain() {makeEmpty(); delete first;}
ChainNode *Search(const long k1) const //搜索;
{
ChainNode *p=first->link;
while(p!=NULL&&p->data<k1) p=p->link;
if(p!=NULL&&p->data==k1) return p;
else return NULL;
}
void Insert(const long k1, Student& e1)
{
ChainNode *p=first->link, *pre=first, *newNode;
while(p!=NULL&&p->data<k1) {pre=p; p=p->link;}
if(p!=NULL&&p->data==k1) {p->data=e1; return;}
newNode=new ChainNode(e1);
assert(newNode!=NULL);
newNode->link=p; pre->link=newNode;
}
bool Remove(const long k1, Student& e1)
{
ChainNode *p=first->link, *pre=first;
while(p!=NULL&&p->data<k1) {pre=p; p=p->link;}
if(p!=NULL&&p->data==k1)
{
pre->link=p->link; e1=p->data;
delete p;
return true;
}
else return false;
}
ChainNode *Begin() {return first->link;}
ChainNode *Next(ChainNode *current) const
{
if(current!=NULL) return current->link;
else return NULL;
}
private:
void makeEmpty()
{
ChainNode *q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
}
private:
ChainNode *first;
};
#endif
字典的线性表描述
最新推荐文章于 2022-11-20 19:30:56 发布