研究了几种数据结构,并手写了一遍练练手.
Queue:
#include<cstdio>
struct FQueue{
struct DataPack{
int iData;
DataPack *p;
DataPack(int _iData=0,DataPack *_p=NULL){iData=_iData;p=_p;}
};
DataPack *QueueTop,*Now;
FQueue(){QueueTop=NULL;Now=NULL;}
bool isBlank(){return QueueTop==NULL;}
void pop(){if(QueueTop)QueueTop=QueueTop->p;}
void push(int Data){
DataPack *np=new DataPack(Data,NULL);
if(isBlank())QueueTop=np;
if(Now)Now->p=np;
Now=np;
}
int top(){
return QueueTop->iData;
}
}test;
int main(){
int iAmount=0;
while(iAmount!=10)test.push(iAmount++);
while(iAmount--){
printf("%d\n",test.top());
test.pop();
}
return 0;
}
Stack:
#include<cstdio>
struct FStack{
struct DatPack{
int iData;
DatPack *p;
DatPack(int _iData=0,DatPack *_p=NULL){iData=_iData;p=_p;}
};
DatPack *Top,*End;
FStack(){Top=NULL;End=NULL;}
bool isBlank(){return Top==NULL;}
void pop(){if(!isBlank())Top=Top->p;}
void push(int Data){
DatPack *np=new DatPack(Data,Top);
Top=np;
}
int GTop(){return Top->iData;}
}test;
int main(){
int iAmount=0;
while(iAmount!=10)test.push(iAmount++);
while(iAmount--){
printf("%d ",test.GTop());
test.pop();
}
printf("\n");
return 0;
}
Linklist:
#include<cstdio>
struct LinkControl{
struct DatPack{
DatPack(int _data=0,DatPack *_pl=NULL,DatPack *_pn=NULL){Data=_data;pl=_pl;pn=_pn;}
DatPack *pl,*pn;
int Data;
}*Now,*New;
int Amount,Process;
LinkControl(){Now=NULL;New=NULL;Amount=0;Process=0;}
bool next(){return Now&&Now->pn&&(Now=Now->pn,Process++,1);}
bool last(){return Now&&Now->pl&&(Now=Now->pl,Process--,1);}
bool Goto(int index){while((Process<index&&next())||(Process>index&&last()));return Process==index;}
int getData(int index){return Goto(index)?Now->Data:-404;}
void insert(int Data,int index=-1){
Goto(~index?index:Amount);
New=new DatPack(Data,Now,(Now?Now->pn:NULL));
(Now?Now->pn:Now)=New;
Amount++;
}
}Test;
int main(){
int i=0;
while(i++<10){
Test.insert(i);
printf("%d ",i);
}
printf("\n");
return 0;
}