C源码@数据结构与算法->栈Stack

本文深入探讨了信息技术领域的各个方面,包括前端开发、后端开发、移动开发、游戏开发等细分领域。从HTML、CSS、JavaScript等基础技术,到PHP、Python、Java等高级语言的应用,再到大数据开发、开发工具、嵌入式硬件等多个子领域,全面展示了信息技术的丰富性和复杂性。此外,文章还涉及到了大数据处理、人工智能、测试、文档协作与知识管理等热门话题,为读者提供了一个全面了解信息技术领域的窗口。

/*
 * fatal.h
 * Header file for print error message.
 */
#ifndef _FATAL_H
#define _FATAL_H

#include 
#include 

#define Error(str) FatalError(str)
#define FatalError(str) fprintf(stderr, "%s\n", str), exit(-1)

#endif /* _FATAL_H */

/*
 * stack.h
 */
#ifndef _STACK_H
#define _STACK_H

#define _STACK_ARRAY_

#ifndef NULL
#define NULL (0)
#endif

typedef int ElementType;

#ifndef _STACK_ARRAY_
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
#else /* ifndef _STACK_ARRAY_ */
struct StackRecord;
typedef struct StackRecord *Stack;
#endif /* ifndef _STACK_ARRAY_ */

#ifdef __cplusplus
extern "C" {
#endif

int IsEmpty(Stack S);
#ifndef _STACK_ARRAY_
Stack CreateStack();
#else /* _STACK_ARRAY_ */
Stack CreateStack(int MaxElements);
int IsFull(Stack S);
ElementType TopAndPop(Stack S);
#endif /* _STACK_ARRAY_ */
void MakeEmpty(Stack S);
void DisposeStack(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

#ifdef __cplusplus
}
#endif

#endif /* _STACK_H */
/*
 * stack.cpp
 */
#include  
#include "fatal.h"
#include "stack.h"

#ifndef _STACK_ARRAY_
/* --------------------Linked List Implementation of Stacks------------------- */

struct Node
{
	ElementType Element;
	PtrToNode Next;
};

/*
 * Test whether a stack is empty.
 */
int IsEmpty(Stack S)
{
	return S->Next == NULL;
}

/*
 * Create a header node.
 * Then, set the next pointer to NULL.
 */
Stack CreateStack()
{
	Stack S;

	S = (Stack)malloc(sizeof(struct Node));
	if (S == NULL)
	{
		FatalError("Out of memory!");
	}

	S->Next = NULL;
	
	MakeEmpty(S);

	return S;
}

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

void DisposeStack(Stack S)
{
	MakeEmpty(S);
	free(S);
}

/*
* The Push is implemented as an insertion into the front of a linked list,
* where the front of the list serves as the top of the stack.
*/
void Push(ElementType X, Stack S)
{
	PtrToNode TmpCell;

	TmpCell = (PtrToNode)malloc(sizeof(struct Node));
	if (TmpCell == NULL)
	{
		FatalError("Out of memory!");
	}
	else
	{
		TmpCell->Element = X;
		TmpCell->Next = S->Next;
		S->Next = TmpCell;
	}
}

/*
 * Retrieve the element in the first position of the list.
 */
ElementType Top(Stack S)
{
	if (!IsEmpty(S))
	{
		return S->Next->Element;
	}
	else
	{
		Error("Empty stack");
		return -1;		/* Return value used to avoid warning */
	}
}

/*
 * Delete a cell from the front of the list.
 */
void Pop(Stack S)
{
	PtrToNode FirstCell;

	if (IsEmpty(S))
	{
		FatalError("Empty stack");
	}
	else
	{
		FirstCell = S->Next;
		S->Next = S->Next->Next;
		free(FirstCell);
	}
}

#else /* #ifndef _STACK_ARRAY_ */
/* --------------------Array Implementation of Stacks------------------- */
#define EmptyTOS		(-1)
#define MinStackSize	(5)

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

/*
 * Test whether stack is empty.
 */
int IsEmpty(Stack S)
{
	return S->TopOfStack == EmptyTOS;
}

/*
 * Test whether stack is full.
 */
int IsFull(Stack S)
{
	return S->TopOfStack == S->Capacity - 1;
}

/*
 * Create stack.
 */
Stack CreateStack(int MaxElements)
{
	Stack S;

	if (MaxElements < MinStackSize)
	{
		FatalError("Stack size is too small!");
	}

	S = (Stack)malloc(sizeof(struct StackRecord));
	if (S == NULL)
	{
		FatalError("Out of memory!");
	}

	S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements);
	if (S->Array == NULL)
	{
		FatalError("Out of memory!");
	}

	S->Capacity = MaxElements;

	MakeEmpty(S);

	return S;
}

/*
 * Make stack empty.
 */
void MakeEmpty(Stack S)
{
	S->TopOfStack = EmptyTOS;
}

/*
 * Free the stack structure.
 */
void DisposeStack(Stack S)
{
	if (S != NULL)
	{
		free(S->Array);
		free(S);
	}
}

/*
 * A Push on a full stack will overflow the array bounds and cause a crash.
 */
void Push(ElementType X, Stack S)
{
	if (IsFull(S))
	{
		FatalError("Full stack!");
	}
	else
	{
		S->Array[++S->TopOfStack] = X;
	}
}

/*
 * Return top of stack.
 */
ElementType Top(Stack S)
{
	if (IsEmpty(S))
	{
		FatalError("Empty stack!");
		return 0;		/* Return value used to avoid warning */
	}
	else
	{
		return S->Array[S->TopOfStack];
	}
}

/*
 * A Pop on an empty stack will underflow the array bounds and cause a crash.
 */
void Pop(Stack S)
{
	if (IsEmpty(S))
	{
		FatalError("Empty stack!");
	}
	else
	{
		--S->TopOfStack;
	}
}

ElementType TopAndPop(Stack S)
{
	if (IsEmpty(S))
	{
		FatalError("Empty stack!");
		return 0;		/* Return value used to avoid warning */
	}
	else
	{
		return S->Array[S->TopOfStack--];
	}
}

#endif /* #ifndef _STACK_ARRAY_ */
/*
 * teststack.cpp
 */
#include 
#include "stack.h"

int main()
{
	Stack S = NULL;
	int i;
#ifndef _STACK_ARRAY_
	S = CreateStack();
#else
	S = CreateStack(12);
#endif
	for (i = 0; i < 10; ++i)
	{
		Push(i, S);
	}

	while (!IsEmpty(S))
	{
		printf("%d\n", Top(S));
		Pop(S);
	}

	DisposeStack(S);

	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值