c语言怎么使用pop函数 c pop函数

c语言 函数参数传递 int pop(int *s, int *e)在函数定义时写int
pop(int
s,
int
e)是说明参数是直接引用的参数
在函数调用时写pop(a,
b);[注意这个不是函数定义,而是调用语句],
这里的是取地址的运算,与函数参数定义时的不是同一个含义,因此在这里不能理解是引用传递的意思,而是取a和b的地址传递给函数的参数变量s和e(应该是调用第3个函数)
第2个函数调用直接写pop(a,b);即可实现
“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(struct SqStack * MyStack, ElemType *e#include stdio.h
#include conio.h
#include stdlib.h
#define elemType int/* 链栈元素数据类型 */
#define SNODE_SIZE sizeof (struct sNode)/* 链栈结点空间大小 */
#define status int /* 状态型变量 */
#define OVERFLOW -1 /* 内存溢出状态码 */
#define ERROR 0/* 错误状态码 */
#define OK 1/* 正确状态码 */
/* 链栈结点存储结构 */
typedef struct sNode {
elemType data;
struct sNode *next;
} sNode, *sNodePtr;
/* 链栈存储结构 */
typedef struct linkStack {
sNodePtr top; /* 栈顶指针 */
} linkStack;
/* 初始化 */
/* 操作结果:构造一个带头结点的空链栈S */
void initStack (linkStack *S) {
S-top = (sNodePtr) malloc (SNODE_SIZE); /* 产生头结点,栈顶指针指向此头结点 */
if (!S-top) /* 内存分配失败 */
exit (OVERFLOW);
S-top-next = NULL;
}
/* 销毁 */
/* 初始条件:链栈S已存在 。操作结果:销毁链栈S */
void destroyStack (linkStack *S) {
sNodePtr p, q;
p = S-top; /* p指向S的头结点 */
while (p) {
q = p-next; /* q指向p的下一个结点 */
free (p); /* 回收p指向的结点 */
p = q; /* p移动到下一个结点 */
} /* 直到没有下一个结点 */
}
/* 判断链栈是否为空 */
/* 初始条件:链栈S已存在 。操作结果:若S为空链栈,则返回TRUE,否则返回FALSE */
status stackIsEmpty (linkStack *S) {
return S-top-next == NULL;
}
/* 入栈 */
/* 操作结果:在S的栈顶插入新的元素e */
status push (linkStack *S, elemType e) {
sNodePtr p;
p = (sNodePtr) malloc (SNODE_SIZE); /* 产生新结点 */
if (!p) /* 内存分配失败 */
exit (OVERFLOW);
p-data = https://www.04ip.com/post/e;
p-next = S-top-next; /* 将新结点链接到原栈顶 */
S-top-next = p; /* 栈顶指向新结点 */
}
/* 出栈 */
/* 操作结果:删除S的栈顶元素,并由e返回其值 */
status pop (linkStack *S, elemType *e) {
sNodePtr p;
if (stackIsEmpty (S))
return ERROR;
p = S-top-next; /* p指向链栈的第一个结点 */
*e = p-data; /* 取出数据 */
S-top-next = p-next;
free (p); /* 删除该结点 */
【c语言怎么使用pop函数 c pop函数】if (S-top == p) /* 栈为空 */
S-top-next = NULL;
return OK;
}
/* 打印栈内容 */
/* 初始条件:链栈S已存在 。操作结果:当栈不为空时 , 打印栈内容并返回OK , 否则返回ERROR */
status printStack (linkStack *S) {
sNodePtr p;
if (stackIsEmpty (S)) {
puts ("The stack is empty! ");
return ERROR;
}
p = S-top-next;
while (p) {
printf ("%d\t", p-data);
p = p-next;
}
putchar ('\n');
return OK;
}
int main (void) {
linkStack S;
elemType e;
elemType a, b, c, d;
a = 1; b = 2; c = 3; d = 4;
initStack (S);
push (S, a);
push (S, b);
push (S, c);
push (S, d);
puts ("Push 4 elements");
printf ("S:\t");
printStack (S);
putchar ('\n');
pop (S, e);
puts ("Pop 1 element");
printf ("S:\t");
printStack (S);
destroyStack (S);
getch (); /* 屏幕暂留 */
return 0;
}
如有问题,可以点击头像联系我
C语言 push和pop函数可以直接用吗?#include stdio.h
#include stdlib.h
#define MAXSIZE 32
typedef struct{
int *elem;/* 栈c语言怎么使用pop函数的存储区 */
int max;/* 栈的容量c语言怎么使用pop函数,即找中最多能存放的元素个数 */
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; //压栈c语言怎么使用pop函数,栈顶加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");
}
c语言怎么使用pop函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c pop函数、c语言怎么使用pop函数的信息别忘了在本站进行查找喔 。

    推荐阅读