栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【数据结构/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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号