0% found this document useful (0 votes)
23 views28 pages

10 - Elementary Data Structures

The document discusses different data structures including stacks, queues, linked lists, and trees. It describes implementations of stacks and queues using arrays, and linked lists using pointers. Linked lists operations like search, insert, and delete are presented, along with using sentinels and handling memory allocation and freeing of nodes. Binary and general trees are also introduced.

Uploaded by

DD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

10 - Elementary Data Structures

The document discusses different data structures including stacks, queues, linked lists, and trees. It describes implementations of stacks and queues using arrays, and linked lists using pointers. Linked lists operations like search, insert, and delete are presented, along with using sentinels and handling memory allocation and freeing of nodes. Binary and general trees are also introduced.

Uploaded by

DD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

10.

Elementary data structures

Hsu, Lih-Hsing
Computer Theory Lab.

10.1 Stacks and queues


Stacks and queues are dynamic set in which elem
ent removed from the set by the DELETE operatio
n is prespecified. In a stack the element deleted f
rom the set is the one most recently inserted; the
stack implements a last-in, first-out, or LIFO, p
olicy. Similarly, in a queue, the element deleted i
s implements a first-in, first-out, or FIFO, polic
y.

Chapter 11 P.2
Computer Theory Lab.

An array implementation of a stack S

Chapter 11 P.3
Computer Theory Lab.

 empty, underflows, overflows

STACK_EMPTY(S )
1 if top[S] = 0
2 then return TRUE
3 else return FALSE

Chapter 11 P.4
Computer Theory Lab.

PUSH(S,x)
1 top[S]top[S]+1
2 S[top[S]] x

POP(S )
1 if STACK-EMPTY(S )
2 then error “underflow”
3 else top[S]  top[s] -1
4 return S[top[S] + 1]
Chapter 11 P.5
Computer Theory Lab.

An array implementation of a queue Q

Chapter 11 P.6
Computer Theory Lab.

ENQUEUE(Q,S )
1 Q[tail[Q]]  x
2 if tail[Q]= length[Q]
3 then tail[Q]  1
4 else tail[Q] tail[Q]+
1

Chapter 11 P.7
Computer Theory Lab.

DEQUEUE(Q )
1 x  Q[head[Q]]
2 if head[Q] = length[Q]
3 then head[Q]  1
4 else head[Q]  head[Q] +1
5 return x

Chapter 11 P.8
Computer Theory Lab.

10.2 Linked lists

Chapter 11 P.9
Computer Theory Lab.

LIST_SEARCH(L,k)
1 x ≠ head[L]
2 while x ≠ NIL and key[x] ≠ k
3 do x ≠ next[x]
4 return x

O(n)

Chapter 11 P.10
Computer Theory Lab.

LIST_INSERT(L,x)

1 next[x]  head[L]
2 if head[L] ≠ NIL
3 then prev[head[L]]  x
4 head[L]  x
5 prev[x]  NIL
O(1)

Chapter 11 P.11
Computer Theory Lab.

LIST_DELETE(L,x)
 (Call LIST_SEARCH first O(n))
1 if prev[x] ≠ NIL
2 then next[prev[x]]  next[x]
3 else head[L]  next[x]
4 if next[x] ≠ NIL
5 then prev[next[x]  prev[x]
O(1) or O(n)
Chapter 11 P.12
Computer Theory Lab.

A Sentinel is a dummy object that allows


us to simplify boundary conditions,

Chapter 11 P.13
Computer Theory Lab.

LIST_DELETE’(L,x)

1 next[prev[x]]  next[x]
2 prev[next[x]]  prev[x]

Chapter 11 P.14
Computer Theory Lab.

LIST_SEARCH’(L,k)

1 x  next[nil[L]]
2 while x ≠ nil[L] and key[x] ≠ k
3 do x  next[x]
4 return x

Chapter 11 P.15
Computer Theory Lab.

LIST_INSERT’(L,x)

1 next[x]  next[nil[L]]
2 prev[next[nil[L]]]  x
3 next[nil[L]]  x
4 prev[x]  nil[L]

Chapter 11 P.16
Computer Theory Lab.

11.3 Implementing pointers and


objects
 A multiple-array representation of objects

Chapter 11 P.17
Computer Theory Lab.

A single array representation of objects

Chapter 11 P.18
Computer Theory Lab.

Allocating and freeing objects--garbage collector

Chapter 11 P.19
Computer Theory Lab.

Allocate_object( ),LIST_INSERT(L,4),Key(4)=25

Chapter 11 P.20
Computer Theory Lab.

LIST_DELETE(L,5), FREE_OBJECT(5)

Chapter 11 P.21
Computer Theory Lab.

ALLOCATE_OBJECT( )

1 if free = NIL
2 then error “out of space”
3 else x  free
4 free  next[x]
5 return x

Chapter 11 P.22
Computer Theory Lab.

FREE_OBJECT(x )

1 next[x]  free
2 free  x

Chapter 11 P.23
Computer Theory Lab.

Two link lists

Chapter 11 P.24
Computer Theory Lab.

10.4 Representing rooted trees

Chapter 11 P.25
Computer Theory Lab.

Binary trees

Chapter 11 P.26
Computer Theory Lab.

Rooted tree with unbounded branching

Chapter 11 P.27
Computer Theory Lab.

 Other tree representation

Chapter 11 P.28

You might also like