数据结构与算法分析-栈
来源:互联网 发布:java中什么是方法重载 编辑:程序博客网 时间:2024/06/09 13:42
数据结构与算法分析-栈(单链表实现)
栈的单链表实现很简单,甚至比单链表本身还简单,因为栈的进栈和出栈都是在表头完成的,具体的就不分析了。代码见github:https://github.com/xiabodan/DataStructure 保持持续更新,或者直接
git clone https://github.com/xiabodan/DataStructure.git
//by xiabodan#include <stdio.h>#include <stdlib.h>typedef int elementtype;typedef struct node *stack;typedef struct node *position;struct node { elementtype data; position next;};int isempty(stack S);void delete_stack(stack S);stack create_stack(void);elementtype top(stack S);elementtype pop(stack S);void push(stack S,elementtype data);int isempty(stack S){ return (S->next == NULL);}stack create_stack(void){ stack S; S = (position)malloc(sizeof(struct node)); if( S == NULL) { printf("create_stack : out of space"); return ; } S->next = NULL; // S->next is top of stack ,because header can't store element return S;}void delete_stack(stack S){ if(S == NULL) printf("S is a empty stack!\n"); else { while( !isempty( S)) pop(S); }}elementtype pop(stack S){ elementtype data; if(!isempty(S)) { position tem; tem = S->next;//tem point to the top node data = S->next->data; S->next = tem->next;//also S->next = S->next->next free(tem); return data; } else printf("pop : empty stack!\n"); return 0;}elementtype top(stack S){ if(!isempty(S)) return (S->next->data); printf("top : empty stack!\n"); return 0;}void push(stack S,elementtype data){ position P; P = (position)malloc(sizeof(struct node)); if( P == NULL) { printf("push : out of space"); return ; } P->data = data; P->next = S->next; //compare list , insert node location the first node here,but insert node anywhere in list S->next = P;}int main(int argc ,char** argv){ stack S; int i = 0 ; elementtype data; S = create_stack(); printf("push 10 datas from 0 to 9 \n"); for(i=0;i<10;i++) { push(S,i); } printf("S is empty? %d \n",isempty( S)); printf("pop 5 datas \n"); for(i=0;i<5;i++) { data = pop(S); printf("pop data is : %d\n",data); } printf("push 2 datas 11,12\n"); push(S,11); push(S,12); printf("pop all remain datas \n"); while(!isempty( S)) { data = pop(S); printf("pop data is : %d\n",data); } printf("S is empty? %d \n",isempty( S)); delete_stack(S);}
1 0
- 数据结构与算法分析-栈
- 数据结构与算法分析:栈与队列
- 数据结构与算法分析:栈与队列
- 数据结构算法与分析
- 数据结构与算法分析
- 数据结构与算法分析
- 数据结构与算法分析
- 数据结构与算法分析
- 数据结构与算法分析
- 数据结构与算法分析
- 数据结构与算法分析
- 《数据结构与算法分析》
- 数据结构与算法分析-栈的实现
- 数据结构与算法分析——栈
- 【数据结构与算法分析】顺序栈
- 数据结构与算法分析之栈
- 数据结构与算法分析c++:算法分析
- 数据结构与算法分析c++:算法分析
- question1-在插入排序中运用二元搜索代替线性查找
- Vijava 学习笔记(指定虚拟机添加虚拟磁盘修订功能版本)
- Tk on Python
- Mac 下解压Android NDK 的 .bin文件
- Welcome to Linux From Scratch!
- 数据结构与算法分析-栈
- 20 款超棒免费的 Bootstrap 管理和前端模板
- 移植busybox-1.14.0
- <fmt:parseNumber/>解析数字
- "晴天"天气app的开源库逐步学习(一)
- 承志医疗管理系统技术解析住院管理(六)
- 数据结构与算法分析-队列
- 如何在CSDN博客中显示图片?
- C/C学习笔记/指针与数组