顺序表——基于C语言

最近复习了C语言的相关知识,实现了数据结构中的顺序表。

下面是Main.c文件:

#include "SeqList.h"
#include <stdio.h>

int main()
{
	SeqList* list = SeqListCreate();
	 
	if(list != NULL)
	{
	 	printf("顺序表容量为:%d\n", SeqListCapacity(list));
	  	printf("顺序表长度为:%d\n", SeqListLength(list));
	  
	  	printf("插入状态:%d\n", SeqListInsert(list, 0, 0));
	  	printf("插入状态:%d\n", SeqListInsert(list, 1, 1));
	  	printf("插入状态:%d\n", SeqListInsert(list, 2, 2));
	  	printf("插入状态:%d\n", SeqListInsert(list, 3, 3));
	  	printf("插入状态:%d\n", SeqListInsert(list, 9, 4));
	  
	  	printf("此时顺序表长度为:%d\n", SeqListLength(list));
	  
	  	SeqListDelete(list, 0, NULL); 
	  	SeqListDelete(list, 0, NULL);
	  
	  	printf("此时顺序表长度为:%d\n", SeqListLength(list));
	  
	  	printf("删除状态:%d\n", SeqListDelete(list, 1, NULL)); 
	  	printf("此时顺序表长度为:%d\n", SeqListLength(list));
	  
	  	list = SeqListDestroy(list); 
	}
	
	return 0;
}

下面是SeqList.h文件:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

typedef int DataType;

#define deBug() printf("File = %s\nLine = %n", __FILE__, __LINE__)

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100

#ifndef __cplusplus
 	typedef int Bool;
 	#define true 1
 	#define false 0
#else
 	typedef bool Bool;
#endif

#define MALLOC(type, size)  (type*)malloc(sizeof(type) * size)
#define FREE(p)    (free(p), p = NULL)

typedef struct
{
 	DataType space[MAXSIZE];
 	int size;
} SeqList;

SeqList* SeqListCreate(void);
SeqList* SeqListDestroy(SeqList *list);
int SeqListLength(SeqList *list);
int SeqListCapacity(SeqList *list);
Bool SeqListInsert(SeqList *list, int i, DataType data);
Bool SeqListDelete(SeqList *list, int i, DataType *data);
Bool SeqListSet(SeqList *list, int i, DataType data);
Bool SeqListGet(SeqList *list, int i, DataType *data);

#endif

下面是SeqList.c文件:

#include "SeqList.h"

SeqList* SeqListCreate(void)
{
 	SeqList *ret = MALLOC(SeqList, 1);
 
 	if(ret != NULL) ret->size = 0;
 
 	return ret;
}

SeqList* SeqListDestroy(SeqList *list)
{ 
 	return FREE(list);
}

int SeqListLength(SeqList *list)
{
 	return (list != NULL) ? list->size : -1;
}

int SeqListCapacity(SeqList *list)
{
 	return (list != NULL) ? MAXSIZE : -1;
}

Bool SeqListInsert(SeqList *list, int i, DataType data)
{
 	Bool ret = (list != NULL) && (i >= 0) && (i <= list->size) && (list->size < MAXSIZE);
 
 	if(ret)
 	{
  		int j;
  
  		for(j = list->size - 1; j >= i; j--)
  		{
   			list->space[j + 1] = list->space[j];
  		}
  
  		list->space[j] = data;
  
  		list->size++;
 	}
 
 	return ret;
}

Bool SeqListDelete(SeqList *list, int i, DataType *data)
{
 	Bool ret = (list != NULL) && (i >= 0) && (i < list->size);
 
 	if(ret)
 	{ 
  		if(data != NULL) *data = list->space[i];
  
  		for(; i < list->size - 1; i++)
  		{
   			list->space[i] = list->space[i + 1]; 
  		}
  
  		list->size--;
 	}
 
 	return ret;
}

Bool SeqListSet(SeqList *list, int i, DataType data)
{
 	return ((list != NULL) && (i >= 0) && (i < list->size)) ? (list->space[i] = data, true) : false;
}

Bool SeqListGet(SeqList *list, int i, DataType *data)
{ 
 	return ((list != NULL) && (i >= 0) && (i < list->size) && (data != 0)) ? (*data = list->space[i], true) : false;
}

不知道什么原因,使用Dev c++编译这个程序会在free掉顺序表时出错。改使用Qt重新编译,无错误无警告。下面是运行结果:

在这里插入图片描述

数据结构基于C语言实现的顺序表. #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100/* 定义二叉树节点类型 */ typedef struct node { char data; struct node *lchild, *rchild; }BTNode; BTNode* CreatBitTree()/* 递归前序建立二叉树 */ { char c; BTNode *T; scanf("%c", &c); if (c == ' ') /* 遇到空节点停止递归 */ { T = NULL; } else { T = (BTNode*) malloc(sizeof(BTNode)); T->data = c;/* 建立根节点 */ T->lchild = CreatBitTree();/* 递归先序建立左子树 */ T->rchild = CreatBitTree();/* 递归先序建立右子树 */ } return T; } void PreOrder(BTNode* T)/* 非递归前序遍历二叉树 */ { BTNode *stack[MAXSIZE], *p; int top = 0; if (T != NULL) { //top++;/* 根节点入栈 */ stack[top] = T; while (top > -1)/* 栈不空时循环 */ { p = stack[top];/* 出栈并访问该节点 */ top--; printf("%c ", p->data); if (p->rchild != NULL)/* 右孩子入栈 */ { top++; stack[top] = p->rchild; } if (p->lchild != NULL)/* 左孩子入栈 */ { top++; stack[top] = p->lchild; } } printf("\n"); } } void PrintTree(BTNode* T,int nLayer) //按竖向树状打印的二叉树 { int i; if(T==NULL) return ; PrintTree(T->rchild,nLayer+1); for(i=0;i<nLayer;i++) printf(" "); printf("%c\n",T->data); PrintTree(T->lchild,nLayer+1); } int main()/* 主函数 */ { BTNode *r = NULL; printf("请先序输入二叉树:(如:AB 三个空格表示A为根结点,B为左子树的二叉树)\n"); r = CreatBitTree(); printf("按竖向树状打印的二叉树:\n"); PrintTree( r,0); printf("先序非递归遍历二叉树:"); PreOrder(r); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值