【数据结构C语言版】【栈】链式存储

【数据结构C语言版】【栈】链式存储,第1张

数据结构/C语言版】【栈】链式存储

链式

#include
using namespace std;
#define SElemType char
#define Status int
typedef char OperandType;

typedef struct SNode {
	SElemType data;
	SNode* next;
}SNode, * linkStack;

Status InitStack(linkStack& S)
{
	S = (linkStack)malloc(sizeof(SNode));
	if (!S)
		return 0;
	S->next = NULL;
}

Status DestroyStack(linkStack& S)
{
	linkStack p = S->next, ptmp;
	while (p) {
		ptmp = p->next;
		free(p);
		p = ptmp;
	}
	free(S);
	return 1;
}

Status ClearStack(linkStack& S)
{
	linkStack p = S->next, ptmp;
	while (p) {
		ptmp = p->next;
		free(p);
		p = ptmp;
	}
	S->next = NULL;
	return 1;
}

Status StackEmpty(linkStack& S)
{
	return S->next == NULL;
}

int StackLength(linkStack S)
{
	int ans = 0;
	linkStack p = S->next;
	while (p) {
		ans++;
		p = p->next;
	}
	return ans;
}

Status GetTop(linkStack S, SElemType& e)
{
	if (S->next == NULL)
		return 0;
	e = S->next->data;
	return 1;
}

Status Push(linkStack& S, SElemType e)
{
	SNode* p = (SNode*)malloc(sizeof(SNode));
	p->data = e;
	p->next = S->next;
	S->next = p;
	return 1;
}

Status Pop(linkStack& S, SElemType& e)
{
	if (S->next == NULL)
		return 0;
	e = S->next->data;
	SNode* p = S->next;
	S->next = p->next;
	free(p);
	return 1;
}

Status visit(SElemType e)
{
	cout << e << endl;
	return 1;
}

Status StackTraverse(linkStack S, Status(*visit)(SElemType))
{
	if (S->next == NULL)
		return 0;
	for (int i = StackLength(S); i >= 1; i--)
	{
		linkStack p = S->next;
		int j = 1;
		while (p && j < i) {
			p = p->next;
			j++;
		}
		visit(p->data);
	}
	printf("n");
	return 1;
}

Status StackTreverse_Top(linkStack S, Status(*visit)(SElemType))
{
	if (S->next == NULL)
		return 0;
	linkStack p = S->next;
	while (p)
	{
		visit(p->data);
		p = p->next;
	}
	printf("n");
	return 1;
}

int main()
{

	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4748918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存