push and pop list_stack ,get mindata ,print it

实现一个特殊的栈,在实现栈的基础上,在实现返回栈中最小元素的操作。

int if_push_stack(lstack * datastack,lstack *minstack,datatype data)
{
    datastack=push_stack(datastack,data);
    if(!datastack)
        return -1;
    if(minstack==NULL)
    {
        minstack=push_stack(minstack,data);
    }
    else if(minstack->top->data>=data)
    {
        minstack=push_stack(minstack,data);
    }
    else
    printf("****\n");
    return 0;
}
int if_pop_stack(lstack * datastack,lstack *minstack)
{
    if(datastack->top==NULL||minstack->top==NULL)
        return -1;
    if(minstack->top->data==datastack->top->data)
        pop_stack(minstack);
    if(minstack->top->data<datastack->top->data)
        printf("no pop\n");
    datastack=pop_stack(datastack);
    return 0;
}
datatype get_mindata (lstack * ls)
{
    if(ls->top==NULL)
        return -1;
    return ls->top->data;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一份示例代码,包含了堆栈和队列的数组、链表实现。代码分为头文件和源文件,使用 make 命令进行编译和链接。 stack.h: ``` #ifndef STACK_H #define STACK_H #define MAX_STACK_SIZE 100 void stack_array_push(int element); int stack_array_pop(); void stack_list_push(int element); int stack_list_pop(); #endif ``` stack_array.c: ``` #include "stack.h" int stack_array[MAX_STACK_SIZE]; int top = 0; void stack_array_push(int element) { if (top == MAX_STACK_SIZE) { printf("Stack overflow!\n"); return; } stack_array[top++] = element; } int stack_array_pop() { if (top == 0) { printf("Stack underflow!\n"); return -1; } return stack_array[--top]; } ``` stack_list.c: ``` #include "stack.h" #include <stdlib.h> typedef struct stack_node { int element; struct stack_node* next; } StackNode; StackNode* top = NULL; void stack_list_push(int element) { StackNode* new_node = (StackNode*)malloc(sizeof(StackNode)); new_node->element = element; new_node->next = top; top = new_node; } int stack_list_pop() { if (top == NULL) { printf("Stack underflow!\n"); return -1; } int element = top->element; StackNode* old_top = top; top = top->next; free(old_top); return element; } ``` queue.h: ``` #ifndef QUEUE_H #define QUEUE_H #define MAX_QUEUE_SIZE 100 void queue_array_put(int element); int queue_array_get(); void queue_list_put(int element); int queue_list_get(); #endif ``` queue_array.c: ``` #include "queue.h" int queue_array[MAX_QUEUE_SIZE]; int front = 0; int rear = 0; void queue_array_put(int element) { if (rear == MAX_QUEUE_SIZE) { printf("Queue overflow!\n"); return; } queue_array[rear++] = element; } int queue_array_get() { if (front == rear) { printf("Queue underflow!\n"); return -1; } return queue_array[front++]; } ``` queue_list.c: ``` #include "queue.h" #include <stdlib.h> typedef struct queue_node { int element; struct queue_node* next; } QueueNode; QueueNode* front = NULL; QueueNode* rear = NULL; void queue_list_put(int element) { QueueNode* new_node = (QueueNode*)malloc(sizeof(QueueNode)); new_node->element = element; new_node->next = NULL; if (rear == NULL) { front = rear = new_node; } else { rear->next = new_node; rear = new_node; } } int queue_list_get() { if (front == NULL) { printf("Queue underflow!\n"); return -1; } int element = front->element; QueueNode* old_front = front; front = front->next; if (front == NULL) { rear = NULL; } free(old_front); return element; } ``` main.c: ``` #include <stdio.h> #include "stack.h" #include "queue.h" int main() { // Stack array implementation printf("Stack array implementation:\n"); stack_array_push(1); stack_array_push(2); stack_array_push(3); printf("%d\n", stack_array_pop()); printf("%d\n", stack_array_pop()); printf("%d\n", stack_array_pop()); printf("%d\n", stack_array_pop()); // should print "Stack underflow!" // Stack list implementation printf("Stack list implementation:\n"); stack_list_push(1); stack_list_push(2); stack_list_push(3); printf("%d\n", stack_list_pop()); printf("%d\n", stack_list_pop()); printf("%d\n", stack_list_pop()); printf("%d\n", stack_list_pop()); // should print "Stack underflow!" // Queue array implementation printf("Queue array implementation:\n"); queue_array_put(1); queue_array_put(2); queue_array_put(3); printf("%d\n", queue_array_get()); printf("%d\n", queue_array_get()); printf("%d\n", queue_array_get()); printf("%d\n", queue_array_get()); // should print "Queue underflow!" // Queue list implementation printf("Queue list implementation:\n"); queue_list_put(1); queue_list_put(2); queue_list_put(3); printf("%d\n", queue_list_get()); printf("%d\n", queue_list_get()); printf("%d\n", queue_list_get()); printf("%d\n", queue_list_get()); // should print "Queue underflow!" return 0; } ``` Makefile: ``` CC = gcc CFLAGS = -Wall -Wextra -Wpedantic -std=c99 all: main main: main.o stack_array.o stack_list.o queue_array.o queue_list.o $(CC) $(CFLAGS) -o $@ $^ main.o: main.c stack.h queue.h $(CC) $(CFLAGS) -c $< stack_array.o: stack_array.c stack.h $(CC) $(CFLAGS) -c $< stack_list.o: stack_list.c stack.h $(CC) $(CFLAGS) -c $< queue_array.o: queue_array.c queue.h $(CC) $(CFLAGS) -c $< queue_list.o: queue_list.c queue.h $(CC) $(CFLAGS) -c $< clean: rm -f main *.o ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值