堆栈的部分基本操作

#include
#define MAXSIZE 100
#define DataType int

typedef struct/*定义一个栈类型*/
{
    DataType array[MAXSIZE];
    int top;
}stack;

/*初始化栈*/

int initstack(stack *stacks)
{
    stacks->top = 0;

    return 0;
};

/*将数据data压入栈stacks*/

int push(DataType data , stack *stacks)
{
    stacks->top++;

    if (stacks->top == MAXSIZE)/*判断栈是否已满*/
    {
        puts("The stack is full!");
        return 1;
    }

    stacks->array[stacks->top] = data;
   
    return 0;
};

/*从栈stacks中取一个元素*/

DataType pop(stack *stacks)
{
    DataType data;

    if (stacks->top == 0)
    {
        puts("The stack is empty!");
        return 1;
    }

    data = stacks->array[stacks->top];

    stacks->top--;

    return data;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是您需要的程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 // 堆栈结构体定义 typedef struct { char data[MAXSIZE]; int top; } Stack; // 队列结构体定义 typedef struct { char data[MAXSIZE]; int front; int rear; } Queue; // 创建堆栈 Stack *createStack() { Stack *s = (Stack *)malloc(sizeof(Stack)); s->top = -1; return s; } // 判断堆栈是否为空 int isEmptyStack(Stack *s) { return s->top == -1; } // 判断堆栈是否已满 int isFullStack(Stack *s) { return s->top == MAXSIZE - 1; } // 入栈 void push(Stack *s, char c) { if (isFullStack(s)) { printf("Stack is full\n"); return; } s->data[++(s->top)] = c; } // 出栈 char pop(Stack *s) { if (isEmptyStack(s)) { printf("Stack is empty\n"); return '\0'; } return s->data[(s->top)--]; } // 创建队列 Queue *createQueue() { Queue *q = (Queue *)malloc(sizeof(Queue)); q->front = q->rear = 0; return q; } // 判断队列是否为空 int isEmptyQueue(Queue *q) { return q->front == q->rear; } // 判断队列是否已满 int isFullQueue(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front; } // 入队列 void enqueue(Queue *q, char c) { if (isFullQueue(q)) { printf("Queue is full\n"); return; } q->data[q->rear] = c; q->rear = (q->rear + 1) % MAXSIZE; } // 出队列 char dequeue(Queue *q) { if (isEmptyQueue(q)) { printf("Queue is empty\n"); return '\0'; } char c = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return c; } // 将一组输入的数据逆序输出 void reverseInput() { Stack *s = createStack(); char c; printf("Please input a sequence of characters: "); while ((c = getchar()) != '\n') { push(s, c); } printf("Reverse sequence: "); while (!isEmptyStack(s)) { putchar(pop(s)); } putchar('\n'); } // 判断字符串是否回文 int isPalindrome(char *str) { int len = strlen(str); Stack *s = createStack(); Queue *q = createQueue(); int i; for (i = 0; i < len; i++) { push(s, str[i]); enqueue(q, str[i]); } while (!isEmptyStack(s)) { if (pop(s) != dequeue(q)) { return 0; } } return 1; } int main() { int choice; do { printf("Please select the operation:\n"); printf("1. Reverse input sequence\n"); printf("2. Judge whether the string is palindrome\n"); printf("0. Exit\n"); scanf("%d", &choice); getchar(); switch (choice) { case 1: reverseInput(); break; case 2: { char str[MAXSIZE]; printf("Please input a string: "); fgets(str, MAXSIZE, stdin); if (isPalindrome(str)) { printf("The string is palindrome\n"); } else { printf("The string is not palindrome\n"); } break; } case 0: printf("Exit\n"); break; default: printf("Invalid choice\n"); break; } } while (choice != 0); return 0; } ``` 程序分为四部分: 1. 堆栈基本操作:创建堆栈、判断堆栈是否为空、判断堆栈是否已满、入栈、出栈。 2. 队列的基本操作:创建队列、判断队列是否为空、判断队列是否已满、入队列、出队列。 3. 将一组输入的数据逆序输出。 4. 判断字符串是否回文。 在主函数中,根据用户的选择进行相应的操作,直到用户选择退出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值