Stack and queue interview questions (2)

1: The validity of the element popping and stacking sequence. For example, the stack sequence is (1,2,3,4,5), and the popup sequence is (4,5,3,2,1).

Idea: Suppose there are two arrays arr1[] and arr2[], respectively saving the sequence of popping and pushing, there is an index index starting from 0, pointing to arr1, there is an index outdex that points to arr2 starting from 0, Use arr1[index] to compare with arr2[outdex], if they are equal, index++, outdex++, and then continue to compare, if they are not equal, index++, the above two sequences are equal for the first time, the pointers of inde and outdex are as shown in the figure below shown,
write picture description here
then continue to walk until any array exceeds the bounds, as shown in the following figure:
write picture description here

At this point, the index has crossed the boundary, but the outdex still points to the array, and we have not finished the comparison, so we need a data structure to save the data that we didn't want to wait before, and this data structure is the stack. The specific code is implemented as follows: In the code For the operation of the stack used, please refer to
https://blog.csdn.net/virgofarm/article/details/80065574

#include<stdio.h>
#include<assert.h>
#include<windows.h>

int StackIsLegal(int* arr, int size, int* arr2, int size2)
{
    Stack s;
    StackInit(&s);
    int index = 0;
    int outdex = 0;

    assert(arr);
    assert(arr2);

    //两个数组大小都不一样,肯定不合法
    if (size != size2)
        return 0;

    while (index < size)
    {
        //如果两个数组中的内容不相等,就让arr中的元素入栈
        if (arr[index] != arr2[outdex] && index < size)
        {
            StackPush(&s, arr[index]);
            index++;
        }

        //相等
        if (arr[index] == arr2[outdex])
        {
            index++;
            outdex++;
        }
    }

    while (!StackEmpty(&s))
    {
        //如果数组arr访问完了,就拿栈顶元素
        if (StackTop(&s) == arr2[outdex])
        {
            StackPop(&s);
            outdex++;
        }

        //没进入if肯定不合法
        return 0;
    }

    if (outdex >= size2)
        return 1;
    else
        return 0;
}



int main()
{

    int arr[] = { 1, 2, 3, 4, 5 };
    int arr2[] = { 4, 5, 3, 2, 1 };

    int size = sizeof(arr) / sizeof(arr[0]);
    int size2 = sizeof(arr2) / sizeof(arr2[0]);

    printf("%d\n", StackIsLegal(arr, size, arr2, size2));
    system("pause");
    return 0;
}

Two: One array implements two stacks (shared stack)

Idea: The idea of ​​​​this question is relatively simple. We can divide the array into two parts. The even numbered subscript is packaged as a stack, and the subscripted odd number is packaged into a stack, so that one array can be used to implement two stacks, but this way It's not very good, because assuming that you only insert elements into the even stack until the even stack is full, but in fact the array still has space, but you can't insert more elements, which causes a lot of wasted space. The specific code is implemented as follows:

StackAndInterview.c

#include "StackAndInterview.h"


void StackInit(PSStack s)
{
    assert(s);

    s->Top1 = 0;//偶数栈从0开始
    s->Top2 = 1;//奇数栈从1开始
}

void StackPush(PSStack s, int flag, DataType data)
{
    assert(s);

    if (flag == 1)//入到偶数栈
    {
        if (s->Top1 == MAX_SIZE)
        {
            printf("栈已满!\n");
            return;
        }

        s->arr[s->Top1] = data;
        s->Top1 += 2;
    }

    else
    {
        if (s->Top1 > MAX_SIZE)
        {
            printf("栈已满!\n");
            return;
        }

        s->arr[s->Top2] = data;
        s->Top2 += 2;
    }
}

void StackPop(PSStack s, int flag)
{
    assert(s);

    if (flag == 1)//出栈1
    {
        if (s->Top1 == 0)
        {
            printf("栈已空!\n");
            return;
        }

        s->Top1 -= 2;
    }

    else
    {
        if (s->Top2 == 1)
        {
            printf("栈已空!!!\n");
            return;
        }

        s->Top2 -= 2;
    }
}

int StackSize(PSStack s, int flag)
{
    assert(s);

    if (flag == 1)
        return s->Top1 / 2;
    else
        return s->Top2 / 2;
}

int StackEmpty(PSStack s, int flag)
{
    assert(s);

    if (flag == 1)
    {
        if (s->Top1 == 0)
            return 1;
        return 0;
    }

    else
    {
        if (s->Top2 == 1)
            return 1;

        else
            return 0;
    }
}

DataType StackTop(PSStack s, int flag)
{
    assert(s);

    if (flag == 1)
    {
        if (s->Top1 == 0)
        {
            printf("栈已空!\n");
            return 0;
        }

        return s->arr[s->Top1 - 2];
    }

    else
    {
        if (s->Top2 == 1)
        {
            printf("栈已空!\n");
            return 0;
        }

        return s->arr[s->Top2 - 2];
    }
}

StackAndInterview.h

#pragma once


#include <assert.h>
#include <stdio.h>

#define MAX_SIZE 10
typedef int DataType;

typedef struct ShareStack
{
    DataType arr[MAX_SIZE];
    int Top1;//栈1用数组的偶数下标
    int Top2;//栈2用数组的奇数下标

}SStack, *PSStack;


void StackInit(PSStack s);
void StackPush(PSStack s, int flag, DataType data);
void StackPop(PSStack s, int flag);
int StackSize(PSStack s, int flag);
int StackEmpty(PSStack s, int flag);
DataType StackTop(PSStack s, int flag);

test.c

#include "StackAndInterview.h"
#include <Windows.h>



void Test6()
{
    SStack s;

    StackInit(&s);
    StackPush(&s, 1, 1);
    StackPush(&s, 1, 3);
    StackPush(&s, 1, 5);
    StackPush(&s, 1, 7);
    StackPush(&s, 1, 9);
    printf("S1Top = %d\n", StackTop(&s, 1));
    StackPush(&s, 2, 2);
    StackPush(&s, 2, 4);
    StackPush(&s, 2, 6);
    StackPush(&s, 2, 8);
    StackPush(&s, 2, 10);
    printf("S2Top = %d\n", StackTop(&s, 2));

    StackPop(&s, 1);
    printf("S1Top = %d\n", StackTop(&s, 1));
    printf("S1Size = %d\n", StackSize(&s, 1));
    printf("S2Size = %d\n", StackSize(&s, 2));

}

int main()
{
    //Test1();//面试题1方法1
    //Test2();//面试题方法2
    //Test3();//面试题2
    //Test4();//面试题3
    //Test5();//面试题4
    Test6();//面试题5
    system("pause");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325944450&siteId=291194637