这是个符号匹配的题,其中match函数的循环体好像只能循环一次就退出了,其中create,入栈出栈还有打印链表的函数应该是没有问题的,求code改正
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char fuhao;
struct node* next;
}node;
void stack_create_r(node* l, char str[], int length)
{
if (l)
{
l->next = NULL;
node* r, * p;
r = l;
for (int i = 0; i < length; i++)
{
p = (node*)malloc(sizeof(node));
p->fuhao = str[i];
r->next = p;
r = p;
}
r->next = NULL;
}
}
void disp(node* l)
{
if (l)
{
node* p;
p = l;
while (p->next != NULL)
{
p = p->next;
printf("%c\t", p->fuhao);
}
printf("\n");
}
}
void push(node* l, char x)
{
if (l)
{
node* p;
p = (node*)malloc(sizeof(node));
p->fuhao = x;
if (l->next == NULL)
l->next = p;
else
{
p->next = l->next;
l->next = p;
}
}
}
void pop(node *l)
{
if (l)
{
node* p;
if (l->next == NULL)
{
printf("match failed!\n");
return;
}
if (l->next->next == NULL)
{
free(l->next);
l->next = NULL;
}
else
{
p = l->next;
l->next = l->next->next;
free(p);
}
}
}
int match(node* l)
{
if (l)
{
node* zhan,*p;
zhan = (node*)malloc(sizeof(node));
zhan->next = NULL;
p = l;
while (p->next != NULL)
{
p = p->next;
if (p->fuhao == '[')
push(zhan, p->fuhao);
if (p->fuhao == '(')
push(zhan, p->fuhao);
if (p->fuhao == '{')
push(zhan, p->fuhao);
if (p->fuhao == ']')
{
if (zhan->next->fuhao == '[')
pop(zhan);
else
return 0;
}
if (p->fuhao == ')')
{
if (zhan->next->fuhao == '(')
pop(zhan);
else
return 0;
}
if (p->fuhao == '}')
{
if (zhan->next->fuhao == '{')
pop(zhan);
else
return 0;
}
}
return 1;
}
}
int main()
{
char str[100];
int length,status=-1;
node* l;
l = (node*)malloc(sizeof(node));
printf("please input the bracket string:\n");
scanf("%s", str);
length = strlen(str);
stack_create_r(l, str, length);
disp(l);
status = match(l);
if (status == 1)
printf("match succeed\n");
if (status == 0)
printf("match failed");
return 0;
}
https://blog.csdn.net/fuluoyide312/article/details/107073901
我做过的,3.18好像是一样的题,要不然你参考下。