栈的线性存储

栈的特性是先入后出,下面给出栈的线性存储:


下面是线性栈和线性链表的头文件:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

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


typedef void SeqList;
typedef void SeqListNode;

SeqList * SeqList_Create(int capacity);//创建链表 Capacity 为链表的容量

void SeqList_Destroy(SeqList* list);  //析构链表 FREE内存

void SeqList_Clear(SeqList* list);  //清空链表

int SeqList_Length(SeqList* list);//求链表的长度

int SeqList_Capacity(SeqList* list);//链表的容量

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);//插入 

SeqListNode* SeqList_Get(SeqList* list, int pos);//获取

SeqListNode* SeqList_Delete(SeqList* list, int pos);//删除



#endif

#ifndef __STACK_H__
#define __STACK_H__

#include "stdio.h"
#include "stdlib.h"
#include "seqlist.h"

typedef  void SeqStack;
typedef void SeqStackNode;

SeqStack * SeqStack_Create(int capacity);
void SeqStack_Destroy(SeqStack * stack);
void SeqStack_Clear(SeqStack * stack);
int SeqStack_Length(SeqStack * stacck);
int SeqStack_Capacity(SeqStack * stack);
int SeqStack_Push(SeqStack * stack, SeqStackNode * node);
SeqStack * SeqStack_Pop(SeqStack *stack);
SeqStack * SeqStack_Delete(SeqStack * stack, int pos);
SeqStack * SeqStack_Top(SeqStack * stack);






#endif

下面是头文件中函数的实现:

#include "seqlist.h"


typedef struct TSeqList
{
	int length;
	int capacity;
	unsigned int *node;
}TSeqList;


SeqList * SeqList_Create(int capacity)
{
	if (capacity == 0)
	{
		return NULL;
	}
	TSeqList *list = NULL;
	list = (TSeqList *)malloc(sizeof(TSeqList));
	if (list == NULL)
	{
		return NULL;
	}
	memset(list, 0, sizeof(TSeqList));


	list->capacity = capacity;
	list->length = 0;

	list->node = (unsigned int *)malloc(sizeof(unsigned int )*capacity);
	return (SeqList *)list;

}

void SeqList_Destroy(SeqList* list)
{
	if (list == NULL)
	{
		return;
	}

	TSeqList * tmp = NULL; 
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return;
	}
	if (tmp->node != NULL)
	{
		free(tmp->node);
	}
	free(list);
	return;
}

void SeqList_Clear(SeqList* list)
{
	if (list == NULL)
	{
		return;
	}

	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return;
	}
	tmp->length = 0;
	return;

}

int SeqList_Length(SeqList* list)
{
	if (list == NULL)
	{
		return 0;
	}

	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return 0;
	}
	int ret = 0;
	ret = tmp->length;
	return ret;
}

int SeqList_Capacity(SeqList* list)
{
	TSeqList * tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return 0;
	}
	return tmp->capacity;
}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int ret = 0;
	if (list == NULL || node == NULL  || pos<0)
	{
		ret = -1;
		return ret;
	}

	
	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		ret = -2;
		return ret;
	}

	if (tmp->length >= tmp->capacity)//检查是否已满
	{
		ret = -3;
		return ret;
	}

	if (pos >= tmp->length)//位置修正
	{
		pos = tmp->length;
	}

	for (int i = tmp->length; i > pos; i--)//数组中元素后移
	{
		tmp->node[i] = tmp->node[i - 1];
	}
	tmp->node[pos] = (unsigned int)node;
	tmp->length++;

	return ret;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	if (list == NULL  || pos<0)
	{
		return NULL;
	}


	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return NULL;
	}


	return (SeqListNode*)(tmp->node[pos]);
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	if (list == NULL || pos<0)
	{
		return NULL;
	}
	
	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return NULL;
	}

	SeqListNode * ret = (SeqListNode *)tmp->node[pos];//缓存删除的节点
	for (int i = pos+1; i < tmp->length; i++)
	{
		tmp->node[i-1] = tmp->node[i];
	}
	(tmp->length)--;
	return ret;
}

#include "stack.h"

SeqStack * SeqStack_Create(int capacity)
{
	return (SeqStack *)SeqList_Create(capacity);
}
void SeqStack_Destroy(SeqStack * stack)
{
	SeqList_Destroy((SeqList *)stack);
	return;
}
void SeqStack_Clear(SeqStack * stack)
{
	SeqList_Clear((SeqList *)stack);
	return;
}
int SeqStack_Length(SeqStack * stack)
{
	return SeqList_Length((SeqList *)stack);
}
int SeqStack_Capacity(SeqStack * stack)
{
	return SeqList_Capacity((SeqList *)stack);
}
int SeqStack_Push(SeqStack * stack, SeqStackNode * node)
{
	SeqList_Insert((SeqList *)stack, (SeqListNode *)node, SeqList_Length((SeqList *)stack));
	return 0;
}
SeqStack * SeqStack_Pop(SeqStack *stack)
{
	SeqListNode * tmp = SeqList_Delete((SeqList *)stack, SeqList_Length((SeqList *)stack)-1);
	return (SeqStack *)tmp;
}
SeqStack * SeqStack_Delete(SeqStack * stack, int pos)
{
	return NULL;
}
SeqStack * SeqStack_Top(SeqStack * stack)
{
	SeqListNode * tmp = SeqList_Get((SeqList *)stack, 0);
	return (SeqStack *)tmp;
}

下面是测试程序:

#include "stack.h"
typedef struct Teacher
{
	SeqStackNode * node;
	int age;

}Teacher;
void main()
{
	Teacher t1, t2, t3, t4;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;
	t4.age = 44;
	SeqStack *stack = NULL;
	stack = SeqStack_Create(10);
	if (stack == NULL)
	{
		goto END;
	}

	int length = SeqStack_Length(stack);
	printf("未插入时的长度为:%d\n", length);

	int ret = 0;
	ret = SeqStack_Push(stack, (SeqStackNode *)(&t4));
	if (ret != 0)
	{
		goto END;
	}
	ret = SeqStack_Push(stack, (SeqStackNode *)(&t3));
	if (ret != 0)
	{
		goto END;
	}
	ret = SeqStack_Push(stack, (SeqStackNode *)(&t2));
	if (ret != 0)
	{
		goto END;
	}
	ret = SeqStack_Push(stack, (SeqStackNode *)(&t1));
	if (ret != 0)
	{
		goto END;
	}

	length = SeqStack_Length(stack);
	printf("插入后的长度:%d\n", length);

	int capacity = SeqStack_Capacity(stack);
	printf("栈的容量:%d\n", capacity);

	Teacher * tmp = NULL;
	tmp = (Teacher *)SeqStack_Top(stack);
	if (tmp == NULL)
	{
		goto END;
	}
	printf("栈顶元素%d\n", tmp->age);

	while (SeqStack_Length(stack) > 0)
	{
		tmp = (Teacher *)SeqStack_Pop(stack);
		if (tmp == NULL)
		{
			goto END;
		}
		printf("出栈元素 %d\n", tmp->age);
	}

	SeqStack_Clear(stack);
	SeqStack_Destroy(stack);





END:

	system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值