2.2王晓东数据结构—用数组实现顺序表

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

祝你好运~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值