這個(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)公司專業(yè)為企業(yè)提供寧強(qiáng)網(wǎng)站建設(shè)、寧強(qiáng)做網(wǎng)站、寧強(qiáng)網(wǎng)站設(shè)計(jì)、寧強(qiáng)網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、寧強(qiáng)企業(yè)網(wǎng)站模板建站服務(wù),10年寧強(qiáng)做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
內(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 退出。
這是我用鏈表寫的:
#include stdio.h
#include stdlib.h
typedef struct node
{
int x;
struct node *next;
}Node;
typedef struct stack
{
Node *top;
}Stack;
void InitStack(Stack *s)
{
s-top=NULL;
}
int IsEmpty(Stack *s)
{
if(s-top==NULL)
return 1;
else
return 0;
}
void PushStack(Stack *s,int x)
{
Node *p;
p=(Node*)malloc(sizeof(Node));
p-x=x;
// p-next=NULL;
p-next=s-top;
s-top=p;
}
int PopStack(Stack *s)
{
int data;
Node *p;
p=(Node *)malloc(sizeof(Node));
if(IsEmpty(s))
{
printf("the stack is empty!\n");
free(p);
return -1;
}
else
{
p=s-top;
data=p-x;
s-top=p-next;
free(p);
return data;
}
}
int main (int argc,char **argv)
{
int i;
Stack s;
InitStack(s);
for(i=0;i1000;i++)
{
PushStack(s,i);
}
for(i=0;i1000;i++)
{
printf("%d\n",PopStack(s));
}
}
1、打開或者新建.h文件;
2、在該文件中添加你的函數(shù);
3、保存退出,記住該文件名及其路徑;
4、在新文件中包含該文件名,如果該文件不在搜索路徑下,則包含該文件的全名
比如:
定義一個(gè)函數(shù)void
mydefun(){}
調(diào)試無誤后,以文件名aa.h保存在D:\abc目錄下
在新文件中要用到這個(gè)函數(shù),則包含語句中必須有以下一條語句:
#include"D:\\abc\\aa.h"
然后你就可以調(diào)用mydefun()函數(shù)了。
ElemType是筆誤S.base=(ElemType *)malloc (S.base, (S.stacksize+STACKINCREMENT)*sizeof(Elemtype));這個(gè)是分配一段內(nèi)存,長度是(S.stacksize+STACKINCREMENT)*sizeof(Elemtype)這么多字節(jié),因?yàn)檫@個(gè)函數(shù)是重新分配的,所以也要分配表s.base的存儲(chǔ)空間
測試過了,可以運(yùn)行。
int eval_postfix(char *exp){
while(*exp!='\0'){
if(is_number(*exp)){
int val = *exp - 48;
push(val);
}
else{
int a1 = pop();
int a2 = pop();
if(*exp=='+') push(a2+a1);
else if(*exp=='-') push(a2-a1);
else if(*exp=='*') push(a2*a1);
else if(*exp=='/') push(a2/a1);
}
exp ++;
}
return pop();
}
void push(int e){
stack[++top] = e;
}
int pop(){
return stack[top--];
}
整個(gè)程序是:
#include stdio.h
#include stdlib.h
#define MAX_SIZE 100
#define boolean unsigned char
#define true 1
#define false 0
// Global stack
int stack[MAX_SIZE];
int top = -1;
void push(int e);
int pop();
int eval_postfix(char *exp);
boolean is_number(char c);
void main ()
{
char exp[100]; // postfix expression
int result;
while(1) {
printf("\n Input postfix expression: ");
scanf("%s", exp);
result = eval_postfix(exp);
printf(" Result = %d \n\n", result);
}
}
boolean is_number(char c)
{
if (('0' = c) (c = '9'))
return true;
else
return false;
}
int eval_postfix(char *exp){
while(*exp!='\0'){
if(is_number(*exp)){
int val = *exp - 48;
push(val);
}
else{
int a1 = pop();
int a2 = pop();
if(*exp=='+') push(a2+a1);
else if(*exp=='-') push(a2-a1);
else if(*exp=='*') push(a2*a1);
else if(*exp=='/') push(a2/a1);
}
exp ++;
}
return pop();
}
void push(int e){
stack[++top] = e;
}
int pop(){
return stack[top--];
}
#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");
}
當(dāng)前名稱:push函數(shù)c語言用法,c++push函數(shù)
URL網(wǎng)址:http://chinadenli.net/article34/dsgcjpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、營銷型網(wǎng)站建設(shè)、建站公司、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)