堆栈的链表实现 [英] Linked List implementation for a stack

查看:85
本文介绍了堆栈的链表实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对链表的实现

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:


  1. 列表头的上一个始终为 NIL

  2. 不在列表中的元素具有 next 上一个设置为NIL

  1. The prev of the list head is always NIL
  2. An element which is not in the list has next and prev set to NIL

采取1.帐户中的POP不一致,可能是错误源:弹出元素时,头部的 prev 就是头部本身,当您按下元素时, prev 是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