使用栈实现数组

发布于 2024-12-15 07:28:26 字数 138 浏览 3 评论 0原文

一种语言没有数组作为数据类型,但它有堆栈作为数据类型,并且可以声明堆栈;并定义了 pushpopisempty 操作。
那么我们如何使用两个栈以及以上操作来实现数组呢?

A language has not array as a data type but it has stack as a data type and one can declare stack's; and push, pop and isempty operations are defined.
So how can we implement array using two stacks and above operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情定在深秋 2024-12-22 07:28:26

效率极其低下 - 但是:

Stack 1 包含详细信息
堆栈 2 为空。

要遍历数组,请弹出堆栈 1 ,当您需要下一个时,请将前一个放入堆栈 2 中,然后再次弹出堆栈 1 。重复直到“isempty”。

如果你想要第N个值,则将非空堆栈弹出N次,同时将不需要的推入另一个堆栈。然后当你玩完它后,将其清空到另一个堆栈中。请注意,这会翻转顺序。

Horribly inefficient - but:

Stack 1 Contains the details
Stack 2 is empty.

To go through the array, Pop Stack 1 , when you want the next one, push the previous one into stack 2 and pop stack 1 again. Repeat until 'isempty'.

If you want the Nth value, pop the not empty stack N times while pushing the unneeded ones into the other stack. Then when you're done playing with it, empty it into the other stack. Note that this;ll flip the order.

靖瑶 2024-12-22 07:28:26

使用两个堆栈,您可以获得某种随机访问(这就是您对数组感兴趣的内容),如下所示:

  • 将所有内容放在堆栈上。
  • 每次您必须访问非堆栈顶部的内容(弹出)时,您都会从该堆栈中弹出所有元素,然后将它们按顺序推入第二个堆栈。

通过将元素从一个堆栈传递到另一个堆栈,可以模拟迭代。

