Data structures 各种数据结构的最坏情况
我正在为不同数据结构上的操作效率编制一份清单。 到目前为止,我得到了这个:Data structures 各种数据结构的最坏情况,data-structures,Data Structures,我正在为不同数据结构上的操作效率编制一份清单。 到目前为止,我得到了这个: 我不太确定这里作为链表的队列和作为链表的堆栈得到了什么。有人能深入了解这个问题吗?唯一的错误是的Pop对于队列,指针指向前面的链表应该是O(1) 为了完整性,列出诸如搜索任何元素、删除任何元素、按索引获取、按索引删除等操作的复杂性可能是值得的 以链表形式排队(指针指向前面): Push:您需要在末尾插入一个元素,但您只有一个指向开头的指针,因此需要遍历所有元素才能到达末尾 current = head while (c
我不太确定这里作为链表的队列和作为链表的堆栈得到了什么。有人能深入了解这个问题吗?唯一的错误是
的Pop
对于队列,指针指向前面的链表应该是O(1)
为了完整性,列出诸如搜索任何元素、删除任何元素、按索引获取、按索引删除等操作的复杂性可能是值得的
以链表形式排队(指针指向前面):
Push:您需要在末尾插入一个元素,但您只有一个指向开头的指针,因此需要遍历所有元素才能到达末尾
current = head
while (current.next != null)
current = current.next
current.next = newItem
Pop:您需要删除开头的元素,因此只需将指向开头的指针重新指定给第二个元素即可
removedItem = head
head = head.next
以链表形式排队(指针指向前面和后面):
Push:您需要在末尾插入一个元素,并且有一个指向末尾的指针,这样您就可以在固定时间内添加它
tail.next = newItem
tail = newItem
newItem.next = head
head = newItem
removedItem = head
head = head.next
last = last+1
array[last] = newItem
removedItem = array[last]
last = last-1
Pop:与单链表的Pop相同
removedItem = head
head = head.next
堆叠为链表:
推送:在开头插入一个项目,在固定时间内很容易完成
tail.next = newItem
tail = newItem
newItem.next = head
head = newItem
removedItem = head
head = head.next
last = last+1
array[last] = newItem
removedItem = array[last]
last = last-1
Pop:移除第一项,在固定时间内轻松完成
tail.next = newItem
tail = newItem
newItem.next = head
head = newItem
removedItem = head
head = head.next
last = last+1
array[last] = newItem
removedItem = array[last]
last = last-1
堆栈为数组:
推送:在最后一个索引处插入项目,易于在固定时间内完成
tail.next = newItem
tail = newItem
newItem.next = head
head = newItem
removedItem = head
head = head.next
last = last+1
array[last] = newItem
removedItem = array[last]
last = last-1
Pop:在最后一个索引中删除项目,在固定时间内轻松完成
tail.next = newItem
tail = newItem
newItem.next = head
head = newItem
removedItem = head
head = head.next
last = last+1
array[last] = newItem
removedItem = array[last]
last = last-1