主题:[讨论]数据结构-——>.数组栈
// < >( ){ }[ ] 括号配对问题 运行时为什么不会出栈
# include <stdio.h>
# include <stdlib.h>
# define N 200
struct shu_zu
{
char ch;
int i;
}emp[N];
//初始化
void Init_LS(struct shu_zu* p,int n);
//分配内存单元
int Malloc_LS(struct shu_zu* p);
//释放内存单元
void Free_LS(struct shu_zu* p,int n);
int main()
{
char ch1;
struct shu_zu* head;
int k;//栈顶
int j;
head = emp;
Init_LS(head,N);
k = Malloc_LS(head);
if(k == 0)
{
printf("Allocation memory error!!!\n");
exit(0);
}
(head+k)->i = 0;
printf("Input character:\n");
ch1 = getchar();
for(;ch1 !='\n';)
{
if(ch1 =='('||ch1 ==')'||ch1 =='['||ch1 ==']'||ch1 =='{'||ch1 =='}'||ch1 =='<'||ch1 =='>')
{
if( ((head+k)->i != 0) &&
(
( ch1 =='('&&(head+k)->ch == ')' )
||( ch1 =='['&&(head+k)->ch == ']' )
||( ch1 =='{'&&(head+k)->ch == '}' )
||( ch1 =='<'&&(head+k)->ch == '>' )
)
)
{//出栈
j = (head+k)->i;
Free_LS(head,k);
k = j;
}
else
{//进栈
j =Malloc_LS(head);
if(j == 0)
{
printf("Allocation memory error!!!\n");
exit(0);
}
(head+j)->ch = ch1;
(head+j)->i = k;
k = j;
}
}
ch1 = getchar();
}
if((head+k)->i == 0)
printf("Success!!!\n");
else
printf("Unsuccess!!!\n");
system("pause");
return 0;
}
//初始化
void Init_LS(struct shu_zu* p,int n)
{
int i;
for(i = 0; i < n -1; ++i)
(p + i)->i = i+1;
}
//分配内存单元
int Malloc_LS(struct shu_zu* p)
{
int k;
k = p->i;
if(k != 0)
p->i = (p+p->i)->i;
return k;
}
//释放内存单元
void Free_LS(struct shu_zu*p,int n)
{
(p + n)->i = p->i;
p ->i = n;
}
# include <stdio.h>
# include <stdlib.h>
# define N 200
struct shu_zu
{
char ch;
int i;
}emp[N];
//初始化
void Init_LS(struct shu_zu* p,int n);
//分配内存单元
int Malloc_LS(struct shu_zu* p);
//释放内存单元
void Free_LS(struct shu_zu* p,int n);
int main()
{
char ch1;
struct shu_zu* head;
int k;//栈顶
int j;
head = emp;
Init_LS(head,N);
k = Malloc_LS(head);
if(k == 0)
{
printf("Allocation memory error!!!\n");
exit(0);
}
(head+k)->i = 0;
printf("Input character:\n");
ch1 = getchar();
for(;ch1 !='\n';)
{
if(ch1 =='('||ch1 ==')'||ch1 =='['||ch1 ==']'||ch1 =='{'||ch1 =='}'||ch1 =='<'||ch1 =='>')
{
if( ((head+k)->i != 0) &&
(
( ch1 =='('&&(head+k)->ch == ')' )
||( ch1 =='['&&(head+k)->ch == ']' )
||( ch1 =='{'&&(head+k)->ch == '}' )
||( ch1 =='<'&&(head+k)->ch == '>' )
)
)
{//出栈
j = (head+k)->i;
Free_LS(head,k);
k = j;
}
else
{//进栈
j =Malloc_LS(head);
if(j == 0)
{
printf("Allocation memory error!!!\n");
exit(0);
}
(head+j)->ch = ch1;
(head+j)->i = k;
k = j;
}
}
ch1 = getchar();
}
if((head+k)->i == 0)
printf("Success!!!\n");
else
printf("Unsuccess!!!\n");
system("pause");
return 0;
}
//初始化
void Init_LS(struct shu_zu* p,int n)
{
int i;
for(i = 0; i < n -1; ++i)
(p + i)->i = i+1;
}
//分配内存单元
int Malloc_LS(struct shu_zu* p)
{
int k;
k = p->i;
if(k != 0)
p->i = (p+p->i)->i;
return k;
}
//释放内存单元
void Free_LS(struct shu_zu*p,int n)
{
(p + n)->i = p->i;
p ->i = n;
}