1(存疑).
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/418d756b1d9b261e87c529a5f01ef53e.png)
#include <stdio.h>
#include <stdlib.h>
#define TYPE char
#define NODETYPE int
typedef struct Link
{
int data;
struct Link *next;
}Link;
struct Stack
{
NODETYPE *arr;
int len;
int top;
};
Link *createLink(int num) {
int n, data;
char letter;
printf("请输入创建链表的结点个数:");
scanf("%d", &n);
Link *q;
Link *head = (Link*) malloc(sizeof(Link));
head->next = NULL;
q = head;
for (int i = 0; i < n; i++) {
Link *newP = (Link*) malloc(sizeof(Link));
num ? printf("请输入第%d个结点的值(char):",i+1): printf("请输入第%d个结点的值(int):", i + 1);
if (num==0) {
scanf("%d", &data);
newP->data = data;
}
else {
getchar();
scanf("%c", &letter);
newP->data = letter;
}
newP->next = NULL;
head->next = newP;
head = head->next;
}
head->next = NULL;
head = q;
printf("打印链表:");
printf("\n");
while (head->next) {
if (num==0) {
printf("%d ", head->next->data);
}
else {
printf("%c", head->next->data);
}
head = head->next;
}
printf("\n");
head = q;
return head;
}
void isSymmetry(Link *h) {
struct Stack *s;
int size;
struct Stack *createLink(int);
bool push(Stack * , char);
bool empty(Stack *);
char top(Stack *);
bool pop(Stack *);
void destory(Stack *);
printf("请输入要创建的栈的大小:");
scanf("%d", &size);
s = createLink(size);
Link *fast = h->next, *slow = h->next;
while(fast->next && fast->next->next){
fast = fast->next->next;
slow = slow->next;
}
fast = h->next;
while(slow->next){
push(s,slow->next->data);
slow = slow->next;
}
while(!empty(s)){
if(fast->data != top(s)){
printf("该链表非中心对称");
break;
}
fast = fast->next;
pop(s);
}
if(empty(s)){
printf("链表对称");
}
destory(s);
}
Stack *createStack(int size) {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->arr = (NODETYPE*)malloc(sizeof(NODETYPE)*size);
stack->len = size;
stack->top = -1;
return stack;
}
bool full(Stack *stack) {
return stack->top + 1 >= stack->len;
}
bool empty(Stack *stack) {
return stack->top == -1;
}
bool push(Stack *stack, NODETYPE p) {
if (full(stack)) return false;
*(stack->arr + ++stack->top) = p;
return true;
}
bool pop(Stack *stack) {
if (empty(stack)) return false;
stack->top--;
return true;
}
NODETYPE top(Stack *stack) {
if (empty(stack)) return NULL;
return *(stack->arr + stack->top);
}
void destory(Stack *stack) {
free(stack->arr);
free(stack);
}
bool contain(Stack *stack, NODETYPE r) {
if (empty(stack)) return false;
for (int i = stack->top; i >= 0; i--) {
if (r == *(stack->arr + i) ){
return true;
}
}
return false;
}
void print(Stack *stack) {
if (empty(stack)) return;
for (int i = stack->top; i >= 0; i--) {
printf("%c",stack->arr[i]);
}
}
int main() {
Link *head;
head = createLink(0);
isSymmetry(head);
return 0;
}
2(存疑).
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a5ca99fafbd7fb3d1ca7c8e3525f439e.png)
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define TYPE char
struct Stack
{
TYPE* arr;
int top[2];
};
Stack *createStack(int size) {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->arr = (TYPE *)malloc(sizeof(TYPE)*size);
stack->top[0] = -1;
stack->top[1] = size;
return stack;
}
bool full(Stack *stack) {
return stack->top[1]-stack->top[0] == 1;
}
bool empty(int i,Stack *stack,int size) {
switch (i) {
case 0:
return stack->top[0] == -1;
case 1:
return stack->top[0] == size;
}
}
bool push(Stack *stack, TYPE data,int i) {
if (full(stack)) return false;
switch (i) {
case 0:
*(stack->arr + ++stack->top[i]) = data;
case 1:
*(stack->arr + --stack->top[i]) = data;
}
return true;
}
bool pop(int i,Stack *stack,int size) {
if (empty(i,stack,size)) return false;
switch (i) {
case 0:
stack->top[i]--;
case 1:
stack->top[i]++;
}
return true;
}