如何在一个数组中高效实现三个栈?
这是一个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()
有没有其他更有效的方法来做到这一点呢?用C/C++这样实现会更高效吗??
4 个回答
0
在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,但其实很多时候,只要我们仔细阅读文档或者参考一些例子,就能找到解决办法。
比如说,如果你在使用一个新的编程库,可能会发现它的使用方式和你之前用过的有些不同。这时候,查看官方文档或者社区的讨论会非常有帮助,因为里面通常会有详细的说明和示例代码,帮助你理解如何正确使用这个库。
另外,很多时候,其他开发者也会在网上分享他们遇到的问题和解决方案。你可以在像StackOverflow这样的网站上搜索相关问题,看看别人是怎么解决的。这不仅能帮你解决当前的问题,还能让你学到更多的知识。
总之,遇到问题时,不要慌张,先查阅资料,看看有没有人遇到过类似的情况,通常你会找到答案的。
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)
1
一个更好的解决办法是用列表来实现链表,而不是用栈。上面的代码是用栈来实现链表的,我觉得在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的指针数组,每个指针都指向链表的头部,你就可以创建三个链表。这种方法能最大限度地利用空间,而且也不需要检查空节点。
6
在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语言风格的链表可能会很有趣,但其实没必要,而且可能会非常慢。直接使用列表就可以了。