数据结构源码(二)栈ADT

栈ADT结构是LIFO(Last Input First Output,后进先出)。

一、栈的链表形式

使用链表的形式即限制了链表的(入栈和出栈)访问只能在尾结点进行。

#include <iostream>
using namespace std;

typedef int ElementType;

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

struct Node
{
	ElementType Element;
	PtrToNode Next;
};

int IsEmpty(Stack S)
{
	return S->Next == NULL;
}

Stack CreateStack(void)
{
	Stack S;

	S = (Stack)malloc(sizeof(struct Node));
	if (S == NULL)
		cerr << "Out of space" << endl;
	S->Next = NULL;
	MakeEmpty(S);
	return S;
}

void MakeEmpty(Stack S)
{
	if (S == NULL)
		cerr << "Must use CreateStack first" << endl;
	else
		while (!IsEmpty(S))
			Pop(S);
}

void Push(ElementType X, Stack S)
{
	PtrToNode TmpCell;

	TmpCell = (PtrToNode)malloc(sizeof(struct Node));
	if (TmpCell == NULL)
		cerr << "Out of space" << endl;
	else
	{
		TmpCell->Element = X;
		TmpCell->Next = S->Next;
		S->Next = TmpCell;
	}
}

ElementType Top(Stack S)
{
	if (!IsEmpty(S))
		return S->Next->Element;
	else
		cerr << "Empty stack" << endl;
	return 0;
}

void Pop(Stack S)
{
	PtrToNode FirstCell;

	if (!IsEmpty(S))
		cerr << "Empty stack" << endl;
	else
	{
		FirstCell = S->Next;
		S->Next = S->Next->Next;
		free(FirstCell);
	}
}

int main(int argc, char* argv[])
{

	return 0;
}

二、栈的数组形式

数组形式通过记录元素数量并且通过改变其值来进行入栈出栈操作。

#include <iostream>
using namespace std;

typedef int ElementType;

struct StackRecord;
typedef struct StackRecord *Stack;

int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopAndPop(Stack S);

#define EmptyTOS (-1)
#define MinStackSize (5)

struct StackRecord
{
	int Capacity;
	int TopOfStack;
	ElementType *Array;
};

int IsEmpty(Stack S)
{
	return S->TopOfStack == EmptyTOS;
}

int IsFull(Stack S)
{
	return S->TopOfStack == S->Capacity;
}

Stack CreateStack(int MaxElements)
{
	Stack S;

	if (MaxElements < MinStackSize)
	{
		cerr << "Stack size is too small" << endl;
	}
	S = (Stack)malloc(sizeof(struct StackRecord));

	if (S == NULL)
		cerr << "Out of space" << endl;

	S->Array = (ElementType*)malloc(sizeof(ElementType)*MaxElements);

	if(S->Array ==NULL)
		cerr << "Out of space" << endl;
	S->Capacity = MaxElements;

	MakeEmpty(S);
	return S;
}

void DisposeStack(Stack S)
{
	if (S != NULL)
	{
		free(S->Array);//先释放数组
		free(S);//再释放指针
	}
}

void MakeEmpty(Stack S)
{
	S->TopOfStack = EmptyTOS;
}

void Push(ElementType X, Stack S)
{
	if (IsFull(S))
	{
		cerr << "Full stack" << endl;
	}
	else
		S->Array[++S->TopOfStack] = X;
}

ElementType Top(Stack S)
{
	if (!IsEmpty(S))
		return S->Array[S->TopOfStack];
	else
		cerr << "Empty stack" << endl;
	return 0;
}

void Pop(Stack S)
{
	if (!IsEmpty(S))
		cerr << "Empty stack" << endl;
	else
		S->TopOfStack--;
}

ElementType TopAndPop(Stack S)
{
	if (!IsEmpty(S))
		return S->Array[S->TopOfStack--];
	else
		cerr << "Empty stack" << endl;
	return 0;
}

int main(int argc, char* argv[])
{
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值