标签:静态列表
#ifndef __STATICLIST_ #define __STATICLIST_ #include<iostream> using namespace std; #define MAXSIZE 8 #define ElemType int typedef struct StaticListNode { ElemType data; int cur; }StaticListNode; typedef StaticListNode StaticList[MAXSIZE]; void InitStaticList(StaticList &SL) { SL[0].cur = -1; for(int i=1; i<MAXSIZE-1; ++i) { SL[i].cur = i+1; } SL[MAXSIZE-1].cur = -1; } int Malloc_SL(StaticList &SL) { if(SL[1].cur == -1) return -1; int i = SL[1].cur; SL[1].cur = SL[i].cur; return i; } void push_back(StaticList &SL, ElemType x) { int i = Malloc_SL(SL); if(i == -1) return; SL[i].data = x; SL[i].cur = -1; int j = 0; while(SL[j].cur != -1) { j = SL[j].cur; } SL[j].cur = i; } void push_front(StaticList &SL,ElemType x) { int i = Malloc_SL(SL); if(i == -1) return; SL[i].data = x; int j=SL[0].cur; SL[0].cur=i; SL[i].cur = j; } void pop_back(StaticList &SL) { int i=SL[0].cur; int j=SL[i].cur; while(SL[j].cur!=-1) { i=SL[i].cur; j=SL[j].cur; } SL[i].cur=-1; SL[j].data=NULL; } void pop_front(StaticList &SL) { int i=SL[0].cur; SL[0].cur=SL[i].cur; SL[i].data=NULL; } void show_list(StaticList &SL) { for(int i=SL[0].cur; SL[i].cur!=-1; i=SL[i].cur) { cout<<SL[i].data<<"-->"; } cout<<SL[i].data<<"-->"<<"OK!"<<endl; } #endif
测试代码
#include"StaticList.h" void main() { StaticList SL; InitStaticList(SL); for(int i=1; i<6; i++) { //push_front(SL,i); push_back(SL,i); } //push_front(SL,9); //push_back(SL,78); show_list(SL); // pop_back(SL); pop_front(SL); show_list(SL); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:静态列表
原文地址:http://blog.csdn.net/fujinlong520/article/details/48084895