69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct node
{
char data;
struct node *link;
}JD;
JD *push(JD **top, char x)
{
JD *p;
p = (JD*)malloc(sizeof(JD));
p->data = x;
p->link = *top;
*top = p;
return(p);
}
JD *pop(JD **top,char *e)
{
JD *q;
if (!(*top)) {
*e = 0;
return NULL;
}
q = *top;
*e = (*top)->data;
*top = (*top)->link;
free(q);
return *top;
}
static void pre_pop(JD *top)
{
JD *p = top;
while (p) {
putchar(p->data);
p = p->link;
}
putchar(10);
}
int main()
{
JD *top = NULL;
int i,lenth;
char str[20]="abcdcba";
char e;
/*
top = (JD*)malloc(sizeof(JD));
top->link = NULL;
*/
lenth=strlen(str);
for(i=0; i<lenth; i++)
push(&top,str[i]);
//pre_pop(top);
for(i=0;i<lenth;i++)
{
pop(&top, &e);
if(e!=str[i])
{
printf("NO\n");
return 1;
}
}
printf("YES\n");
}
参考一下吧
如果不想使用返回值,就需要传递二重指针;
如果不使用二重指针,建议使用返回值。