新聞中心
“c語言”中,“pop函數(shù)”和“push函數(shù)”的作用分別是什么?
這個(gè)算是數(shù)據(jù)結(jié)構(gòu)的內(nèi)容講解的是一個(gè)叫做棧類型的數(shù)據(jù)結(jié)構(gòu),這個(gè)數(shù)據(jù)結(jié)構(gòu)的特點(diǎn)就是后進(jìn)先出--最后放進(jìn)去的數(shù)據(jù)最先拿出來。pop函數(shù)就是拿出數(shù)據(jù)的操作,push是放入是數(shù)據(jù)的操作。
成都創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)呼倫貝爾,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
內(nèi)容拓展:
pop函數(shù)呵push函數(shù)的使用:
#include stdio.h
#include unistd.h
#include pthread.h
void *clean(void *arg)
{
printf("cleanup: %s \n",(char *)arg);
return (void *)0;
}
void * thr_fn1(void * arg)
{
printf("chread 1 start \n");
pthread_cleanup_push((void *)clean,"thraed 1 first handler");
pthread_cleanup_push((void *)clean,"thread 1 second handler");
printf("thread 1 push complete \n");
if(arg)
{
return ((void *)1);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)1;
}
//輸出結(jié)果: chread 1 start -thread 1 push complte?
//push和pop框起來的代碼,不管正常退出還是異常退出,都將執(zhí)行清除函數(shù),但是存在特例:不包括return 退出。
C語言寫Pop函數(shù)取棧的出錯(cuò)
Pop函數(shù)改成這樣:
int Pop (Stack * pstack, int * pname)
{
if(pstack-top=0)
{
return 0;
}
pstack-top--;
* pname = pstack-data[pstack-top];
return 1;
}
Push函數(shù)改成這樣:
int Push (Stack * pstack, int num)
{
if(pstack-top=Stack_size)
{
printf("Push Error!");
return 0;
}
pstack-data[pstack-top]=num;
pstack-top++;
return 0;
}
試試(原來那樣當(dāng)元素達(dá)到最大數(shù)目時(shí)pstack-top就越界了)。
C語言 push和pop函數(shù)可以直接用嗎?
#include?stdio.h
#include?stdlib.h
#define?MAXSIZE?32
typedef?struct{
int?*elem;/*?棧的存儲(chǔ)區(qū)?*/
??int?max;???/*?棧的容量,即找中最多能存放的元素個(gè)數(shù)?*/
??int?top;???/*?棧頂指針?*/?
}Stack;
int?InitStack(Stack?*S,?int?n)?/*創(chuàng)建容量為n的空棧*/
{
S-elem?=?(int?*)malloc(n?*?sizeof(int));
if(S-elem==NULL)?return?-1;
S-max=n;
S-top?=0;?//棧頂初值0
return?0;
}
int?Push(Stack?*S,?int?item)?/*將整數(shù)item壓入棧頂*/
{
if(S-top==S-max)?{
printf("Stack?is?full!?\n");
return?-1;
}
S-elem[S-top++]?=?item;?//壓棧,棧頂加1
return?0;
}
int?StackEmpty(Stack?S)
{
return?(!S.top)?1:0;?/*判斷棧是否為空*/
}
int?Pop(Stack?*S)?/*棧頂元素出棧*/
{
if(!S-top)?{
printf("Pop?an?empty?stack!\n");
return?-1;
}
return?S-elem[--S-top]??;?//彈出棧,棧頂減1
}
void?MultibaseOutput(long?n,int?B)
{
int?m;?Stack?S;
if(InitStack(S,MAXSIZE)){
printf("Failure!\n");
return;
}
do?{
if?(Push(S,B?))?//------
{
printf("Failure!\n");
return;
}
n=?n-1?;?//--------
}while(n!=0);
while(!StackEmpty(S))?{?/*輸出B進(jìn)制的數(shù)*/
m=Pop(S);
if(m10)?printf("%d",m);?/*小于10,輸出數(shù)字*/
else?printf("%c",?m+55);?/*大于或等于10,輸出相應(yīng)的字符*/
}
printf("\n");
}
網(wǎng)站欄目:c語言中pop函數(shù) c語言pop函數(shù)參數(shù)
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/hpgsgd.html