数据结构-链栈

#include<stdio.h>
#include<malloc.h>

#define true 1
#define false 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define yes 1
#define no 0

typedef int ElemType;
typedef int Status;
typedef struct Node{
	ElemType data;
	struct Node *next;
}Stack,*LinkStack;

LinkStack InitStack(LinkStack top)
{
	top = (LinkStack)malloc(sizeof(Stack));
	top->next = NULL;
	return top;
}

LinkStack PushLinkStack(LinkStack top,ElemyType e)
{
	LinkStack p;
	p = (LinkStack)malloc(sizeof(Stack));
	p->data = e;
	p->next = top->next;
	top->next = p;
	return top;
}
ElemType PopLinkStack(LinkStack top)
{
	LinkStack p;
	ElemType e;
	if(top->next = NULL)
		return false;
	else{
		p = top->next;
		e = p->data;
		top->next = p->next;
		free(p);
	}	
	return e;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
判断回文可以使用链栈或队列来实现。具体实现方法如下: 1.使用链栈实现回文判断 将字符串的前一半依次入栈,然后依次出栈并与字符串的后一半进行比较,如果全部相等,则为回文。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 typedef struct Node { char data; struct Node* next; } Node; typedef struct Stack { Node* top; } Stack; Stack* initStack() { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->top = NULL; return stack; } void push(Stack* stack, char data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = stack->top; stack->top = node; } char pop(Stack* stack) { if (stack->top == NULL) { return '\0'; } char data = stack->top->data; Node* temp = stack->top; stack->top = stack->top->next; free(temp); return data; } int isPalindrome(char* str) { int len = strlen(str); Stack* stack = initStack(); for (int i = 0; i < len / 2; i++) { push(stack, str[i]); } for (int i = len / 2 + len % 2; i < len; i++) { if (pop(stack) != str[i]) { return 0; } } return 1; } int main() { char str[MAXSIZE]; printf("请输入字符串:"); scanf("%s", str); if (isPalindrome(str)) { printf("是回文\n"); } else { printf("不是回文\n"); } return 0; } ``` 2.使用队列实现回文判断 将字符串的前一半依次入队,然后依次出队并与字符串的后一半进行比较,如果全部相等,则为回文。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 typedef struct Queue { char* data; int front; int rear; } Queue; Queue* initQueue() { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->data = (char*)malloc(sizeof(char) * MAXSIZE); queue->front = queue->rear = 0; return queue; } void enqueue(Queue* queue, char data) { queue->data[queue->rear] = data; queue->rear = (queue->rear + 1) % MAXSIZE; } char dequeue(Queue* queue) { if (queue->front == queue->rear) { return '\0'; } char data = queue->data[queue->front]; queue->front = (queue->front + 1) % MAXSIZE; return data; } int isPalindrome(char* str) { int len = strlen(str); Queue* queue = initQueue(); for (int i = 0; i < len / 2; i++) { enqueue(queue, str[i]); } for (int i = len / 2 + len % 2; i < len; i++) { if (dequeue(queue) != str[i]) { return 0; } } return 1; } int main() { char str[MAXSIZE]; printf("请输入字符串:"); scanf("%s", str); if (isPalindrome(str)) { printf("是回文\n"); } else { printf("不是回文\n"); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是洋洋a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值