189 8069 5689

c语言pop函数和pu c语言函数pow

C语言 push和pop函数可以直接用吗?

#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(m10) printf("%d",m); /*小于10,输出数字*/

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

}

printf("\n");

}

队列具有先进先出的特性,设计一个20字节(unsigned char)的队列,请用C语言实现队列的push和pop函数。

#includestdio.h

#include string.h

#define byte unsigned char

byte queue_buf[20], idx = 0;

void push(byte n)

{

if (idx  20)

queue_buf[idx++] = n;

}

byte pop()

{

byte ret = 0;

if (idx--  0)

{

ret = queue_buf[0];

memcpy(queue_buf, queue_buf[1], idx);

}

return ret;

}

byte size()

{

return idx;

}

int main()

{

int len;

for (int i = 1; i = 20; i++)

push(i);

printf("size = %d\n", len = size());

for (int i = 1; i = len; i++)

printf("%d ", pop());

printf("\n");

return 0;

}

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

这个算是数据结构的内容讲解的是一个叫做栈类型的数据结构,这个数据结构的特点就是后进先出--最后放进去的数据最先拿出来。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 退出。


新闻名称:c语言pop函数和pu c语言函数pow
网站链接:http://cdxtjz.cn/article/ddjsisd.html

其他资讯