用到之前的C实现链表
http://blog.youkuaiyun.com/edwards_june/article/details/54380468
int isPalindrome(LNode *s, int n) { //n是长度
if (0 > n) {
return 0;
} else {
LNode *node = s;
int index = n / 2 + (n % 2 == 0 ? 0 : 1);
int i = index + 1;
Type data;
StackLink *p = (StackLink*) malloc(sizeof(StackLink));
initStack(p);
for (; i <= n; i++) {
if ((data = get(&node, i)) != NULL) {
push(p, data);
}
}
int length = getStackLength(p);
ElemType elem;
while (length-- > 0) {
pop(p, &elem);
if (node->data != elem) {
return 0;
}
node = node->next;
}
return 1;
}
}
本文介绍了一种使用链表和栈来判断链表是否为回文的方法。通过遍历链表后半部分并将其元素压入栈中,再与前半部分进行对比,实现了高效的回文检测。
1693

被折叠的 条评论
为什么被折叠?



