「数据结构」线性表(C语言):链栈和顺序栈

栈的ADT

  • CreateStack():创建栈
  • IsEmpty(StackList S):判断是否为空栈
  • EmptyStack(StackList S):置空栈
  • Push(StackList S):入栈
  • Pop(StackList S):出栈
  • Top(StackList S):获得栈顶元素

链栈(基于链表建立的栈)

链栈没有溢出问题,因为链栈不能定义栈的空间大小。

全部代码

#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
	int value;
	struct Stack * next;
}Stack, * StackList;
StackList CreateStack()
{
	///创建栈
	StackList head;
	head = (StackList)malloc(sizeof(Stack));
	if(head == NULL)
		printf("空间不足!\n");
	head->next = NULL;
	return head;
}
bool IsEmpty(StackList S)
{
	///判断是否为空栈
	if(S->next == NULL)
		return true;
	else
		return false;
}
void EmptyStack(StackList S)
{
	///置空栈
	while(!IsEmpty(S))
	{
		Stack *node = S->next;
		S->next = S->next->next;
		free(node);
	}
}
void Push(StackList S)
{
	///入栈
	Stack *node;
	while(true)
	{
		printf("请输入入栈元素(n结束入栈):\n");
		node = (Stack *)malloc(sizeof(Stack));
		scanf("%d",&node->value);
		node->next = S->next;
		S->next = node;
		printf("入栈成功!!\n");
		getchar();
		printf("是否继续入栈:");
		char c = getchar();
		if(c == 'n')
		{
			printf("结束入栈!!\n");
			break;
		}
		else
			continue;
	}
}
void Pop(StackList S)
{
	///出栈
	while(!IsEmpty(S))
	{
		Stack *node = S->next;
		S->next = S->next->next;
		printf("%d已出栈\n是否继续出栈",node->value);
		free(node);
		getchar();
		if(getchar() == 'y')
			continue;
		else
		{
			printf("出栈结束!!\n");
			break;
		}
	}
	if(IsEmpty(S))	printf("Error:空栈警告\n");
}
void Top(StackList S)
{
	///打印栈顶元素
	if(IsEmpty(S))
		printf("Error:空栈警告\n");
	else
		printf("栈顶元素为:%d\n",S->next->value);
}
int main()
{
	StackList S;
	S = CreateStack();
	Push(S);

	Top(S);

	Pop(S);

	Top(S);
	return 0;
}

顺序栈(基于数组建立的栈)

全部代码

#include<stdio.h>
#include<stdlib.h>
#define MinStackSize 20
typedef struct StackList
{
	int Capacity;	//栈的容量
	int TopOfStack;	//栈顶所在的位置
	int * Array;
}*Stack;
Stack CreateStack(int MaxElements)
{
	///创建栈
	Stack S;
	if(MaxElements > MinStackSize)
	{
		printf("栈空间太小!!\n");
		return 0;
	}
	S = (Stack)malloc(sizeof(StackList));
	if(S == NULL)
	{
		printf("创建失败!!\n");
		return 0;
	}
	S->Array = (int *)malloc(sizeof(int) * MaxElements);
	if(S->Array == NULL)
	{
		printf("空间不足!!\n");
		return 0;
	}
	S->Capacity = MaxElements;
	S->TopOfStack = -1;	//置空栈
	printf("创建成功!!\n");
	return S;
}

int IsEmpty(Stack S)
{
	///判断是否为空栈
	return S->TopOfStack == -1;
}

int IsFull(Stack S)
{
	///判断是否即将溢出
	return S->TopOfStack+1 == S->Capacity; 
}

void Push(Stack S, int x)
{
	///入栈
	int i = 0;
	if(IsFull(S))
		printf("无法插入,空间不足!!\n");
	else
	{
		S->Array[++S->TopOfStack] = x;
	}
}

void Pop(Stack S)
{
	///出栈
	if(IsEmpty(S))
		printf("空栈警告!!\n");
	else
	{
		printf("%d已出栈!!\n",S->Array[S->TopOfStack]);
		S->TopOfStack--;
	}
}

void Top(Stack S)
{
	///打印栈顶元素
	if(IsEmpty(S))
		printf("空栈警告!!\n");
	else
		printf("栈顶元素为:%d\n",S->Array[S->TopOfStack]);
}
int main()
{
	//顺序栈
	Stack S = NULL;
	int x;
	printf("请输入创建栈的大小:");
	scanf("%d",&x);
	S = CreateStack(x);
	while(true){

		printf("请输入入栈元素:");
		scanf("%d",&x);
		Push(S,x);
		if(IsFull(S))
			break;
	}
	Top(S);
	Pop(S);
	Top(S);
	return 0;
}
发布了20 篇原创文章 · 获赞 75 · 访问量 6905

猜你喜欢

转载自blog.csdn.net/weixin_42089228/article/details/103786082