栈和队列面试题

本文探讨了栈和队列在面试中的常见问题,包括如何使用两个栈实现一个队列,两个队列构建栈,创建最小栈,验证字符串的出栈顺序,以及如何用一个数组实现两个共享栈。这些题目旨在考察对数据结构的理解和应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈和队列的实现方式参考博客:https://blog.youkuaiyun.com/cx2479750196/article/details/80493848

1、两个栈实现一个队列


typedef struct QueuewithStack{
    SeqStack entry;
    SeqStack exit;
}QueuewithStack;
typedef char QueueType;
****************************************************************************************************************************
void QueuewithStackInit(QueuewithStack* q){
    if (q == NULL){
        return;
    }
    SeqStackInit(&q->entry);
    SeqStackInit(&q->exit);

}
void QueuewithStackPush(QueuewithStack* q,QueueType value){
    if (q == NULL){
        return;
    }
    QueueType tmp;//查看exit中是否有元素,循环取栈顶元素插入entry中
    while (SeqStackFindTop(&q->exit, &tmp)){
        SeqStackPop(&q->exit);
        SeqStackPush(&q->entry, tmp);
    }
    //此时exit为空
    SeqStackPush(&q->entry, value);
}
void QueuewithStackPop(QueuewithStack* q){
    if (q == NULL){
        return;
    }
    QueueType tmp;//出队列时要将数据放在exit中然后不断pop
    while (SeqStackFindTop(&q->entry, &tmp)){
        SeqStackPop(&q->entry);
        SeqStackPush(&q->exit, tmp);
    }
    SeqStackPop(&q->exit);
}
int QueuewithStack_FindTop(QueuewithStack* q, QueueType* value){
    if (q == NULL||value==NULL){
        return 0;
    }
    QueueType tmp;//将entry中的元素倒腾到exit中,然后才能准确的取出队首元素
    while (SeqStackFindTop(&q->entry, &tmp)){
        SeqStackPop(&q->entry);
        SeqStackPush(&q->exit, tmp);
    }
    int ret=SeqStackFindTop(&q->exit,value);
    return ret;
}

2、两个队列实现一个栈


typedef struct StackwithQueue{
    SeqQueue q1;
    SeqQueue q2;
}StackwithQueue;
typedef char  StackwithQueueType;
void Init(StackwithQueue* Sq){
    if (Sq == NULL){
        return;
    }
    QueueInit(&Sq->q1);
    QueueInit(&Sq->q2);

}
void Push(StackwithQueue* Sq, StackwithQueueType data){
    if (Sq == NULL){
        return;
    }
    //无论如何不可能两个队同时为空,也不可能两个队同时不为空,
    /*if (SeqQueueSize(&Sq->q1)>0){
        QueuePush(&Sq->q1, data);
        return;
    }
    QueuePush(&Sq->q2,data);*/
    SeqQueue* entry = SeqQueueSize(&Sq->q1) > 0 ? &Sq->q1 : &Sq->q2;
    QueuePush(entry, data);
}
void Pop(StackwithQueue* Sq){
    if (Sq == NULL){
        return;
    }
    size_t size1 = SeqQueueSize(&Sq->q1);
    size_t size2 = SeqQueueSize(&Sq->q2);
    if (size1 ==0&& size2 == 0){
        return;//空栈
    }//两个队列不可能同时为空,也不可能同时不为空
    SeqQueue* exit = size1 > 0 ? &Sq->q1 : &Sq->q2;
    SeqQueue* backup = size1 ==0 ? &Sq->q1 : &Sq->q2;//重新分配角色
    while (SeqQueueSize(exit) > 1){
        StackwithQueueType tmp;
        FindHeadElem(exit,&tmp);
        QueuePop(exit);
        QueuePush(backup, tmp);
    }
    QueuePop(exit);
}
int  FindStackTop(StackwithQueue* Sq,StackwithQueueType* value){
    if (Sq == NULL||value==NULL){
        return 0;
    }
    size_t size1 = SeqQueueSize(&Sq->q1);
    size_t size2 = SeqQueueSize(&Sq->q2);
    if (size1 == 0 && size2 == 0){
        return 0;
    }
    //两个队列不可能同时为空,也不可能同时不为空
    SeqQueue* exit = size1 > 0 ? &Sq->q1 : &Sq->q2;
    SeqQueue* backup = size1 == 0 ? &Sq->q1 : &Sq->q2;
    StackwithQueueType tmp;
    while (SeqQueueSize(exit) > 1){
        FindHeadElem(exit, &tmp);
        QueuePop(exit);
        QueuePush(backup, tmp);
    }
     FindHeadElem(exit, &tmp);//返回队首元素,成功ret=0;
    /*while (SeqQueueSize(exit) > 0){
        FindHeadElem(exit, &tmp);
        QueuePop(exit);
        QueuePush(backup, tmp);
    }*/
     *value = tmp;
    return 1;
}

3、建立最小栈


方法1
typedef StackType MinStackType;

typedef struct MinStack{
    SeqStack data;
}MinStack;

