#include "iostream.h"
#define MAX 100
typedef int ElemType; //顺åºè¡¨å
ç´ çæ°æ®ç±»å
//顺åºè¡¨çåå¨ç»æ
typedef struct STable
{
ElemType base[MAX]; //顺åºè¡¨çåå¨ç©ºé´
int length; //顺åºè¡¨çé¿åº¦
}STable;
//å¨é¡ºåºè¡¨stçindexä½ç½®æå
¥å¼ä¸ºdataçå
ç´ ï¼è¿åæå
¥æåä¸å¦çæ å¿
bool AddAt(STable *st, int index, ElemType data)
{
for(int i=st->length-1; i>=index; i--)
st->base[i+1] = st->base[i];
st->base[index] = data;
st->length++;
return true;
}
//å é¤é¡ºåºè¡¨stä¸indexä½ç½®çå
ç´ ï¼è¿åå é¤æåä¸å¦çæ å¿
bool RemoveAt(STable *st, int index, ElemType *data)
{
*data = st->base[index];
for(int i=index+1; i<st->length; i++)
st->base[i-1] = st->base[i];
st->length--;
return true;
}
void main()
{
int i;
STable *st;
ElemType data;
st = new STable();
st->length = 5;
for(i=0; i<st->length; i++)
st->base[i] = 2* i + 1;
for(i=0; i<st->length; i++)
cout<<st->base[i]<<" ";
cout<<endl;
AddAt(st, 2, 4);
cout<<"æå
¥å
ç´ åï¼"<<endl;
for(i=0; i<st->length; i++)
cout<<st->base[i]<<" ";
cout<<endl;
RemoveAt(st, 2, &data);
cout<<"å é¤å
ç´ åï¼"<<endl;
for(i=0; i<st->length; i++)
cout<<st->base[i]<<" ";
cout<<endl;
}
温馨提示:内容为网友见解,仅供参考