【问题标题】:how to implement 3 stack in a single array efficiently?如何有效地在单个数组中实现 3 个堆栈?
【发布时间】:2011-06-18 16:28:31
【问题描述】:

是python代码..是否使用链表实现....这样效率高............

data = []            # data storage for stacks represented as linked lists
stack = [-1, -1, -1] # pointers to each of three stacks (-1 is the "null" pointer)
free = -1            # pointer to list of free stack nodes to be reused

def allocate(value):
    ''' allocate a new node and return a pointer to it '''
    global free
    global data
    if free == -1:
        # free list is empty, need to expand data list
        data += [value,-1]
        return len(data)-2
    else:
        # pop a node off the free list and reuse it
        temp = free
        free = data[temp+1]
        data[temp] = value
        data[temp+1] = -1
        return temp

def release(ptr):
    ''' put node on the free list '''
    global free
    temp = free
    free = ptr
    data[free+1] = temp

def push(n, value):
    ''' push value onto stack n '''
    global free
    global data
    temp = stack[n]
    stack[n] = allocate(value)
    data[stack[n]+1] = temp

def pop(n):
    ''' pop a value off of stack n '''
    value = data[stack[n]]
    temp = stack[n]
    stack[n] = data[stack[n]+1]
    release(temp)
    return value

def list(ptr):
    ''' list contents of a stack '''
    while ptr != -1:
        print data[ptr],
        ptr = data[ptr+1]
    print

def list_all():
    ''' list contents of all the stacks and the free list '''
    print stack,free,data
    for i in range(3):
        print i,":",
        list(stack[i])
    print "free:",
    list(free)

push(0,"hello")
push(1,"foo")
push(0,"goodbye")
push(1,"bar")
list_all()
pop(0)
pop(0)
push(2,"abc")
list_all()
pop(1)
pop(2)
pop(1)
list_all()

r 除了这个之外还有什么方法可以有效地做到这一点??以这种方式在 c /c++ 中实现会很有效???

【问题讨论】:

  • 天啊,这些年来我都不知道 C 和 C++ 是什么!!!
  • 问题被标记为 C 和 C++,但代码看起来像 Python(当然,它看起来像一些 C/C++ 人会编写的 Python 代码,但仍然如此)。
  • @Armen Tsirunyan 它不是 c/c++ 代码。这是一个python代码......
  • 那你为什么把它标记为C和C++?
  • @learn 哦,真的吗?那是一种解脱。有那么一瞬间,我以为是“C/C++”代码……我可以谦虚地问一下,为什么你的问题被标记为 C 和 C++ 吗? ;)

标签: python


【解决方案1】:

在python中,列表就是一个栈:

>>> l = [1, 2, 3, 4, 5]
>>> l.pop()
5
>>> l.pop()
4
>>> l.append(9)
>>> l
[1, 2, 3, 9]
>>> l.pop()
9
>>> l.pop()
3
>>> l.append(12)
>>> l
[1, 2, 12]

虽然在 python 中实现一个 c 风格的链表可能是一个有趣的练习,但它是不必要的,而且可能非常慢。只需使用列表即可。

【讨论】:

  • 是的。我要写c代码。是否可以在不使用链表的情况下在单个数组中实现 3 个堆栈??
  • 任何其他方式在单个数组中实现 3 个堆栈,而在 c 中没有链表??
  • @learn,如果你想用 c 实现一些东西,我建议发布 c 代码而不是 python 代码。
【解决方案2】:

更好的解决方案是使用列表而不是堆栈来实现链表。给出的代码是链表的堆栈实现,我认为这在 python 中是一种规范,但在 C/C++ 中,您可以使用列表来高效实现。

C 中的示例代码如下:-

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *next;
};

struct node* add(struct node *head, int data){
    struct node *tmp;

