一个非常简单的指针判断为空有关问题
一个非常简单的指针判断为空问题。
废话不多说,看代码。
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef char datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//ERROR
void Push(slink& top,datatype x)
{
slink p;
if(Emptystack(top))
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
free(p);
}
else
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
//free(p);
}
}
datatype Pop(slink& top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
/******************************************************/
/**** 表达式括号匹配的检验.cpp ****/
/******************************************************/
# include "stack.h "
# include <iostream.h>
int bdsxgs(char E[]);
void main()
{
char a[]= "A[(I-2)*4]+3# ";
if(bdsxgs(a))
{
cout < < "括号匹配。 " < <endl;
}
else
{
cout < < " 括号不匹配. " < <endl;
}
}
int bdsxgs(char E[])
{
int i=0;
char x;
slink top;
Clearstack(top);
while(E[i]!= '# ')
{
if(E[i]== '( '||E[i]== '[ ')
{
Push(top,E[i]);
}
if(E[i]== ') '||E[i]== '] ')
{
if(Emptystack(top))
{
return (0);
}
else
{
x=Pop(top);
if(x== '( '&&E[i]== '] '||x== '[ '&&E[i]== ') ')
{
return (0);
}
}
}
i++;
}
//cout < < "... " < <endl;
if(Emptystack(top))
{
//cout < < "??? " < <endl;
return (1);
}
else
{
//cout < < "??? " < <endl;
return (0);
}
}
--------------------Configuration: 表达式括号匹配的检验 - Win32 Debug--------------------
废话不多说,看代码。
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef char datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//ERROR
void Push(slink& top,datatype x)
{
slink p;
if(Emptystack(top))
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
free(p);
}
else
{
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
//free(p);
}
}
datatype Pop(slink& top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
/******************************************************/
/**** 表达式括号匹配的检验.cpp ****/
/******************************************************/
# include "stack.h "
# include <iostream.h>
int bdsxgs(char E[]);
void main()
{
char a[]= "A[(I-2)*4]+3# ";
if(bdsxgs(a))
{
cout < < "括号匹配。 " < <endl;
}
else
{
cout < < " 括号不匹配. " < <endl;
}
}
int bdsxgs(char E[])
{
int i=0;
char x;
slink top;
Clearstack(top);
while(E[i]!= '# ')
{
if(E[i]== '( '||E[i]== '[ ')
{
Push(top,E[i]);
}
if(E[i]== ') '||E[i]== '] ')
{
if(Emptystack(top))
{
return (0);
}
else
{
x=Pop(top);
if(x== '( '&&E[i]== '] '||x== '[ '&&E[i]== ') ')
{
return (0);
}
}
}
i++;
}
//cout < < "... " < <endl;
if(Emptystack(top))
{
//cout < < "??? " < <endl;
return (1);
}
else
{
//cout < < "??? " < <endl;
return (0);
}
}
--------------------Configuration: 表达式括号匹配的检验 - Win32 Debug--------------------