堆栈的链表实现 [英] Linked List implementation for a stack
本文介绍了堆栈的链表实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我对链表的实现
STACK using linked list
STACK-EMPTY:
if L.head == NIL
return True
else return False
PUSH(x):
x.next = L.head
if L.head != NIL
L.head.prev = x
L.head = x
x.prev = NIL
POP():
x = L.head
L.head = x.next
x.next.prev = L.head
return x
您会验证吗?如何改善?
would you validate this? how to improve ?
谢谢
推荐答案
您可以改善一致性数据结构:
You can improvement the consistency of your data structure:
- 列表头的
上一个
始终为NIL
- 不在列表中的元素具有
next
和上一个
设置为NIL
- The
prev
of the list head is alwaysNIL
- An element which is not in the list has
next
andprev
set to NIL
采取1.帐户中的POP不一致,可能是错误源:弹出元素时,头部的 prev
就是头部本身,当您按下元素时, prev $ c $头的c>是NIL。
Taking 1. in account your POP has an inconsistency which can be source of errors: When you pop an element the prev
of the head is the head itself, when you push an element the prev
of the head is NIL.
这篇关于堆栈的链表实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文