stack.c

来自「本次实验」· C语言 代码 · 共 64 行

C
64
字号
#include <stdio.h>
#include<malloc.h>
#include "stack.h"

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

void Pop(Stack S)
{
	PtrToNode FirstCell;

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

Stack CreateStack(void)
{
	Stack S;

	S=(PtrToNode)malloc(sizeof(struct Node));
	if(S==NULL)
		printf("Out of space!!!");
	MakeEmpty(S);
	return S;
}

ElmentType Top(Stack S)
{
	if(!IsEmpty(S))
		return S->Next->Elment;
	printf("Empty stack");
	return 0;
}

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

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

void MakeEmpty(Stack S)
{
	if(S==NULL)
		printf("Must use CreateStack first");
	else
		S->Next=NULL;
}

⌨️ 快捷键说明

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