当前位置:首页 » 编程语言 » c语言入栈

c语言入栈

发布时间: 2025-06-11 19:51:05

Ⅰ 数据结构定义一个栈并实现入栈和出栈操作的程序c语言完整版

如下:

#include"stdio.h"
structstackNode{
intdata;
structstackNode*nextPtr;
};
;
typedefLISTSTACK*STACKNODEPTR;
voidpush(STACKNODEPTR*,int);
intpop(STACKNODEPTR*);
intisEmpty(STACKNODEPTR);
voidprintStack(STACKNODEPTR);
voidinstruct();
intmain()
{
intitem;
intchoice;
STACKNODEPTRsPtr=NULL;
instruct();
printf("chooseyourchoice ");
scanf("%d",&choice);
while(choice!=3)
{
switch(choice)
{
case1:
printf("pleaseinputaninteger! ");
scanf("%d",&item);
//printf("%d ",item);
push(&sPtr,item);
printStack(sPtr);
break;
case2:
if(!isEmpty(sPtr))
{
printf("deletingelementoftopstack ");
pop(&sPtr);
printStack(sPtr);
}
else{
printf("noelementinthestack ");
}
break;
default:
printf("invalidinput,checkyourinput! ");
break;
}
printf("pleacechooseyourchoice");
instruct();
scanf("%d",&choice);
}
}
voidinstruct()
{
printf("Followingtheinstructionbelow: "
"1:insertnewelmentintothestack "
"2:deletethetopelementofthestack "
"3:toendofrun ");
}
intisEmpty(STACKNODEPTRsPtr)
{
returnsPtr==NULL;
}
voidprintStack(STACKNODEPTRsPtr)
{
if(sPtr==NULL)
{
printf("Thestackisempty! ");
}
else{
printf("Theelementsofthestack: ");
while(sPtr!=NULL)
{
printf("%d-->",sPtr->data);
sPtr=sPtr->nextPtr;
}
printf("NULL ");
}
}
voidpush(STACKNODEPTR*topPtr,intvalue)
{
STACKNODEPTRnewPtr;
newPtr=malloc(sizeof(STACKNODEPTR));
if(newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=*topPtr;
*topPtr=newPtr;
}
else
{
printf("%disnotinsertedintostack.Nomemoryisavailiable ");
}
}
intpop(STACKNODEPTR*topPtr)
{
STACKNODEPTRnewPtr;
inttopValue;
newPtr=*topPtr;
*topPtr=(*topPtr)->nextPtr;
free(newPtr);
topValue=(*topPtr)->data;
printf("deleting---%d ",topValue);
returntopValue;
}

Ⅱ 求用C语言编写一个程序实现顺序栈初始化,出栈,入栈,判栈空,判栈满,急需,谢谢

#define STACK_SIZE 100
#define PUSH_POP_SUCCESS 1
#define PUSH_POP_ERROR 0
struct _stackbuf {
int _collection[STACK_SIZE];
int _top;
};
typedef struct _stackbuf S_STACK;
typedef unsigned int u_int_f;
// 入栈
u_int_f push(S_STACK *stack, int d){
if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR;
stack->_collection[stack->_top++] = d;
return PUSH_POP_SUCCESS;
}
// 出栈
u_int_f pop(S_STACK *stack, int *e){
if (!stack->_top) return PUSH_POP_ERROR;
*e=stack->_collection[--(stack->_top)];
return PUSH_POP_SUCCESS;
}
int main(){
S_STACK stack = { {0},0 };
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
int gv = 0;
pop(&stack, &gv);
printf("%d\n", gv);
system("PAUSE");
return 0;
}

Ⅲ C语言编程:顺序栈的入栈与退栈及读顶元素

举一个例子说明 《停车场管理》
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#include "stdio.h"
#include "malloc.h"
#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",p.license);
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=time*PRICE;
printf("● %s 已经离开停车场 停留%d小时 收费%d元",leavecar.license,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");
}

}

热点内容
怎么开启服务器的ftp 发布:2025-06-13 08:05:25 浏览:643
js无需编译 发布:2025-06-13 07:36:35 浏览:804
linux共享上网 发布:2025-06-13 07:32:53 浏览:532
查询域主机服务器ip 发布:2025-06-13 07:20:13 浏览:121
通过ip进服务器文件看不到了 发布:2025-06-13 07:07:46 浏览:583
linux查看用户的命令 发布:2025-06-13 07:05:37 浏览:173
ftp传输使用的端口是 发布:2025-06-13 06:34:32 浏览:643
linux系统有哪些系统 发布:2025-06-13 06:31:02 浏览:946
hht算法 发布:2025-06-13 06:29:14 浏览:493
电脑开机密码设置怎么设置 发布:2025-06-13 06:28:36 浏览:163