栈的数组和链表实现
来源:互联网 发布:淘宝店铺重新激活 编辑:程序博客网 时间:2024/06/05 14:29
一、顺序栈的数组实现#include "stdafx.h"#include <malloc.h>#define MAX_SIZE_STACK 1024 /*栈的大小*/#define IS_EMPTY_STACK 1;#define IS_NOT_EMPTY_STACK 0;typedef struct{int data[MAX_SIZE_STACK];int top;/*指向栈顶*/}SeqStack;SeqStack *Init_SeqStack(){SeqStack *S = NULL;S = (SeqStack *) malloc(sizeof(SeqStack));if(NULL == S){printf("\r\n 分配栈失败!\r\n");return NULL;}else{S->top= -1;return S;}}int Empty_SeqStack(SeqStack*s)/*空栈判断*/ { if(s->top == -1){return IS_EMPTY_STACK;}else{return IS_NOT_EMPTY_STACK;}}int Push_SeqStack(SeqStack *s,int data)/*压栈操作*/{if(s->top == MAX_SIZE_STACK -1) {return 0;/*栈已满无法入栈*/}else{s->top++;s->data[s->top] = data;return 1;/*将数据压入栈内*/}}int Pop_SeqStack(SeqStack *s,int *pstdata){if(Empty_SeqStack(s) || NULL == pstdata){return 0;/*空栈没数据可取*/}else{*pstdata = s->data[s->top];s->top--;return 1;/*从栈中取出数据*/}}int Top_SeqStack(SeqStack *s)/*取栈顶元素*/{if(Empty_SeqStack(s)){return 0;/*空栈无数据可取*/}else{return s->data[s->top];}}int Length_SeqStack(SeqStack *s)/*获取栈的元素个数*/{int len =0;while(s->top >= 0){len++;s->top--;}return len;}int Print_SeqStack(SeqStack *s){int i;printf("\r\n 打印栈的元素:\n");for(i=s->top;i>=0;i--)printf("%-5d",s->data[i]);printf("\r\n");return 0;}int _tmain(int argc, _TCHAR* argv[]){SeqStack *L;int n,num,m;int i;L = Init_SeqStack();if(NULL == L){printf("栈初始化失败,直接返回\n");return 0;}printf("栈初始化完成\n");printf("栈空:%d\n",Empty_SeqStack(L));printf("请输入入栈元素个数:\n");scanf("%d",&n);printf("请输入要入栈的%d个元素:\n",n);for(i = 0;i < n; i++){scanf("%d",&num);Push_SeqStack(L,num);}Print_SeqStack(L);printf("栈顶元素:%d\n",Top_SeqStack(L));printf("请输入要出栈的元素个数(不能超过%d个):\n",n);scanf("%d",&n);printf("依次出栈的%d个元素:\n",n);for(i=0;i<n;i++){Pop_SeqStack(L,&m);printf("%-5d",m);}printf("\n");Print_SeqStack(L);printf("栈顶元素:%d\n",Top_SeqStack(L));printf("\r\n栈的元素个数:%d\r\n",Length_SeqStack(L));return 0;}
<pre name="code" class="cpp">二、栈的链表实现#include "stdafx.h"#include <malloc.h>/************************************************************************//* 获取栈顶元素 *//************************************************************************/ElementType TopStackElement(StackNode *S){if(IsEmptyStack(S)){printf("\r\n The stack is empty.\r\n");return 0;}else{return S->Next->Element;}}/************************************************************************//* 销毁栈操作 *//************************************************************************/void DestroyStack(StackNode *S){free(S);printf("\r\n Destroy the stack.\r\n");}/************************************************************************//*压栈操作 *//************************************************************************/void PushStack(StackNode *S,ElementType data){StackNode *pNewElement;pNewElement = (StackNode *)malloc(sizeof(StackNode));if (NULL == pNewElement){printf("\r\n Failed to malloc new stack node.\r\n");}else{pNewElement->Element = data;pNewElement->Next = S->Next;S->Next = pNewElement;}}/************************************************************************//* 出栈操作 *//************************************************************************/void PopStack(StackNode *S,ElementType *pstdata){StackNode *pTopElement;if(IsEmptyStack(S) || (NULL == pstdata)){printf("\r\n The stack is empty.\r\n");}else{pTopElement = S->Next;*pstdata = pTopElement->Element;S->Next = pTopElement->Next;free(pTopElement);}}/************************************************************************//* 栈的判空,如果栈为空,则返回1,若栈不为空,则返回0 *//************************************************************************/int IsEmptyStack( StackNode *S ){return S->Next == NULL;}/************************************************************************//* 清空栈操作 *//************************************************************************/void ClearStack(StackNode *S){ElementType data;if(NULL == S){printf("\r\n The stack does not exist,must be creat a stack first.\r\n");}else{while (!IsEmptyStack(S)){PopStack(S,&data);}}printf("\r\n Clear the stack succeeded.");}/************************************************************************//*创建一个新栈,返回栈指针 *//************************************************************************/ StackNode * CreatStack( void){StackNode *Stack;Stack = (StackNode *)malloc(sizeof(StackNode));if (NULL == Stack){printf("\r\n Malloc stack node error.");return NULL;}Stack->Next = NULL;ClearStack(Stack);return Stack;}/************************************************************************//* 打印栈 *//************************************************************************/ void PrintStack(StackNode *S) {StackNode *ptemp = NULL;ptemp = S->Next;while(ptemp){printf("%-5d",ptemp->Element);ptemp = ptemp->Next;}printf("\r\n"); } /************************************************************************/ /*计算栈中元素个数 */ /************************************************************************/ int Length_SeqStack(StackNode *S) { StackNode *ptemp = NULL; int len = 0; ptemp = S->Next; while(ptemp) { len++; ptemp = ptemp->Next; } return len; }int _tmain(int argc, _TCHAR* argv[]){ElementType m;StackNode *Stack;int i,n,num;Stack = CreatStack();if (NULL == Stack){printf("\r\n Creat stack failed.");return 0;}else{printf("\r\n Creat stack success.");}printf("\r\nStack is empty:%d\n",IsEmptyStack(Stack)); printf("请输入入栈元素个数:\n"); scanf("%d",&n); printf("请输入要入栈的%d个元素:\n",n); for(i = 0;i < n; i++) { scanf("%d",&num); PushStack(Stack,num); } PrintStack(Stack); printf("栈顶元素:%d\n",TopStackElement(Stack)); printf("请输入要出栈的元素个数(不能超过%d个):\n",n); scanf("%d",&n); printf("依次出栈的%d个元素:\n",n); for(i=0;i<n;i++) { PopStack(Stack,&m); printf("%-5d",m); } printf("\n"); PrintStack(Stack); printf("栈顶元素:%d\n",TopStackElement(Stack)); printf("\r\n栈的元素个数:%d\r\n",Length_SeqStack(Stack)); DestroyStack(Stack);return 0;}
0 0
- 栈的数组和链表实现
- 栈的数组和链表实现
- 栈的实现--键表和数组
- 队列的数组和链表实现
- 栈和队列的基本实现(数组实现和链表实现)
- 基于数组和链表的栈实现
- 栈的实现——链表和数组
- 栈的C语言实现(链表和数组)
- 栈的两种实现方式:数组和链表
- 栈的实现——链表和数组
- Java栈实现:数组实现和链表实现
- 使用数组和链表实现栈
- 线性表的数组实现和链表实现
- 冒泡升序排序的数组实现和链表实现
- 桶排序的数组实现和链表实现
- 栈的实现(分别用数组和链表的java实现)
- 数据结构栈和队列的数组实现和链表实现的4个头文件(完全个人思路)
- 堆栈的两种实现形式:数组和链表
- 习惯一:积极主动--个人愿景的原则
- Chrome快捷键
- VIM编辑器配置php语法检测功能
- ubuntu 不能上网Networking disabled 解决办法
- 指针
- 栈的数组和链表实现
- 栈和堆的区别
- android IntentService使用
- android 点击空白区域隐藏软键盘
- HDU 1575 Tr A (矩阵快速幂入门)
- java Annotation 基础图解
- 公开课.古代智慧与当代爱情 [第23课]电影《情如物证》 --- David O'Connor
- Cas(05)——修改Cas的其它配置
- android 代码方式安装apk