数据结构之链栈的实现

链栈的进栈出栈操作
#include <stdio.h>
#include <malloc.h>
typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode;

LNode* initStack(LNode *&stack){
    stack = (LNode*)malloc(sizeof(LNode));
    stack->next = NULL;
    return stack;
}
int isEmpty(LNode *stack){
    if(stack->next==NULL) return 1;
    else return 0;
}
//头插法
void push(LNode *&stack,int x){
    LNode *s = (LNode*)malloc(sizeof(LNode));
    s->data = x;
    s->next = stack->next;
    stack->next = s;
    printf("   %d is pushed!\r\n",x);
}
//y由于采用头插法,故最后插入的节点在链表的头部,故删除头结点的下一个节点就可以
void pop(LNode *&stack,int &e){
    LNode *q;
    q = stack->next;
    stack ->next = stack->next->next;
    printf("   %d is poped!\r\n",q->data);
    free(q);    

}
void print(LNode *stack){
    LNode *p;
    p=stack->next;
    printf("\r\n form top to bottom\r\n");
    while(p){
     printf("%d ",p->data);
     p=p->next;
    }

}
int main()
{
    LNode* stack;
    int e;
    initStack(stack);
    push(stack,15);
    push(stack,19);
    push(stack,128);
    push(stack,17);
    print(stack);
    pop(stack,e);
    printf("\r\n");
    print(stack);
    return 0;
}
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值