Part Ⅲ Elementary Data Structure

Stacks

last-in, first-out (LIFO)

insertion operation: Push
deletion operation:Pop

If an empty stack is popped, then stack underflows
If top[S ] exceeds n, then stack overflows.

Pseudo code of stack empty

 STACK -EMPTY
if  top[S]=0
 then  return  ture
else  return  false
 PUSH(S, x)
  top[S]←top [S ]+1
  S[top[S]]←x
Pop(S)
if STACK-EMPTY(S)
   then error "underflows"
else top[S] ←top[S]-1
   return S[top[S]+1]

Each of the three stack operations takes Ο(1) tim.

Queue
First in, first out( FIFO)

Enqueue (Q, x )

Q[tail[Q]]←x
if tail[Q]=length[Q]
   then tail[Q]←1
  else tail[Q]←tail[Q]+1
Dequeue(Q)

x←Q[head[Q]]
if head[Q]=length [Q]
   then head[Q]=1
else head[Q]←head[Q]+1
   return x

Inserting into a linked list

LIST -Insert (L, x)
next [x ]←head [L]
if head [L]≠NIL 
then prev [head [L ]]←x 
head [L]←x 
prev [x]←NIL
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,790评论 0 33
  • 今晚知道这个消息觉得又难过又开心,希望我care的人可以身体健康!你的病快D好起来,加油,不要放弃!
    逍逍逍遥阅读 146评论 0 0
  • 有人肯定会呵呵哒,这一些小偏方怎么可以去掉可恶的斑点,这肯定是逗着玩的吧。 很多姐妹也许不会相信!但是也有很多斑点...
    中岛暑缎大岛阅读 262评论 1 0
  • 2017年,恢复高考四十年,刚好是我参加高考十周年。十年前,我觉得我已经很努力的准备高考了,可如果时光倒流...
    lan505阅读 366评论 0 2