“c语言”中,“pop函数”和“push函数”的作用分别是什么?

Python023

“c语言”中,“pop函数”和“push函数”的作用分别是什么?,第1张

这个算是数据结构的内容讲解的是一个叫做栈类型的数据结构,这个数据结构的特点就是后进先出--最后放进去的数据最先拿出来。pop函数就是拿出数据的操作,push是放入是数据的操作。

内容拓展:

pop函数呵push函数的使用:

#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

}

//输出结果: chread 1 start ->thread 1 push complte 

//push和pop框起来的代码,不管正常退出还是异常退出,都将执行清除函数,但是存在特例:不包括return 退出。

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 32

typedef struct{

int *elem/* 栈的存储区 */

  int max   /* 栈的容量,即找中最多能存放的元素个数 */

  int top   /* 栈顶指针 */ 

}Stack

int InitStack(Stack *S, int n) /*创建容量为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) /*将整数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进制的数*/

m=Pop(&S)

if(m<10) printf("%d",m) /*小于10,输出数字*/

else printf("%c", m+55) /*大于或等于10,输出相应的字符*/

}

printf("\n")

}

在keil C51中,直接调用库函数:

#include<intrins.h>// 其中包含了对部分汇编指令的调用申明

_nop_()// 产生一条NOP指令

_push_(acc)// 产生一条push指令

以下是intrins.h的内容

/*--------------------------------------------------------------------------

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