With two stacks you can get some sort of random access (which is what you're interested in an array) like this:

  • Put everything on a stack.
  • Every time you have to access something that's not the top of the stack (pop), you pop all elements from this stack and push them in order to the second one.

By passing elements from one stack to another you simulate iteration.

残花月 2024-12-22 07:28:26

数组的第一个元素是 stack1 的底部;
数组的最后一个元素是 stack2 的底部;
数组的当前元素是 stack1 的顶部;
通过将 stack1 的元素移至 stack2(移至开头)来迭代数组,反之亦然(移至结尾)。

First element of array is a bottom of stack1;
Last element of array is a bottom of stack2;
Current element of array is a top of stack1;
Iterates through the array by shifting the elements of the stack1 to stack2(move to the begin) and vice versa (move to end).

虫児飞 2024-12-22 07:28:26
#include <stdio.h>
#include <stdlib.h>

#define Type int
#define DefaultValue 0

#define bool int

typedef struct stack{
    Type *stack;
    Type *sp;
    size_t size;
} Stack;

Stack* Stack_make(size_t size){
    Stack *s;
    s = (Stack*)malloc(sizeof(Stack));
    s->stack = (Type*)malloc(sizeof(Type)*size);
    s->sp = s->stack -1;
    s->size = size;
    return s;
}

bool Stack_empty(Stack *s){//isempty
    return s->sp < s->stack;
}

void Stack_push(Stack *s, Type value){
    if(s->sp >= s->stack + s->size -1){
        fprintf(stderr, "stack over flow\n");
        exit(-1);
    }
    *(++s->sp) = value;
}

Type Stack_pop(Stack *s){
    if(Stack_empty(s)){
        fprintf(stderr, "stack is empty\n");
        exit(-2);
    }
    return *s->sp--;
}

void Stack_free(Stack *s){
    if(s!=NULL){
        free(s->stack);
        free(s);
    }
}

typedef struct array {
    Stack *front;
    Stack *back;
    size_t size;
    size_t index;
} Array;

Array* Array_make(size_t size){
    Array *a;
    int i;
    a = (Array*)malloc(sizeof(Array));
    a->front = Stack_make(size);
    a->back  = Stack_make(size);
    a->size = size;
    //initialize
    Stack_push(a->front, DefaultValue);
    for(i=0;i<size;++i){
        Stack_push(a->back, DefaultValue);
    }
    a->index = 0;
    return a;
}

void Array_pos_set(Array *a, size_t index){
    if(index < 0 || index >= a->size){
        fprintf(stderr, "out of range\n");
        exit(-11);
    }
    if(a->index < index){
        while(a->index < index){
            Stack_push(a->front, Stack_pop(a->back));
            ++a->index;
        }
    } else if(a->index > index){
        while(a->index > index){
            Stack_push(a->back, Stack_pop(a->front));
            --a->index;
        }
    }
}

Type Array_set(Array *a, size_t index, Type value){
    Array_pos_set(a, index);//a->index == index
    Stack_pop(a->front);
    Stack_push(a->front, value);

    return value;
}

Type Array_get(Array *a, size_t index){
    Type value;
    Array_pos_set(a, index);//a->index == index
    value = Stack_pop(a->front);
    Stack_push(a->front, value);

    return value;
}

void Array_free(Array *a){
    Stack_free(a->front);
    Stack_free(a->back);
    free(a);
}

int main(){
    Array *a;
    int i;

    a = Array_make(10);
    for(i=0;i<10;i++){
        Array_set(a, i, i+1);//a[i] = i+1;
    }
    for(i=0;i<10;i++){
        printf("%d\n", Array_get(a, i));// a[i];
    }
    Array_free(a);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

#define Type int
#define DefaultValue 0

#define bool int

typedef struct stack{
    Type *stack;
    Type *sp;
    size_t size;
} Stack;

Stack* Stack_make(size_t size){
    Stack *s;
    s = (Stack*)malloc(sizeof(Stack));
    s->stack = (Type*)malloc(sizeof(Type)*size);
    s->sp = s->stack -1;
    s->size = size;
    return s;
}

bool Stack_empty(Stack *s){//isempty
    return s->sp < s->stack;
}

void Stack_push(Stack *s, Type value){
    if(s->sp >= s->stack + s->size -1){
        fprintf(stderr, "stack over flow\n");
        exit(-1);
    }
    *(++s->sp) = value;
}

Type Stack_pop(Stack *s){
    if(Stack_empty(s)){
        fprintf(stderr, "stack is empty\n");
        exit(-2);
    }
    return *s->sp--;
}

void Stack_free(Stack *s){
    if(s!=NULL){
        free(s->stack);
        free(s);
    }
}

typedef struct array {
    Stack *front;
    Stack *back;
    size_t size;
    size_t index;
} Array;

Array* Array_make(size_t size){
    Array *a;
    int i;
    a = (Array*)malloc(sizeof(Array));
    a->front = Stack_make(size);
    a->back  = Stack_make(size);
    a->size = size;
    //initialize
    Stack_push(a->front, DefaultValue);
    for(i=0;i<size;++i){
        Stack_push(a->back, DefaultValue);
    }
    a->index = 0;
    return a;
}

void Array_pos_set(Array *a, size_t index){
    if(index < 0 || index >= a->size){
        fprintf(stderr, "out of range\n");
        exit(-11);
    }
    if(a->index < index){
        while(a->index < index){
            Stack_push(a->front, Stack_pop(a->back));
            ++a->index;
        }
    } else if(a->index > index){
        while(a->index > index){
            Stack_push(a->back, Stack_pop(a->front));
            --a->index;
        }
    }
}

Type Array_set(Array *a, size_t index, Type value){
    Array_pos_set(a, index);//a->index == index
    Stack_pop(a->front);
    Stack_push(a->front, value);

    return value;
}

Type Array_get(Array *a, size_t index){
    Type value;
    Array_pos_set(a, index);//a->index == index
    value = Stack_pop(a->front);
    Stack_push(a->front, value);

    return value;
}

void Array_free(Array *a){
    Stack_free(a->front);
    Stack_free(a->back);
    free(a);
}

int main(){
    Array *a;
    int i;

    a = Array_make(10);
    for(i=0;i<10;i++){
        Array_set(a, i, i+1);//a[i] = i+1;
    }
    for(i=0;i<10;i++){
        printf("%d\n", Array_get(a, i));// a[i];
    }
    Array_free(a);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文