栈的C语言实现(链表和数组)
2015-03-10 16:09
399 查看
数组栈的实现
stack_array.h[code]#ifndef _stack_array_h #define EmptyTOS (-1) #define MinStackSize (5) #define TRUE 1 #define FALSE 0 typedef int ElementType; typedef struct StackArrayNode { int Capacity; int TopOfStack; ElementType *Array; }StackArrayNode,*Stack; int IsEmpty(Stack S); int IsFull(Stack S); Stack CreatStack(int MaxStackSize); void FreeStack(Stack S); void Push(ElementType x, Stack S); void Pop(Stack S); ElementType Top(Stack S); ElementType TopandPop(Stack S); #endif
stack_array.c
[code]#include <stdio.h> #include <stdlib.h> #include "stack_array.h" int IsEmpty(Stack S) { if(S->TopOfStack == EmptyTOS) return TRUE; else return FALSE; } int IsFull(Stack S) { if(S->TopOfStack == S->Capacity) return TRUE; else return FALSE; } Stack CreatStack(int MaxStackSize) { Stack S; if(MaxStackSize < MinStackSize) printf("Stack is too small\n"); S = (Stack)malloc(sizeof(struct StackArrayNode)); if(S == NULL) exit(FALSE); S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxStackSize); if(S->Array == NULL) exit(FALSE); S->Capacity = MaxStackSize; S->TopOfStack = EmptyTOS; } void FreeStack(Stack S) { if(S != NULL) { free(S->Array); free(S); } } void Push(ElementType x, Stack S) { if(IsFull(S)) printf("Stack is full\n"); else { S->TopOfStack ++; S->Array[S->TopOfStack] = x; } } void Pop(Stack S) { if(IsEmpty(S)) printf("Stack is empty\n"); else { S->TopOfStack --; } } ElementType Top(Stack S) { if(!IsEmpty(S)) return S->Array[S->TopOfStack]; printf("Stack is Empty\n"); return FALSE; } ElementType TopandPop(Stack S) { if(!IsEmpty(S)) return S->Array[S->TopOfStack--]; printf("Stack is Empty\n"); return FALSE; } int main() { Stack S; S = CreatStack(10); Push(1,S); Push(2,S); Push(4,S); printf("%d\n",Top(S)); Pop(S); printf("%d\n",Top(S)); Pop(S); printf("%d\n",Top(S)); }
链表栈的实现
stack.h[code]#ifndef _Stack_h #define TRUE 1 #define FALSE 0 typedef int ElementType; typedef struct StackNode { ElementType data; struct StackNode *next; }StackNode,*Stack; int isEmpty(Stack S); Stack CreateStack(void); void MakeEmpty(Stack S); void Push(ElementType x, Stack S); void Pop(Stack S); ElementType Top(Stack S); #endif /*_Stack_h */
stack.c
[code]#include <stdlib.h> #include <stdio.h> #include "stack.h" int isEmpty(Stack S) { if(S->next == NULL) return TRUE; else return FALSE; } void MakeEmpty(Stack S) { if(S == NULL) printf("fata error"); else { while(!isEmpty(S)) Pop(S); } } Stack CreateStack(void) { Stack S = (Stack)malloc(sizeof(struct StackNode)); if(S == NULL) printf("fata error"); S->next = NULL; MakeEmpty(S); return S; } void DeleteStack(Stack S) { MakeEmpty(S); free(S); } void Push(ElementType x, Stack S) { Stack TmpStack = (Stack)malloc(sizeof(struct StackNode)); if(TmpStack == NULL) printf("fata error"); else { TmpStack->data = x; TmpStack->next = S->next; S->next = TmpStack; } } void Pop(Stack S) { Stack TmpStack; if(isEmpty(S)) printf("Stack is Empty"); else { TmpStack = S->next; S->next = S->next->next; free(TmpStack); } } ElementType Top(Stack S) { if(isEmpty(S)) return FALSE; else return S->next->data; } int main() { Stack S = CreateStack(); Push(1,S); Push(2,S); Push(4,S); printf("%d\n",Top(S)); Pop(S); printf("%d\n",Top(S)); Pop(S); printf("%d\n",Top(S)); }
参考自《数据结构与算法分析》
相关文章推荐
- 数组和链表的归并排序算法实现(C语言)
- C语言链表中数组实现数据选择排序,升序、降序功能主要难点
- 栈(stack)C语言链表实现&&数组实现
- C语言链表中数组实现数据选择排序,升序、降序功能主要难点
- 用数组实现的单链表[C语言]
- 《数据结构与算法分析》—栈的链表和数组实现(C语言)
- 链表的C语言实现(含动态内存分配)
- C语言实现数据结构的链表
- 链表的C语言实现之动态内存分配
- 链表的C语言实现(范例)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现,附代码)
- 如何使用c语言实现双向链表的插入删除操作
- 链表的C语言实现之循环链表及双向链表
- 链表的C语言实现(五)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- C语言实现数据结构中的循环链表
- 链表的C语言实现之动态内存分配(转载)
- 链表的c语言实现(四)
- C语言链表的实现(修改版)Cpp文件