您的位置:首页 > 理论基础 > 数据结构算法

数据结构之线性表的顺序存储

2015-04-22 21:55 232 查看
为了复习下数据结构,最近开始着手捡捡原来学过的数据结构,从最简单的顺序存储的线性表开始...

代码很简单,对于自己来说起到一个督促作用

#include<stdio.h>
#include<string.h>

#define MAXSIZE 100
#define OK 1
#define ERROR 0

typedef struct
{
	char elem[MAXSIZE];
	int last;
}SeqList;

void InitList(SeqList *L)
{
	L->last = -1;
	printf("init is ok\n");
}

void DestroyList(SeqList *L)
{
	L->last = -1;
}

void ClearList(SeqList *L)
{
	L->last = -1;
}

int EmptyList(SeqList *L)
{
	if(L->last == -1)
		return ERROR;
	else
		return OK;
}

int ListLength(SeqList *L)
{
	return L->last;
}

char GetData(SeqList *L,int i)
{
	if(i < 0||i > L->last)
		return ERROR;
	else
		return L->elem[i];
}

int InsList(SeqList *L,int i,char data)
{
	int j;
	if(i < 0||i>L->last)
		return ERROR;
	else
	{
		for(j=ListLength(L)+1;j > i;j--)
			L->elem[j] = L->elem[j - 1];
		L->elem[j] = data;
		L->last++;
		return OK;
	}
}

int DelList(SeqList *L,int i)
{
	int j;
	if(i < 0||i>L->last)
		return ERROR;
	else
	{
		for(j = i;j < ListLength(L);j++)
			L->elem[j] = L->elem[j + 1];
		L->last-=1;
		return OK;
	}
}

void CreList(SeqList *L)
{
	int j = 0;
	char ch;
	for(j = 0;j < MAXSIZE; j++)
	{
		scanf("%c",&ch);
		if(ch == '#')
			break;
		else
			L->elem[j] = ch;
	}
	L->last = j-1;
	printf("the length = %d\n",j-1);
}

void PrintList(SeqList *L)
{
	int length;
	int i;
	length = ListLength(L);
	if(length <= 0)
		printf("there is on List!!!!\n");
	else
	{
		for(i = 0;i <= length;i++)
		{
			printf("%c",L->elem[i]);
		}
		printf("\n");
		//printf("\n");
	}
}

int main()
{
	SeqList List;
	int choice;
	int i;
	char e;
	printf("=============线性表练习==============\n");
	printf("选项:\n");
	printf("1初始化\n");
	printf("2销毁线性表\n");
	printf("3清除线性表\n");
	printf("4判断线性表是否为空\n");
	printf("5求线性表长度\n");
	printf("6得到某个位置的数据\n");
	printf("7在某个位置插入数据\n");
	printf("8删除某个位置的数据\n");
	printf("9创建线性表\n");
	printf("10打印当前线性表的所有数据\n");
	printf("输入你的选项:\n");
	scanf("%d",&choice);
	while(choice)
	{
		switch(choice)
		{
		case 1:
			InitList(&List);
			break;
		case 2:
			DestroyList(&List);
			break;
		case 3:
			ClearList(&List);
			break;
		case 4:
			if(EmptyList(&List) == OK)
				printf("no null\n");
			if(EmptyList(&List) == ERROR)
				printf("NULL\n");
			break;
		case 5:
			printf("length = %d\n",ListLength(&List));
			break;
		case 6:
			printf("input the number of the data:\n");
			scanf("%d",&i);
			e = GetData(&List,i);
			printf("data = %c\n",e);
			break;
		case 7:
			printf("input the number and the data:\n");
			scanf("%d%c",&i,&e);
			if(InsList(&List,i,e) == OK)
				printf("insert ok\n");
			else
				printf("insert failed\n");
			break;
		case 8:
			scanf("%d",&i);
			if(DelList(&List,i) == OK)
				printf("delete ok\n");
			else
				printf("delete failed\n");
			break;
		case 9:
			printf("input your data:\n");
			CreList(&List);
			break;
		case 10:
			PrintList(&List);
			break;
		}
		scanf("%d",&choice);
	}
	return 0;
}


个人总结:数据结构实现时的大体框架都是一样的,自己可以整理出一个自己的框架来,以后每次写程序时就可以和填空一样来填写代码,这样很节省自己的时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: