DS:04栈

缘起

内容

01、顺序栈

  • 理论知识

  • 代码

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 1024
#define INFINITY 65535
typedef struct
{
	int data[MAXSIZE];  // 在结构中定义一个数组
	int top;            // 指示栈顶元素,在数组中相当于索引
}SeqStack;

void InitStack(SeqStack* stack)
{
	stack->top = -1;
}

int IsEmpty(SeqStack* stack)
{
	if (stack->top == -1)
		return 1;
	return 0;
}

int SeqStack_Top(SeqStack* stack)
{
	if (!IsEmpty(stack))
		return stack->data[stack->top];
	return INFINITY;
}

int SeqStack_Pop(SeqStack* stack)
{
	if (!IsEmpty(stack))
		return stack->data[stack->top--];
	return INFINITY;
}

void SeqStack_Push(SeqStack* stack, int val)
{
	if (stack->top >= MAXSIZE - 1)  // stack full
		return;
	stack->top++;
	stack->data[stack->top] = val;
}

int main()
{
	srand((unsigned)time(0));
	SeqStack stack;
	InitStack(&stack);

	for (int i = 0; i < 50; i++)
	{
		SeqStack_Push(&stack, rand() % 1000);
	}

	printf("栈顶元素:%d\n", SeqStack_Top(&stack));

	printf("栈中元素:");
	for (int i = 0; i < 50; i++)
	{
		if (i % 5 == 0)
			printf("\n");
		printf("%d ", SeqStack_Pop(&stack));
	}
	printf("\n");

	return 0;
}

02、链栈

理论知识

代码

#include<stdio.h>
#include<stdlib.h>

typedef struct Node* pNode;
typedef struct Stack* LinkStack;
struct Node
{
	int data;
	pNode next; // 指针
};

struct Stack
{
	pNode top; // 栈顶元素指针
	int size;  // 栈大小
};

LinkStack Create()
{
	LinkStack lstack = (LinkStack)malloc(sizeof(struct Stack));
	if (lstack != NULL)
	{
		lstack->top = NULL;
		lstack->size = 0;
	}
	return lstack;
}

int IsEmpty(LinkStack lstack)
{
	if (lstack->top == NULL || lstack->size == 0)
		return 1;
	return 0;
}

int getSize(LinkStack lstack)
{
	return lstack->size;
}

pNode getTop(LinkStack lstack)
{
	if (lstack->size != 0)
		return lstack->top;
	return NULL;
}

int Push(LinkStack lstack, int val)
{
	pNode node = (pNode)malloc(sizeof(struct Node));
	if (node != NULL)
	{
		node->data = val;
		node->next = getTop(lstack); // 新元素结点指向下一个结点,链式实现  【先构建一个节点,再放入到link里去,lionel】
		lstack->top = node; // top指向新节点
		lstack->size++;
	}
	return 1;
}

int Pop(LinkStack lstack)
{
	if (IsEmpty(lstack))
		return NULL;
	pNode node = lstack->top;
	lstack->top = lstack->top->next;  // top指向下一个元素
	lstack->size--;
	return node;
}

void Destory(LinkStack lstack)
{
	if (!IsEmpty(lstack))
	{
		free(lstack);
		printf("栈已空,不必再销毁!\n");
		return;
	}
	// 栈不为空,需要把栈中的结点都删除释放
	do
	{
		pNode pTmp;
		pTmp = Pop(lstack);
		free(pTmp);
	} while (lstack->size > 0);
	printf("栈销毁成功!\n");
}

int main()
{
	srand((unsigned)time(0));
	LinkStack lstack = NULL;
	lstack = Create();

	int ret;
	ret = IsEmpty(lstack);
	if (ret)
		printf("栈为空!\n");
	else
		printf("栈不为空!\n");

	for (int i = 0; i < 10; i++)
	{
		Push(lstack, rand() % 100);
	}

	ret = IsEmpty(lstack);
	if (ret)
		printf("栈为空!\n");
	else
		printf("栈不为空!\n");

	printf("栈的长度:%d\n", getSize(lstack));

	printf("栈顶元素: %d\n", *((int*)getTop(lstack)));


	while (lstack->size > 0)
	{
		printf("%d ", *((int*)Pop(lstack)));
	}
	printf("\n");

	Destory(lstack);

	return 0;
}

03、栈的应用举例

1、圆括号匹配的检验

2、字符串回文的判断

3、数制转换

04、栈与递归

  • 无太多内容

参考文献&履历

  • 知识内容来自 自学考试 教材
  • 代码 来自 传智播客

履历

  • 2021年02周,书的内容过了一遍,但代码怎么写,不确定呢。
  • 2021-01-11把 顺序栈 代码跑了一遍。
posted @ 2021-01-11 23:28  im天行  阅读(90)  评论(0编辑  收藏  举报