线性表顺序存储结构

// onlinejudge.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define CardNumber 13
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20/**/
typedef struct {
	int data[MAXSIZE];/**/
	int length;
}SqList;

/*插入操作*/
int ListInsert(SqList *L,int i,int e)
{
	int k;
	if(L->length==MAXSIZE)/**/
		return ERROR;
	if(i<1||i>L->length+1)
		return ERROR;
	if(i<=L->length)
	{
		for (k=L->length-1;k>=i-1;k--)/**/
			L->data[k+1]=L->data[k];
	}
	L->data[i-1] = e;
	L->length++;
	return OK;

}

/*删除操作*/
int ListDelete(SqList *L,int i,int *e)
{
	int k;
	if(L->length==0)/*线性表为空*/
		return ERROR;
	if(i<1||i>L->length)/*删除位置不正确*/
		return ERROR;
	*e = L->data[i-1];
	if(i<L->length)
	{
		for(k=i;k<L->length;k++)
			L->data[k-1]=L->data[k];
	}
	L->length--;
	return OK;
}




int _tmain(int argc, _TCHAR* argv[])
{
	
	SqList sqlist={{1,2,3,4,5,6,7,9,10},9};/*初始化一个线性表*/
	
	//ListInsert(&sqlist,8,8);
	SqList *p = &sqlist;
 
	int e;
	ListInsert(p,8,8);
	cout<<sqlist.data[0]<<endl;
	cout<<sqlist.data[1]<<endl;
	cout<<sqlist.data[7]<<endl;
	ListDelete(p,9,&e);
	cout<<sqlist.length<<endl;
    return 0;
}

  

posted @ 2017-07-20 23:52  dreamafar  阅读(186)  评论(0编辑  收藏  举报