栈实现(数据结构---数组,链表 C实现)
2017-11-21 17:23
381 查看
#include<stdio.h> #include<stdlib.h> //栈的数组实现 struct StackRecord { int Capacity; int TopOfStack; int *Array; }; typedef struct StackRecord *Stack; Stack CreateStack(int Length) { Stack S; S = (Stack)malloc(sizeof(struct StackRecord)); S->Array = (int*)malloc(Length * sizeof(int)); S->Capacity = Length; S->TopOfStack = -1; return S; } void DisposeStack(Stack S) { if (S != NULL) { free(S->Array); free(S); } } int IsEmpty(Stack S) { return S->TopOfStack == -1; } int IsFull(Stack S) { return S->Capacity == (S->TopOfStack+1); } void MakeEmpty(Stack S) { S->TopOfStack = -1; } void Push(int x, Stack S) { if (IsFull(S)) { printf("Full Stack\n"); return; } else S->Array[++ S->TopOfStack] = x; } int Top(Stack S) { if (!IsEmpty(S)) return (S->Array[S->TopOfStack]); printf("Empty Stack\n"); return 0; } int Pop(Stack S) { if (IsEmpty(S)) { printf("Empty Stack\n"); return ; } else return(S->Array[S->TopOfStack--]); } int TopAndPop(Stack S) { if (!IsEmpty) return(S->Array[S->TopOfStack--]); printf("Empty Stcak\n"); return 0; }
#include<stdio.h> #include<stdlib.h> //链表实现,元素默认的字符~~~ struct Node { double a; struct Node *next; }; typedef struct Node *Stack; Stack CreakStack() { Stack S; S = (Stack)malloc(sizeof(Node)); S->next = NULL; return S; } void Push(char c, Stack S) { Stack tmp; tmp = (Stack)malloc(sizeof(Node)); tmp->a = c; tmp->next = S->next; S->next = tmp; } void Pop(Stack S) { Stack tmp; tmp = S->next; S->next = tmp->next; free(tmp); } char Top(Stack S) { return(S->next->a); } bool Isempty(Stack S) { return S->next == NULL; }
相关文章推荐
- 简单的数据结构(总结)只涉及用数组或链表实现
- 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现
- 数据结构学习---堆栈的动态数组实现及链表实现
- Lua中使用table实现的其它5种数据结构(数组、链表、队列、集合和包、StringBuild)
- 数据结构学习---堆栈的动态数组实现及链表实现
- 用单链表与数组实现队列先进先出的数据结构
- 数据结构-C++实现(一):数组链表
- 分别用数组和链表实现集合数据结构
- 算法与数据结构 其二 数组和链表 的实现
- 数据结构 —— 链表实现多项式加减乘
- 数据结构---无重复元素链表的实现
- C#实现常用的数据结构(一):链表
- 一步一步学数据结构之1--1(队列--单链表实现--含队头尾指针)
- java实现数据结构-数组中按次数进行循环右移
- 数据结构-线性表的数组实现-Java
- C++数据结构--用向量数组实现大(小)根堆的插入和删除
- 数据结构之链表与数组(三)-单向链表上的简单操作
- 数据结构示例之用链表实现栈
- 用数组和链表实现队列操作
- [数据结构]C#基于数组实现泛型顺序表