栈的基本运算(Push,Pop,初始化等)有给源码和详细注释

2023-12-21 06:17:19

栈是一种只能在一端进行插入或删除操作的线性表。

其实和前面讲过的单链表头存储一样

栈有两种基本的结构(1)顺序结构(2)链式结构

如下图

基本运算

InitStack? 初始化栈

DestroyStack? ?摧毁栈

StackEmpty? 判断空栈

Push? 进栈

Pop? 出栈

GetTop? 取栈顶元素

预备动作

#define  _CRT_SECURE_NO_WARNINGS 1
//顺序栈
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define a 100;
typedef int ElemType;
typedef struct
{
	ElemType data[100];
	int top;
}SqStack;

栈的顺序存储结构

InitStack? 初始化栈

SqStack* InitStack()
{
	SqStack* s = (SqStack*)malloc(sizeof(SqStack));
	s->top = -1; //栈顶指针置于-1
	return s;
}

DestroyStack? ?摧毁栈

void DestroyStack(SqStack*s)
{
	free(s);
}

StackEmpty? 判断空栈

bool StackEmpty(SqStack* s)
{
	return (s->top == -1);
}

Push? 进栈

bool Push(SqStack* s, ElemType e)
{
	if (s->top == 100 - 1)//栈满情况,防止溢出
		return false;
	s->top++;
	s->data[s->top] = e;
	return true;
}

Pop? 出栈

bool Pop(SqStack* s, ElemType* e)
{
	if (s->top == -1) return false;//栈为空栈时
	*e = s->data[s->top];
	s->top--;
	return true;
}

GetTop? 取栈顶元素

和Pop的区别时GetTop的栈顶指针不会移动

bool GetTop(SqStack* s, ElemType* e)
{
	if (s->top == -1) return false;
	*e = s->data[s->top];
	return true;
}

总代码

总代码main函数是判断str数组是否为对称数组

#define  _CRT_SECURE_NO_WARNINGS 1
//顺序栈
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define a 100;
typedef int ElemType;
typedef struct
{
	ElemType data[100];
	int top;
}SqStack;
SqStack* InitStack()
{
	SqStack* s = (SqStack*)malloc(sizeof(SqStack));
	s->top = -1;
	return s;
}
void DestroyStack(SqStack*s)
{
	free(s);
}
bool StackEmpty(SqStack* s)
{
	return (s->top == -1);
}
bool Push(SqStack* s, ElemType e)
{
	if (s->top == 100 - 1)
		return false;
	s->top++;
	s->data[s->top] = e;
	return true;
}
bool Pop(SqStack* s, ElemType* e)
{
	if (s->top == -1) return false;
	*e = s->data[s->top];
	s->top--;
	return true;
}
bool GetTop(SqStack* s, ElemType* e)
{
	if (s->top == -1) return false;
	*e = s->data[s->top];
	return true;
}
int main()//例题判断str是否为对称数组
{
	ElemType str[10] = { 1,2, 3,4,5,5,4,3,2,1 };
	ElemType sts[10];
	SqStack*s = InitStack();
	for (int i = 0; i < 10; i++)
	{
		Push(s, str[i]);
	}
	ElemType e;
	ElemType* t = &e;

	for (int i = 0; i < 10; i++)
	{
		Pop(s, t);
		sts[i] = *t;
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", sts[i]);
	}
	DestroyStack(s);
	return 0;
}

栈的链式结构

算法和上面基本一样直接給代码需要自取哈

#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int ElemType;
typedef struct LinkNode
{
	ElemType data;
	struct LinkNode* next;
}LinkStNode;
LinkStNode* InitStack()
{
	LinkStNode* L = (LinkStNode*)malloc(sizeof(LinkStNode));
	L->next = NULL;
	return L;
}
void DestroyStack(LinkStNode* s)
{
	LinkStNode* p = s->next, * pre = s;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}
bool StackEmpty(LinkStNode* s)
{
	return (s->next == NULL);
}
bool Push(LinkStNode* s, ElemType e)
{
	LinkStNode* L = InitStack();
	L->data = e;
	L->next = s->next;
	s->next = L;
	return true;
}
bool Pop(LinkStNode* s, ElemType* e)
{
	LinkStNode* p = s->next;
	if (s->next == NULL) return false;
	else
	{
		e = p->data;
		s->next = p->next;
		free(p);
		return true;
	}
}
bool GetTop(LinkStNode* s, ElemType* e)
{
	if (s->next == NULL) return false;
	else
	{
		*e = s->next->data;
		return true;
	}
}
int main()
{
	return 0;
}

作者阿尼亚要你们的点赞 耶耶耶

文章来源:https://blog.csdn.net/2301_80035594/article/details/135119302
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。