数据结构查找算法基本栈的算法,主函数不会写

求大神,主函数不会写啊!!!!顺序栈的主函数怎么写啊!!_数据结构吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:16,487贴子:
求大神,主函数不会写啊!!!!顺序栈的主函数怎么写啊!!收藏
#include&stdio.h&#include&stdlib.h&const STACK_INIT_SIZE=100;const STACKINCREMENT=10;typedef struct{ SElemType *}SqSvoid InitStack_Sq(SqStack &S,int maxsize=STACK_INIT_SIZE,int incresize=STACKINCREMENT){ S.elem=new SElemType[maxsize]; S.top=-1; S.stacksize= S.incrementsize=}bool GetTop_Sq(SqStack s,SElemType &e){ if(S.top=-1)return FALSE; e=S.elem[S.top]; return TRUE;}void Push_Sq(SqStack &S,SElemType e){ if(S.top==S.stacksize-1)incrementStacksize(S); S.elem[++S.top]=e;}bool Pop_Sq(SqStack &S,SElemType &e){ if(S.top==-1)return FALSE; e=S.elem[S.top--]; return TURE;}
这些你都能写出来,主函数就写不出来?
直接引用函数就是了。
登录百度帐号我的游戏推荐游戏
后查看最近玩过的游戏
为兴趣而生,贴吧更懂你。或stack,中文翻译为堆栈,其实指的是栈,heap,堆。这里讲的是数据结构的栈,不是内存分配里面的堆和栈。
栈是先进后出的数据的结构,好比你碟子一个一个堆起来,最后放的那个是堆在最上面的。
队列就是排队买苹果,先去的那个可以先买。
public class Stack {
private int array[];
public Stack(int max){
this.max =
array = new int[max];
public void push(int value){
if(isFull()){
System.out.println(&full,can not insert&);
array[top++]=
public int pop(){
return array[--top];
public boolean isEmpty(){
if(top == 0){
public boolean isFull(){
if(top == max ){
public void display(){
while(!isEmpty()){
System.out.println(pop());
public static void main(String[] args) {
Stack s = new Stack(5);
s.push(1);
s.push(3);
s.push(5);
s.push(5);
s.push(5);
s.display();
}其实还是觉得设置top为-1好计算一点,记住这里的i++和++i,如果i=1,那么array[i++]=2,指的是array[1]=2,下次用到i的时候i的值才会变2,而++i就是直接使用i=2。
top指向0,因为每次都push一个元素加一,那么添加到最后一个元素的时候top=max。由于先进后出,那么先出的是最后进的,刚刚为top-1所在的位置。
正确输出:
一、栈的使用——单词逆序。
public String reverse(String in){
String out=&&;
for (int i = 0; i & in.length(); i++) {
char c = in.charAt(i);
while(!isEmpty()){
out+=pop();
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String string = s.nextLine();
Stack st = new Stack(string.length());
System.out.println(st.reverse(string));
}将Stack的数组类型改为char即可。
读取输入也可以用IO读取。
public static void main(String[] args) {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(is);
String string=&&;
string = b.readLine();
} catch (IOException e) {
e.printStackTrace();
Stack st = new Stack(string.length());
System.out.println(st.reverse(string));
二、栈的使用——分隔符匹配。
public int charat(char c){
for (int i = 0; i & array. i++) {
if(c == array[i])
return array.
public void match(String in){
String out=&&;
for (int i = 0; i & in.length(); i++) {
char c = in.charAt(i);
if(c == '{' || c == '(' || c == '[' ){
if(c == '}' || c == ')' || c == ']'){
char temp = pop();
if(c == '}' && temp != '{'|| c == ')'
&& temp != '('|| c == ']' && temp != ']'){
System.out.println(&can not match in &+i);
while(!isEmpty()){
char c = pop();
if(c == '{'){
System.out.println(&insert } to match &+charat(c));
if(c == '[' ){
System.out.println(&insert ] to match &+charat(c));
if(c == '(' ){
System.out.println(&insert ) to match &+charat(c));
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String string = s.nextLine();
    Stack st = new Stack(string.length());
    st.match(string);
klsjdf(klj{lkjjsdf{)
can not match in 19
insert } to match 1
insert ) to match 0
将({[先压入栈,一旦遇到)}]便与弹出的元素比较,若吻合,则匹配。如果一直没有)}],最后便会弹出栈的左符号,提示是在具体哪个位置,缺少的具体的右符号类型。
这是可以用栈来实现的工具。
栈中数据入栈和出栈的时间复杂度为常数O(1),因为与数据个数无关,直接压入弹出,操作时间短,优势便在这里,如果现实生活的使用只需用到先进后出的顺序而且只用到进出数据的比较,那就可以使用栈了。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:98426次
积分:2236
积分:2236
排名:第6645名
原创:87篇
评论:116条
文章:15篇
阅读:5329
(4)(9)(5)(16)(8)(3)(2)(2)(1)(2)(2)(1)(3)(3)(6)(5)(9)(9)(5)(1)(2)(1)(1)

我要回帖

更多关于 数据结构算法 的文章

 

随机推荐