《数据结构教程》(第5版)学习笔记(一)
生活随笔
收集整理的這篇文章主要介紹了
《数据结构教程》(第5版)学习笔记(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這是實現順序棧的各種基本運算的算法:
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;typedef int ElemType;
const int MaxSize=100;//聲明順序棧的類型
typedef struct{ElemType data[MaxSize];int top;
}SqStack;//棧的初始化
void InitStack(SqStack *&s){s=(SqStack*)malloc(sizeof(SqStack));s->top=-1;
} //棧的銷毀
void Destory(SqStack *&s){free(s);
}//判斷棧是否為空
bool StackEmpty(SqStack *s){return s->top==-1;
}//元素進棧
bool Push(SqStack *&s,ElemType e){if(s->top==MaxSize-1)return false;s->data[++s->top]=e;return true;
}//元素出棧
bool Pop(SqStack *&s,ElemType &e){if(s->top==-1)return false;e=s->data[s->top--];return true;
}//獲取棧頂元素
bool GetTop(SqStack *&s,ElemType &e){if(s->top==-1)return false;e=s->data[s->top];
}int main() {/*一些簡單的操作:string str;cin>>str;int len=str.length();SqStack* s;InitStack(s);for(int i=0;i<len;i++) Push(s,str[i]);ElemType e;for(int i=0;i<len/2;i++){Pop(s,e);if(str[i]!=e){cout<<"no";return 0;}}cout<<"yes";
*/ElemType a[]={1,3,5,7,9,0,8,6,4,2};SqStack* s;InitStack(s);for(int i=0;i<10;i++)Push(s,a[i]);for(int i=0;i<10;i++)Pop(s,a[i]); for(int i=0;i<10;i++)cout<<a[i];Destory(s);cout<<endl<<StackEmpty(s)<<endl;return 0;
}
有不對的地方,請多多指教,謝謝。
總結
以上是生活随笔為你收集整理的《数据结构教程》(第5版)学习笔记(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的IT之路2012(一)
- 下一篇: python百度ai文字识别、不精确、进