Data Structure(with C).Experiment01.01

============algo2-1.cpp============

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 6

typedef struct{
	char data[MaxSize];
	int length;
}SqList;

void InitList(SqList *&L){
	L=(SqList *)malloc(sizeof(SqList));
	L->length=0;
}

void DestroyList(SqList *&L){
	free(L);
}

bool ListEmpty(SqList *&L){
	return (L->length==0);
}

int ListLength(SqList *&L){
	return (L->length);
}

void DispList(SqList *&L){
	int i=0;
	for(i;i<L->length;i++)
		printf("%c ",L->data[i]);
	printf("\n");
}

bool GetChar(SqList *&L,int i,char &e){
	if(i<1||i>L->length)
		return false;
	e=L->data[i-1];
	return true;
}

int LocateChar(SqList *&L,char e){
	int i=0;
	while(i<L->length&&L->data[i]!=e)
		i++;
	if(i>=L->length)
		return 0;
	else
		return i+1;
}

bool ListInsert(SqList *&L,int i,char e){
	int j;
	if(i<1||i>L->length+1)
		return false;
	i--;
	for(j=L->length;j>i;j--)
		L->data[j]=L->data[j-1];
	L->data[i]=e;
	L->length++;
	return true;
}

bool ListDelete(SqList *&L,int i){
	int j;
	if(i<1||i>L->length)
		return false;
	i--;
	for(j=i;j<L->length-1;j++)
		L->data[j]=L->data[j+1];
	L->length--;
	return true;
}

void ListTialIns(SqList *&L,char e){
	L->data[L->length]=e;
	L->length++;
}


==========exp2-1============

#include "algo2-1.cpp"
void main(){
	SqList *L;
	char e;
	InitList(L);
	ListTialIns(L,'a');
	ListTialIns(L,'b');
	ListTialIns(L,'c');
	ListTialIns(L,'d');
	ListTialIns(L,'e');
	DispList(L);
	printf("The list-length is:%d\n",ListLength(L));
	if(ListEmpty(L))
		printf("The list is empty!\n");
	else
		printf("The list is not empty!\n");
	if(GetChar(L,3,e))
		printf("The third element of the list is:%c\n",e);
	else
		printf("The element is not found!\n");
	printf("The element a's location is:%d\n",LocateChar(L,'a'));
	ListInsert(L,4,'f');
	DispList(L);
	ListDelete(L,3);
	DispList(L);
	DestroyList(L);
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值