zoukankan      html  css  js  c++  java
  • 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
    ", SeqStack_Top(&stack));
    
    	printf("栈中元素:");
    	for (int i = 0; i < 50; i++)
    	{
    		if (i % 5 == 0)
    			printf("
    ");
    		printf("%d ", SeqStack_Pop(&stack));
    	}
    	printf("
    ");
    
    	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("栈已空,不必再销毁!
    ");
    		return;
    	}
    	// 栈不为空,需要把栈中的结点都删除释放
    	do
    	{
    		pNode pTmp;
    		pTmp = Pop(lstack);
    		free(pTmp);
    	} while (lstack->size > 0);
    	printf("栈销毁成功!
    ");
    }
    
    int main()
    {
    	srand((unsigned)time(0));
    	LinkStack lstack = NULL;
    	lstack = Create();
    
    	int ret;
    	ret = IsEmpty(lstack);
    	if (ret)
    		printf("栈为空!
    ");
    	else
    		printf("栈不为空!
    ");
    
    	for (int i = 0; i < 10; i++)
    	{
    		Push(lstack, rand() % 100);
    	}
    
    	ret = IsEmpty(lstack);
    	if (ret)
    		printf("栈为空!
    ");
    	else
    		printf("栈不为空!
    ");
    
    	printf("栈的长度:%d
    ", getSize(lstack));
    
    	printf("栈顶元素: %d
    ", *((int*)getTop(lstack)));
    
    
    	while (lstack->size > 0)
    	{
    		printf("%d ", *((int*)Pop(lstack)));
    	}
    	printf("
    ");
    
    	Destory(lstack);
    
    	return 0;
    }
    
    

    03、栈的应用举例

    1、圆括号匹配的检验

    2、字符串回文的判断

    3、数制转换

    04、栈与递归

    • 无太多内容

    参考文献&履历

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

    履历

    • 2021年02周,书的内容过了一遍,但代码怎么写,不确定呢。
    • 2021-01-11把 顺序栈 代码跑了一遍。
  • 相关阅读:
    小朋友学数据结构(2):栈
    小朋友学数据结构(1):约瑟夫环的链表解法、数组解法和数学公式解法
    小朋友学C++(2)
    小朋友学C++(1)
    小朋友学C语言(8)
    小朋友学C语言(7)
    小朋友学C语言(6)
    小朋友学C语言(5):常量和变量,for循环
    小朋友学C语言(4):单精度浮点数与双精度浮点数
    小朋友学C语言(3):整数、浮点数、字符
  • 原文地址:https://www.cnblogs.com/fewolflion/p/14264955.html
Copyright © 2011-2022 走看看