test03.txt

来自「嵌入式C语言培训课件」· 文本 代码 · 共 95 行

TXT
95
字号
#include <malloc.h>
#define M 10
typedef int Elemtype;
typedef struct NODE
{
	Elemtype data;
	struct NODE *next;
}SNode;

void InitStact (SNode *&s)
{
	s = NULL;
}

void Push(SNode *&s, Elemtype x)
{
	SNode* p;
	p = (SNode*)malloc(sizeof(SNode));
	p->data = x;
	p->next = NULL;
	if (s == NULL)
	{
		s = p;
	}
	else
	{
		p->next = s;
		s = p;
	}
}

int GetTop (SNode *s, Elemtype &x)
{
	if (s == NULL)
	{
		return 0;
	}
	else
	{
		x = s->data;
		s = s->next;
		return 1;
	}
}

//出栈
int PopStack(SNode *&s, Elemtype &x)
{
	SNode *p = s;
	if (s == NULL)
	{
		return 0;
	}
	else
	{
		x = s->data;
		s = s->next;
		free (p);
		return 1;
	}
	return 1;
}

int StackEmpty (SNode *s)
{
	if (s == NULL)
	{
		return 1;
	}
	return 0;
}

void Display(SNode *p)
{
	printf("出栈显示");
	while (p != NULL)
	{
		int x;
		PopStack(p, x);
		printf("%d-->", x);
	}
}
void main()
{
	int i;
	SNode *p = NULL;
	printf("入栈显示");
	for (i=0; i<10; i++)
	{
		Push(p, i);
		printf("%d-->", i);
	}
	printf("\n");
	Display(p);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?