代码实现
#include
#include
#include
#define ElemType char
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkStack;
void InitStack(LinkStack *stack) {
*stack = NULL;
}
bool StackEmpty(LinkStack stack) {
if (stack == NULL)
return true;
return false;
}
bool Push(LinkStack *stack, ElemType e) {
LinkStack new = (LinkStack) malloc(sizeof(LNode));
if (new == NULL)
return false;
new->data = e;
new->next = *stack;
*stack = new;
return true;
}
bool Pop(LinkStack *stack, ElemType *e) {
if (StackEmpty(*stack))
return false;
*e = (*stack)->data;
LinkStack old = *stack;
*stack = (*stack)->next;
free(old);
return true;
}
bool GetTop(LinkStack stack, ElemType *e) {
if (StackEmpty(stack))
return false;
*e = stack->data;
return true;
}
void DestroyStack(LinkStack *stack) {
while (*stack != NULL) {
LNode *new = *stack;
*stack = (*stack)->next;
free(new);
}
}
bool match(LinkStack stack, int n) {
int i;
char data[n / 2];
for (i = 0; i < n / 2; i++) {
data[i] = stack->data;
stack = stack->next;
}
i--;
if(n%2 == 0){
for(;i>0;i--){
if(data[i] != stack->data)
return false;
stack = stack->next;
}
return true;
}
else{
for(;i>0;i--){
if(data[i] != stack->data)
return false;
stack = stack->next;
}
}
}
int Length(LinkStack stack){
int num = 0;
while(stack != NULL){
num++;
stack = stack->next;
}
return num;
}
int main() {
LinkStack stack;
ElemType Elem;
InitStack(&stack);
Push(&stack, 'a');
Push(&stack, 'b');
Push(&stack, 'a');
if(match(stack,Length(stack)))
printf("该链表为中心对称\n");
else
printf("该链表不是中心对称\n");
Push(&stack, 'a');
Push(&stack, 'b');
Push(&stack, 'a');
if(match(stack,Length(stack)))
printf("该链表为中心对称\n");
else
printf("该链表不是中心对称\n");
Push(&stack, 'a');
if(match(stack,Length(stack)))
printf("该链表为中心对称\n");
else
printf("该链表不是中心对称\n");
DestroyStack(&stack);
return 0;
}