測試過了,可以運行。
公司主營業(yè)務(wù):網(wǎng)站設(shè)計、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出德宏州免費做網(wǎng)站回饋大家。
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 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--];
}
在keil C51中,直接調(diào)用庫函數(shù):
#includeintrins.h // 其中包含了對部分匯編指令的調(diào)用申明
_nop_(); // 產(chǎn)生一條NOP指令
_push_(acc); // 產(chǎn)生一條push指令
以下是intrins.h的內(nèi)容
/*--------------------------------------------------------------------------
INTRINS.H
Intrinsic functions for C51.
Copyright (c) 1988-2004 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __INTRINS_H__
#define __INTRINS_H__
extern void _nop_ (void);
extern bit _testbit_ (bit);
extern unsigned char _cror_ (unsigned char, unsigned char);
extern unsigned int _iror_ (unsigned int, unsigned char);
extern unsigned long _lror_ (unsigned long, unsigned char);
extern unsigned char _crol_ (unsigned char, unsigned char);
extern unsigned int _irol_ (unsigned int, unsigned char);
extern unsigned long _lrol_ (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
extern void _push_ (unsigned char _sfr);
extern void _pop_ (unsigned char _sfr);
#endif
這是我用鏈表寫的:
#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));
}
}
以下代碼是基于C語言寫的堆棧的壓棧,出棧,清棧,讀棧指針等方法,在Visual studio 中,可直接使用,供學習者參考學習。
#include
#include
#include
#include
#include
#include
#define MAX_SIZE 100
typedef struct Stack
{
char *data;
int size;
int top;
};
void initStack(Stack *s); //init stack
void destroyStack(Stack *s);
bool push(Stack *s,char ch);
char pop(Stack *s);
char gettop(Stack *s);
bool isEmpty(Stack *s);
bool isFull(Stack *s);
void setNull(Stack *s);
#endif
void initStack(Stack *s)
{
s-data = (char*)malloc(MAX_SIZE * sizeof(char)); //分配最大內(nèi)存空間
if (!s-data)
exit(OVERFLOW); //提前終止程序
s-size = MAX_SIZE;
s-top = -1;
}
void destroyStack(Stack *s)
{
free(s-data);
}
bool push(Stack *s, char ch)
{
if ((s-top + 1) != s-size)
{
s-data[++s-top] = ch;
return true;
}
else
return false;
}
char pop(Stack *s)
{
if (s-top != -1)
return s-data[s-top--];
}
char gettop(Stack *s)
{
return s-data[s-top];
}
bool isEmpty(Stack *s)
{
if (s-top == -1)
return true;
else
return false;
}
bool isFull(Stack *s)
{
if ((s-top + 1) == s-size)
return true;
else
return false;
}
void setNull(Stack *s)
{
s-top = -1;
}
int main()
{
char chd;
bool c;
Stack s1;
initStack(s1);
c = push(s1, 'a');
printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c,s1.data[s1.top],s1.top);
c = push(s1, 'b');
printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c, s1.data[s1.top], s1.top);
chd = gettop(s1);
printf("Stack s1-top data:%c,top value is %d ", chd, s1.top);
chd = pop(s1);
printf("Stack 彈出 data:%c,top value is %d ", chd, s1.top);
chd = pop(s1);
printf("Stack 彈出 data:%c,top value is %d ", chd, s1.top);
c = isEmpty(s1);
printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);
c = isFull(s1);
printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);
return 0;
}
這個算是數(shù)據(jù)結(jié)構(gòu)的內(nèi)容講解的是一個叫做棧類型的數(shù)據(jù)結(jié)構(gòu),這個數(shù)據(jù)結(jié)構(gòu)的特點就是后進先出--最后放進去的數(shù)據(jù)最先拿出來。pop函數(shù)就是拿出數(shù)據(jù)的操作,push是放入是數(shù)據(jù)的操作。
內(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
#define?MAXSIZE?32
typedef?struct{
int?*elem;/*?棧的存儲區(qū)?*/
??int?max;???/*?棧的容量,即找中最多能存放的元素個數(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進制的數(shù)*/
m=Pop(S);
if(m10)?printf("%d",m);?/*小于10,輸出數(shù)字*/
else?printf("%c",?m+55);?/*大于或等于10,輸出相應(yīng)的字符*/
}
printf("\n");
}
本文名稱:c語言push函數(shù)使用 c++中push是什么意思
URL鏈接:http://jinyejixie.com/article42/hpdjhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)、面包屑導航、網(wǎng)站排名、靜態(tài)網(wǎng)站、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)