055.链表操作(2)

#include "stdafx.h"
#include "stdio.h"
#ifndef SQLIST_H
#define SQLIST_H
#include <stdlib.h>
#define List_INIT_SIZE  100   //线性表的存储空间初始大小
#define LIST_INCREMENT  10    //分配增量
#define OVERFLOW   -2
#define OK     1
#define ERROR    -1
#define TRUE    1
#define FALSE    0
typedef int  ElemType;
typedef struct{
	ElemType *elem;  //存储空间基址
	int   length;  //当前长度
	int   size;    //当前存储容量(sizeof(ElemType)为单位)
}SqList;
int InitList(SqList &L)
{
	//构建一个线性表L
	L.elem = (ElemType*)malloc(List_INIT_SIZE*sizeof(ElemType));
	if(!L.elem)
	{
		exit(OVERFLOW);
	}
	L.length = 0;
	L.size = List_INIT_SIZE;
	return OK;
}//InitList

void DestoryList(SqList &L)
{
	if(!L.elem)
	{
		delete L.elem;
		L.elem = NULL;
	}
	L.length = 0;
	L.size = 0;
}//DestoryList

void ClearList(SqList &L)
{
	L.elem = NULL;
	L.length = 0;
	L.size = List_INIT_SIZE;
}//ClearList

int ListEmpty(SqList L)
{
	if(L.length > 0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}//ListEmpty

int ListLength(SqList L)
{
	return L.length;
}//ListLength

int ListInsert(SqList &L, int i, ElemType e)
{
	if(i<1 || i>ListLength(L)+1) //需满足条件1<i<ListLength(L)+1
	{
		return ERROR;
	}
	ElemType * NewBase;  //新的基址
	if(L.length > L.size) //空间已满
	{
		NewBase = (ElemType*)malloc((L.size+LIST_INCREMENT)*sizeof(ElemType));  //申请新的空间
		if(!NewBase)
		{
			exit(OVERFLOW);
		}
		L.elem = NewBase;
		L.size +=LIST_INCREMENT;
		delete NewBase;
		NewBase = NULL;
	}
	ElemType *p;
	ElemType *temp;
	p = &(L.elem[i-1]);  //取得i的位置,即插入位置
	for(temp = &(L.elem[L.length-1]);temp>p;--temp)  //将插入点后的所有元素向后移动一位
	{
		*(temp+1) = *temp;
	}
	*p = e;
	++L.length;
	return OK;
}//ListInsert

int ListDelete(SqList &L, int i, ElemType &e)
{
	if (i<0 || i>ListLength(L)) {
		return ERROR;
	}
	ElemType *p;
	p = &(L.elem[i-1]);
	e = *p;
	ElemType *pos;
	pos =&(L.elem[L.length-1]);
	for(++p; p<=pos; ++p){
		*(p-1) = *p;
	}
	--L.length;
	return OK;
}
#endif //SQLIST_H
int main(){
	SqList list;
	ElemType e;
	InitList(list);
	ListInsert(list, 1, 1);
	ListInsert(list, 2, 2);
	ListInsert(list, 3, 3);
	ListInsert(list, 4, 4);
	ListInsert(list, 5, 5);
	for(int i =0; i<list.length; i++){
		printf("%d\n",list.elem[i]);
	}
	ListDelete(list, 2, e);
	for(i =0; i<list.length; i++){
		printf("%d\n",list.elem[i]);
	}	printf("%d\n", list.length);
	
	return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程与实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值