几种数据结构

    研究了几种数据结构,并手写了一遍练练手.

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值