栈的基本算法

来源:互联网 发布:qq号码淘宝 编辑:程序博客网 时间:2024/06/12 01:03

#include<stdio.h>#include<malloc.h>

#define Yes 1typedef struct node{ char data; struct node *next;}Snode;void InitStack(Snode  *L1){ L1=NULL;} push(Snode *top, char x) { Snode *S;  S = (Snode *)malloc(sizeof(Snode));       if (!S)  {  printf("内存空间不足无法插入! /n");  return 0;     } S->data = x; S->next=top;      /*新结点链入栈顶*/ top = S;           /*新结点成为新的栈顶*/ return 1;}char pop(Snode *top, char e){ Snode * p;  if (top!=NULL)  {   e = top->data;             /*原栈顶结点元素赋予e*/  } p=top;                        /*被删结点赋予P*/   top=top->next;             /*设置新栈顶*/ free(p);           /*释放原栈顶结点内存空间*/ return e;}          StackEmpty(Snode  *L){ if(L) return 0; else  return 1;}char compare(){Snode *L1;

char ch1,ch2,e2,e1;InitStack(L1);

while((ch1=getchar())!='@'||(ch1=getchar())!='&')    push(L1,ch1);while((ch2=getchar())=='&')    pop(L1,ch2);while(!StackEmpty(L1)){ e1=pop(L1,ch1); e2= pop(L1,ch2); if(e1==e2)   continue; else {  printf("No/n");break; }}return Yes;}

main(){ compare(); printf("/n");}

原创粉丝点击