C++顺序栈的主函数怎么写,我的程序的话主程序该怎么写

C++顺序栈的主函数怎么写,我的程序的话主程序该怎么写,第1张

你这是数据结构中的队列问题,而不是栈的问题。head代表的是队列头,删除时删的是队列头元素,tail代表的是队列尾,插入时插的是队列尾元素 程序中这条语句是错误的。中括号只有一半,那一半呢? p_y=qp++head]; 至于要求的主函数,如果是队列问题还要涉及的是一般队列还是循环队列,因此没法写给你。

#include"stdioh"

#include<iostreamh>

#include<malloch>

#include<stringh>

#define STACK_INIT_SIZE 200

struct Stack{

int base;

int top;

}sq;

void InitStack(Stack &s){

sbase=(int )malloc(STACK_INIT_SIZEsizeof(int));

stop=sbase;

};

void Push(Stack &s,int e){

stop=e;

stop++;

};

int Pop(Stack &s){//设从键盘输入一整数的序列:a1,a2,a3,……an,

stop--;

return stop;//试编写算法实现:用栈结构存储输入的整数,

};//当ai≠—1时,将ai进栈,当当ai≠—1时,将所有栈元素出

void main()//栈。算法应对异常情况(如栈满等)给出相应的信息》

{

Stack S1,S2;

InitStack(S1);

InitStack(S2);

int a[10];

int i=0,b;

while(i<10){

cout<<"请输入第"<<i<<"个元素"<<endl;

cin>>a[i];

if(a[i]==-1)

break;

Push(S1,a[i]);

i++;}

while(S1top!=S1base){

b=Pop(S1);

cout<<b<<endl;}

}

上次把类型定义错了 以前是只能使用小于128的 现在把类型改成int了就使用任何数字了

举一个例子说明 《停车场管理》

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

#include "stdioh"

#include "malloch"

#define max_stop 5 // 停车场容量//

#define PRICE 8 //停车单价 //

typedef struct//汽车//

{ char license[20]; //汽车//

}car;

typedef struct //停车场//

{ car stop[max_stop]; //停车场//

int top;

}parking;

void come(car p,parking tcc)

{

if(tcc->top>=max_stop-1){printf("已满!!");}

else

{

printf(" 输入到来汽车的车牌号码 : ");

scanf("%s",plicense);

printf("\n");

tcc->top++; tcc->stop[tcc->top]=p;//如果停车场没有满就入场//

}

}

void display(car p,parking tcc)

{

int n=0;

if(tcc->top==-1)printf(" 停车场内无汽车!\n"); //指针在-1处 停车场内无车//

else{ // 有车 //

printf("●停车场内的车为:\n");

while(n<=tcc->top)

{

printf("第 %d 辆 %s\n",n+1,tcc->stop[n]license);

n++;

}

}

}

void leave(car p,parking tcc)

{

car leavecar;

int num,money,time;

printf(" 输入将要离开的汽车车位号码:");

scanf("%d",&num);

num--;

if(num>tcc->top||num<0)printf(" 你的输入有误 请检查预备出停车场的车辆号码\n");

else

{

printf(" 输入此车停留的时间 : ");

scanf("%d",&time);

if(time<=0)printf(" 你的输入有误 请检查停车时间\n");

else

{

leavecar=tcc->stop[num];

while(num<tcc->top)

{

tcc->stop[num]=tcc->stop[num+1];

num++;

}

tcc->top--;

}

}

if((num<=tcc->top) && (num>=0))

{

money=timePRICE;

printf("● %s 已经离开停车场 停留%d小时 收费%d元",leavecarlicense,time,money);

}

}

void welcome() //欢迎界面//

{

printf(" ●欢迎适用本程序●\n");

printf(" 本程序为停车场的模拟管理程序,有车到来时请按 C 键\n");

printf(" 然后根据屏幕提示进行相关 *** 作,有车要走时请按 L 键\n");

printf(" 然后根据屏幕提示进行相关 *** 作,若需要帮助请按 H 键\n");

printf(" 然后根据屏幕提示进行相关 *** 作,要退出程序请按 Q 键\n");

}

void main()

{

char key;

car p;

parking tcc;

tcc=(parking )malloc(sizeof(parking));

tcc->top=-1;

while(1)

{

flushall();

printf(" 请输入您的 *** 作 : ");

scanf("%c",&key);

if(key=='c'||key=='C')come(p,tcc);

else if(key=='l'||key=='L')leave(p,tcc);

else if(key=='d'||key=='D')display(p,tcc);

else if(key=='h'||key=='H')welcome();

else if((key=='q')||(key=='Q'))break;

else printf(" 您的输入有误 ! ! 请重新输入:\n");

}

}

你这完全是抄严蔚敏那本书上的啊。。。

我帮你改了下:

#include<stdioh>

#include<malloch>

#define stack_init_size 100

#define stackincrement 10

typedef struct

{

int base;

int top;

int stacksize;

} sqstack;

int Initstack(sqstack s)

{

s->base=(int )malloc(stack_init_sizesizeof(int));

if(!s->base)exit(-1);

s->top=s->base;

s->stacksize=stack_init_size;

return 1;

}

int gettop(sqstack s,int e)

{

if(s->top==s->base)

return 0;

e=(s->top-1);

return 1;

}

int push(sqstack s,int e)

{

if(s->top-s->base>=s->stacksize)

{

s->base=(int )realloc(s->base,(s->stacksize+stackincrement)sizeof(int));

if(!s->base)exit(-1);

s->top=s->base+s->stacksize;

s->stacksize+=stackincrement;

}

s->top++=e;

return 1;

}

int pop(sqstack s,int e)

{

if(s->top==s->base)

return 0;

e=--s->top;

return 1;

}

int main(){

sqstack s;

Initstack(s);

push(s,1);

push(s,2);

push(s,3);

push(s,4);

int val;

pop(s,&val);

printf("%d\n",val);

return 0;

}

以上就是关于C++顺序栈的主函数怎么写,我的程序的话主程序该怎么写全部的内容,包括:C++顺序栈的主函数怎么写,我的程序的话主程序该怎么写、关于顺序栈的实现、C语言编程:顺序栈的入栈与退栈及读顶元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9401051.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存