void MinStackInit(MinStack* min_stack){
    if (min_stack == NULL){
        return;
    }
    SeqStackInit(&min_stack->data);
}
void MinStackPush(MinStack* min_stack, MinStackType data){
    if (min_stack == NULL){
        return;
    }
    MinStackType value;
    int ret = SeqStackFindTop(&min_stack->data, &value);
    if (ret == 0){
        SeqStackPush(&min_stack->data, data);
        SeqStackPush(&min_stack->data, data);
        return;
    }
    MinStackType min = value < data ? value : data;
    SeqStackPush(&min_stack->data, data);
    SeqStackPush(&min_stack->data, min);
}
void MinStackPop(MinStack* min_stack){
    if (min_stack == NULL){
        return;
    }
    SeqStackPop(&min_stack->data);
    SeqStackPop(&min_stack->data);

}
int MinStackTop(MinStack* min_stack, MinStackType* value){
    if (min_stack == NULL){
        printf("空栈,取栈顶元素失败");
    }
    SeqStackFindTop(&min_stack->data, value);
}
方法2、
typedef struct MinStack{
    SeqStack data;//建立两个栈
    SeqStack min;
}MinStack;
typedef StackType MinStackType;
void  MinStackInit(MinStack* min_stack){
    if (min_stack == NULL){
        return;
    }
    SeqStackInit(&min_stack->data);
    SeqStackInit(&min_stack->min);

}
void MinStackPush(MinStack* min_stack, MinStackType data){
    if (min_stack == NULL){
        return;
    }
    MinStackType tmp;
    int ret = SeqStackFindTop(&min_stack->min,&tmp);//tmp是输出型参数,会带回找到的栈顶元素
    if (ret == 0){
        SeqStackPush(&min_stack->data, data);
        SeqStackPush(&min_stack->min, data);//空栈
        return;
    }
    //比较与当前元素谁大,小的放在min栈中
    MinStackType min = tmp > data ? data : tmp;
    SeqStackPush(&min_stack->min, min);
    SeqStackPush(&min_stack->data, data);
}
void MinStackPop(MinStack* min_stack){
    if (min_stack == NULL){
        return;
    }
    SeqStackPop(&min_stack->data);
    SeqStackPop(&min_stack->min);

}
int MinStackTop(MinStack* min_stack, MinStackType* value){
    if (min_stack == NULL){
        printf("空栈,取栈顶元素失败");
    }
    SeqStackFindTop(&min_stack->min, value);
}

4、判定字符串是否按照出栈顺序


int IsStackOrder(char input[], size_t input_size, char output[], size_t output_size){
    size_t output_index = 0;
    size_t i = 0;
    SeqStack seq;
    SeqStackInit(&seq);
    for (; i < input_size; i++){
        SeqStackPush(&seq, input[i]);
        char top;
        while (SeqStackFindTop(&seq, &top)){
            if (output_index>output_size){
                return 0;
            }
            if (top == output[output_index]){
                ++output_index;
                SeqStackPop(&seq);
            }
            else{
                break;
            }
        }
    }
    if (output_size == output_index&&SeqSize(&seq) == 0){
        return 1;
    }
    return 0;
}
int main(){
    char input[] = { 'a', 'b', 'c', 'd', 'e' };
    char output[] = { 'e', 'd', 'c', 'e', 'a' };
    int size1 = sizeof(input) / sizeof(input[0]);
    int size2 = sizeof(output) / sizeof(output[0]);
    int ret = IsStackOrder(input, size1, output, size2);
    printf("%d\n", ret);
    system("pause");
    return 0;
}

5、共享栈(一个数组实现两个栈)


typedef char SharedType;
#define SharedMaxsize 100
typedef struct SharedStack{
    SharedType data[SharedMaxsize];
    size_t top1;
    size_t top2;
}SharedStack;
void Init(SharedStack* sk){
    if (sk == NULL){
        return;
    }
    sk->top1 = 0;
    sk->top2 = SharedMaxsize - 1;
}
void Push1(SharedStack* sk, SharedType data){
    if (sk == NULL){
        return;
    }
    if (sk->top1 >= sk->top2){
        return;//栈已经满了
    }
    sk->data[sk->top1] = data;
    sk->top1++;
}
void Push2(SharedStack* sk, SharedType data){
    if (sk == NULL){
        return;
    }
    if (sk->top1 >= sk->top2){
        return;//栈已经满了
    }
    sk->data[sk->top2] = data;
    sk->top2--;
}
void Pop1(SharedStack* sk){
    if (sk == NULL){
        return;
    }
    if (sk->top1 == 0){
        return;
    }
    --(sk->top1); 
}
void Pop2(SharedStack* sk){
    if (sk == NULL){
        return;
    }
    if (sk->top2 ==SharedMaxsize-1){
        return;
    }
    ++(sk->top2);
}
int SharedTop1(SharedStack* sk, SharedType* value){
    if (sk == NULL||value==NULL){
        return 0;
    }
    if (sk->top1 == 0){
        return 0;
    }
    *value = sk->data[sk->top1 - 1];
    return 1;
}
int SharedTop2(SharedStack* sk, SharedType* value){
    if (sk == NULL || value == NULL){
        return 0;
    }
    if (sk->top1 == 0){
        return 0;
    }
    *value = sk->data[sk->top2];
    return 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值