    if(head == NULL){
        head=(struct node *)malloc(sizeof(struct node));
        if(head == NULL){
            printf("Error! memory is not available\n");
            exit(0);
        }
        head-> data = data;
        head-> next = head;
    }else{
        tmp = head;

        while (tmp-> next != head)
            tmp = tmp-> next;
        tmp-> next = (struct node *)malloc(sizeof(struct node));
        if(tmp -> next == NULL)
        {
            printf("Error! memory is not available\n");
            exit(0);
        }
        tmp = tmp-> next;
        tmp-> data = data;
        tmp-> next = head;
    }
    return head;
}

void printlist(struct node *head)
{
    struct node *current;
    current = head;
    if(current!= NULL)
    {
        do
        {
            printf("%d\t",current->data);
            current = current->next;
        } while (current!= head);
        printf("\n");
    }
    else
        printf("The list is empty\n");

}

void destroy(struct node *head)
{
    struct node *current, *tmp;

    current = head->next;
    head->next = NULL;
    while(current != NULL) {
        tmp = current->next;
        free(current);
        current = tmp;
    }
}
void main()
{
    struct node *head = NULL;
    head = add(head,1); /* 1 */
    printlist(head);

    head = add(head,20);/* 20 */
    printlist(head);

    head = add(head,10);/* 1 20 10 */
    printlist(head);

    head = add(head,5); /* 1 20 10 5*/
    printlist(head);

    destroy(head);
    getchar();
}

在上面的例子中,如果你创建一个大小为3的指针数组,每个指针指向head,你可以创建三个链表。这将以最大的效率处理空间,也无需检查空闲节点。

【讨论】:

  • @satck 程序员有没有办法在没有链表的数组中实现 3 个堆栈???
  • 创建一个大小为 3 的指针数组,其中数组的每个指针都指向一个栈顶
  • @stack 程序员的三个指针将指向右边的 3 个堆栈的顶部.. 如何有效地处理空间?如何查找数组 r 数组中是否有可用空间已满???如何存储3个数组???
  • 希望更改后的帖子能回答您的问题
  • @stack 程序员你所做的是正确的。如果我实现三个指针并指向栈顶..栈顶是什么意思?考虑一个大小为100的a [100] ..数组。第一个指针指向a [0]然后第二个和第三个指针指向什么也???
【解决方案3】:
def finding_element(a,k):
    print a
    i = 0
    while k < a[i]:
        i = i-1
        print k,a[i]
        if k > a[i]:
            i = i+1
            print k,a[i]
            if k == a[i]:
                print k,a[i]
    else:
        print "not found"

a = [ 1,3,5,7,8,9]
k = 5
finding_element(a,k)

【讨论】:

  • 这个程序没有迭代。我想知道这有什么问题。谢谢
【解决方案4】:

当 Python 开箱即用地完成所有这些工作时,您真的不必费心费力。如果你有一些复杂的对象要操作,当然你可以将它包装在函数中,但不要想太多,让 Python 担心内存分配(现在没有人手动这样做了)。

下面是您在非常基本的 Python 中的所有函数调用的等价物:

stacks = [ [] for _ in range(3) ]

stacks[0].append("hello")   # push(0,"hello")
stacks[1].append("foo")     # push(1,"foo")
stacks[0].append("goodbye") # push(0,"goodbye")
stacks[1].append("bar")     # push(1,"bar")
print(stacks)               # list_all()
stacks[0].pop()             # pop(0)
stacks[0].pop()             # pop(0)
stacks[2].append("abc")     # push(2,"abc")
print(stacks)               # list_all()
stacks[1].pop()             # pop(1)
stacks[2].pop()             # pop(2)
stacks[1].pop()             # pop(1)
print(stacks)               # list_all()

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2019-02-22
    • 2012-01-01
    • 2022-12-20
    • 1970-01-01
    • 2016-12-16
    • 2021-09-09
    • 2010-12-15
    • 2014-04-20
    相关资源
    最近更新 更多