#include <iostream>
#include <cstdlib>
using namespace std;
/*
The implement of list by array.
Aha, I only want to pratice my English, my English is poor.
author:Jiawei Wu
*/
typedef struct alist *List;
typedef char ListItem;//Type of element of table.
typedef struct alist{
int n; //Length of list in pratice.
int maxsize; //Max length of list.
ListItem* table; //Elements which are consist of array.
} Alist;
/**
*Init the structure of List.
* param:
* size:The expect size of list space.
* return:
* Empty list.
*/
List ListInit(int size){
List L = (List)malloc(sizeof(Alist));
ListItem* table = (ListItem*)malloc(size*sizeof(ListItem));
L->table = table;
L->maxsize = size;
L->n = 0;
return L;
}
/**
*Check whethere list is empty.
*/
int ListEmpty(List L){
return L->n == 0;
}
/**
*Get the length of list.
*/
int ListLength(List L){
return L->maxsize;
}
/**
* Return: The Kth element of list. (K start at zero)
*/
ListItem ListRetrieve(int k, List L){
if (k<0 || k>=L->n){
cout<<"Out of bounds"<<endl;
exit(1);
}
return L->table[k];
}
/**
* Get the position of element x.
* Return:
* Non - -1:the position of x.
* -1: The element is not among list.
*/
int ListLocate(ListItem x,List L){
for(int i = 0;i < L->n;i++){
if(x == L->table[i]){
return i;
}
}
cout<<"Not found specified element"<<endl;
return -1;
}
/**
* Insert a element follow kth elements.
*/
void ListInsert(int k, ListItem x,List L){
int i;
if(k<0 || k>=L->n){
cout<<"Out of bounds"<<endl;
exit(1);
}
if(L->maxsize < L->n+1){
cout<<"Out of bounds"<<endl;
exit(1);
}
for(int i = L->n;i>k;i--){
L->table[i+1] = L->table[i];
}
L->table[k] = x;
L->n++;
}
/**
* Delete kth element from list.
*/
ListItem ListDelete(int k,List L){
int i;
if(k<0 || k>=L->n){
cout<<"Out of bounds"<<endl;
exit(1);
}
ListItem elementK = L->table[k];
for(int i = k;i<L->n;i++){
L->table[i] = L->table[i+1];
}
L->n--;
return elementK;
}
/**
* Print the list by order.
*/
void PrintList(List L){
for(int i = 0;i < L->n;i++){
cout<<L->table[i]<<' ';
}
cout<<endl;
}
int main(){
//Do anything which you want to, good luck.
return 0;
}
祝你好运~