當前位置:首頁 » 編程語言 » 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");
}

}

熱點內容
id伺服器如何填 發布:2025-06-13 03:36:32 瀏覽:386
為什麼很多安卓手機不敵蘋果 發布:2025-06-13 03:34:54 瀏覽:390
uc如何解鎖手機密碼 發布:2025-06-13 03:22:06 瀏覽:561
vs連接sql資料庫代碼 發布:2025-06-13 03:21:57 瀏覽:743
咋學編程 發布:2025-06-13 03:18:05 瀏覽:60
路虎攬勝運動最低配是哪個配置 發布:2025-06-13 02:50:23 瀏覽:235
phptimer 發布:2025-06-13 02:41:54 瀏覽:279
蘋果投屏怎麼連接安卓電視機 發布:2025-06-13 02:37:42 瀏覽:447
網站源碼什麼意思 發布:2025-06-13 02:35:35 瀏覽:467
linux賬戶被鎖定 發布:2025-06-13 02:09:48 瀏覽:847