zoukankan      html  css  js  c++  java
  • 数据结构入门-栈

    定义:一种可以实现“先进后出”的存储结构

    分类:

    1. 静态栈
    2. 动态栈

    算法:

    1. 出栈
    2. 压栈

    代码实现:

    多敲,多敲,后期改进

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    typedef struct Node
    {
    	int data;
    	struct Node * pNext;
    }NODE , * PNODE;
    
    
    typedef struct Stack
    {
    	PNODE pTop;
    	PNODE pBottom;
    }STACK , * PSTACK;
    
    
    void init(PSTACK);
    void push(PSTACK , int);
    void traverse(PSTACK);
    bool pop(PSTACK , int *);
    void clear(PSTACK pS);
    
    
    int main(void)
    {
    	STACK S; // STACK等价于 struct Stack
    	int val;
    
    	init(&S); // 目的是造出一个空栈
    
    	push(&S , 1); // 压栈
    	push(&S , 8);
    	push(&S , 23);
    	push(&S , 26);
    	push(&S , 34);
    	push(&S , 45);
    	push(&S , 76);
    	push(&S , 88);
    	traverse(&S); // 遍历输出
    
    	if(pop(&S , &val))
    	{
    		printf("你删除的是%d
    ", val );
    		traverse(&S);
    		printf("清空数据
    ");
    		clear(&S);
    		traverse(&S);
    	}
    	else
    	{
    		printf("删除失败
    ");
    	}
    }
    
    
    
    
    void init(PSTACK pS)
    {
    	pS->pTop = (PNODE)malloc(sizeof(NODE));
    	if (NULL == pS->pTop)
    	{
    		printf("动态内存分配失败
    ");
    		exit(-1);
    	}
    	else
    	{
    		pS->pBottom = pS->pTop;
    		pS->pTop->pNext = NULL; // pS->pBottom->pNext = NULL
    	}
    }
    
    
    void push(PSTACK pS, int val)
    {
    	PNODE pNew = (PNODE)malloc(sizeof(NODE));
    
    	pNew->data = val; 
    
    	pNew->pNext = pS->pTop; // 这里需要注意
    	pS->pTop = pNew;
    
    	return;
    }
    
    
    void traverse(PSTACK pS)
    {
    	PNODE p = pS->pTop;
    
    	while(p != pS->pBottom)
    	{
    		printf("%d ", p->data);
    		p = p->pNext;
    	}
    
    	printf("
    ");
    	return;
    }
    
    
    bool empty(PSTACK pS )
    {
    	if (pS->pTop == pS->pBottom)
    		return true;
    	else
    		return false;
    
    }
    
    
    // 把pS所指向的栈出栈一次,并把出栈元素存下
    bool pop(PSTACK pS , int *val)
    {
    
    	if (empty(pS))
    	{
    		return false;
    	}
    	else
    	{
    		PNODE p = pS->pTop;
    		*val = p->data;
    
    		pS->pTop = p->pNext;
    		free(p);
    		p = NULL;
    		return true;
    
    	}
    	
    }
    
    
    // 清空
    void clear(PSTACK pS)
    {
    	if (empty(pS))
    	{
    		return;
    	}
    	else
    	{
    		PNODE p = pS->pTop;
    		PNODE q = NULL;
    
    		while(p != pS->pBottom)
    		{
    			q = p->pNext;
    			free(p);
    			p = q;
    		}
    
    		pS->pTop = pS->pBottom;
    	}
    }
    

    应用:

    1. 函数调用
    2. 中断
    3. 表达式求值
    4. 内存分配
    5. 缓冲处理
    6. 迷宫
  • 相关阅读:
    linux 用户、组,修改文件权限
    linux下获取帮助
    PHPSESSID的cookie//session_start()
    【python】import 模块、包、第三方模块
    python练习——最长的递减子序列
    python练习——水仙花数
    Linux目录结构
    Scala入门3(特质线性化)
    Scala入门2(特质与叠加在一起的特质)
    人工智能我见及特征提取mfcc算法理解
  • 原文地址:https://www.cnblogs.com/mengd/p/12041368.html
Copyright © 2011-2022 走看看