#include
#include
#define null 0
typedef struct node
{
int data;
struct node *next;
}linkstack;
linkstack *top;
linkstack push(linkstack *top,int x) ////入栈
{
linkstack *p;
p=(linkstack)malloc(sizeof(linkstack));
p->data=x;
p->next=top;
top=p;
return top;
}
linkstack* pop(linkstack *top) //出栈
{
linkstack *p;
if(!top)
{
printf("空链栈");
return null;
}
p=top;
top=top->next;
free(p); //释放存储空间
return top;
}
void print(linkstack *top) //输出栈
{
linkstack *p;
p=top;
while(p!=null) ////刚开始栈顶的next项为空////
{
printf("%d\n",p->data);
p=p->next;
}
}
void main()
{
int x,i;
top=(linkstack*) malloc(sizeof(linkstack));
scanf("%d",&x);
top->data=x;//头部赋值。
top->next=null;
// initstack(top);
for(i=1;i<=3;i++)
{
scanf("%d",&x);
top=push(top,x);//返回一个指针。
}
print(top);
top=pop(top);//返回一个指针。
}
怎么加数制转换和字符匹配?
任意进制数的转换(链表栈的实现)
http://wenku.baidu.com/link?url=Z-9pODFQbcqTWbuz3pvfoRU3xWxRnXHYXIhYmfQRfZ27DbyD8JBKa7S3ugYkmrTmTX0hDYSXrwzIHuO9cNz8zsQ6adiDYYqtyQgzjl_FDVS
括号匹配,定义一个变量,初始值0,从左往右扫描文本,遇到前括号+1,遇到后括号-1,判断要求,这个变量始终>=0,并且最终等于0
int foo(char * s)
{
int x = 0;
while (x >= 0 && *s != '\0')
{
if (*s == '(') x++;
if (*s == ')') x--;
s++;
}
return x == 0 ? 1 : 0;
}
int main()
{
char s1[]="1+(2*3-(4+5)";
char s2[]="(4+5)*(8-(3+2))";
char d1 = foo(s1) ? 'y' : 'n';
printf("%c\n", d1);
char d2 = foo(s2) ? 'y' : 'n';
printf("%c\n", d2);
}