3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation...

36
3. Linear Structures 3. Linear Structures Abstract Data Types(ADTs) The List ADT The Stack ADT The Queue ADT Implementation Malek Mouhoub, CS340 Fall 2004 1

Transcript of 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation...

Page 1: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

3. Linear Structures

• Abstract Data Types (ADTs)

• The List ADT

• The Stack ADT

• The Queue ADT

• Implementation

Malek Mouhoub, CS340 Fall 2004 1

Page 2: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

Abstract Data Type (ADT)

• Set of data together with a set of operations.

• Definition of an ADT : [what to do ? ]

– Definition of data and the set of operations (functions).

• Implementation of an ADT : [how to do it ? ]

– How are the objects and operations implemented.

⇒ use the C++ class.

Malek Mouhoub, CS340 Fall 2004 2

Page 3: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

ADT List

Structure List is

Data : A list of elements.

Functions :

for every list ∈ List, item ∈ Element

List createList() ::= return an empty list

List insert(item,i,list) ::= add item at the position i and return the new list

List remove(item,list) ::= remove item and return the new list

Boolean isempty(list) ::= return list == empty

find(item,list) ::= If item ∈ list return position of item

else return ERROR

findKth(i,list) ::= return the item at position i if it exists, ERROR otherwise.

Malek Mouhoub, CS340 Fall 2004 3

Page 4: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

Array Implementation of Lists

• Contiguous allocation of memory to store the elements of the

list.

• Estimation (overestimation) of the maximum size of the list is

required

⇒ waste of memory space.

• O(N) for find , constant time for findKth

• But O(N) is required for insertion and deletion in the worst

case.

⇒ building a list by N successive inserts would require O(N2)in the worst case.

Malek Mouhoub, CS340 Fall 2004 4

Page 5: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

3412521622

n

12345

3412521622

n

12345

find(X): O(n) removeKth: O(n)remove(X):O(n)

3412521612

n

12345

insert(kth,X)O(n)

findKth=List[Kth]O(C)

12521622

n

12345

findKth(3)=52 find(52)=3 remove(34) insert(1,34)

List

Figure 1: Contiguous allocation of memory to store the elements of the list

Malek Mouhoub, CS340 Fall 2004 5

Page 6: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

Linked Lists

• Non contiguous allocation of memory.

• O(N) for find

• O(N) for findKth (but better time in practice if the calls to

findKth are in sorted order by the argument).

• Constant time for insertion and deletion.

Malek Mouhoub, CS340 Fall 2004 6

Page 7: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

3412345 12

52

16

22

678

5

3

7

4

Head Data Link

List = 34 12 52 16 22

34 12 52 16 22

Head

printList():O(n)find(x):O(n)findKth(i):O(i)

\

Figure 2: Non contiguous allocation of memory

Malek Mouhoub, CS340 Fall 2004 7

Page 8: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

A1 A2 A3 A4 A5

(a)

A1 A2 A3 A4 A5

(b)

Figure 3: (a) A double linked list (b) A double circular linked list

Malek Mouhoub, CS340 Fall 2004 8

Page 9: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

header (e) Empty list with header

header

A1 A2 A3 A4 A5

(d) Linked list with a header

34 12 16 22

52

(c) Insertion into a linked list

(b) Deletion from a linked list

34 12 52 16 22

(a) A lilnked list

34 12 52 16 22

remove(52)

insert(3,52)

header

header

header

Figure 4: Linked List Functions

Malek Mouhoub, CS340 Fall 2004 9

Page 10: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

ADT Stack

Structure Stack is

Data : A list of elements.

Functions :

for every stack ∈ Stack, item ∈ Element, capacity ∈ Natural

Stack createStack(capacity)::= return an empty stack

Boolean isFull(stack) ::= return (number of items in stack == capacity)

Boolean isEmpty(stack)::= return (stack == createStack(capacity))

Stack push(item,stack)::= If isFull(stack) return stack

else add item at the top of stack and return the new stack

Stack pop(stack) ::= If isEmpty(stack) return stack

else delete the element at the top of the stack and return the new stack

Element topAndPop(stack)::= If isEmpty(stack) return ERROR

else delete and return the element at the top of the stack

Malek Mouhoub, CS340 Fall 2004 10

Page 11: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

string input = "[()]"

push("[")

Top[

Top

push ("(")

[(

top()="("pop()

[

read "[" read "(" read ")"

Top

top()="["pop()

read "]"

Top

empty()

read ""

correct

Figure 5: Application: Balancing Symbols

Malek Mouhoub, CS340 Fall 2004 11

Page 12: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

intput infix = a+b*c+(d*e+f)*g

output:a push ("+")

+

output:ab

+

read "a" read "+" read "b"

push("*")

read "*"

output:abc

read "c"

*+

pop()output:abc*+

push ("(")

+

output:abc*+d

+

read "+" read "(" read "d"

push("*")

read "*"

output:abc*+de

read "e"

(+

pop()output:abc*+de*push("+")

output:abc*+de*f

+

pop()output:abc*+de*f+

pop()

+

read "+" read "f" read ")"

push("*")

read "*"

output:abc*+def+g

read "g"

*+

+

*

+

( (

*

+

(

*

+(

+(

+

*

+

pop()output:abc*+def+g*pop()output:abc*+def+g*+

Figure 6: Application : Infix and Postfix Conversion

Malek Mouhoub, CS340 Fall 2004 12

Page 13: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

ADT Queue

Structure Queue is

Data : A list of elements.

Functions :

for every queue∈ Queue, item∈ Element, capacity∈ Natural

Queue createQueue(capacity) ::= return an empty queue

Boolean isFull(queue) ::= return (number of items in stack == capacity)

Boolean isEmpty(queue) ::= return (queue == createQueue(capacity))

Queue enqueue(item,queue) ::= If isFull(queue) return queue

else add item at the end of queue

and return the new queue

Queue dequeue(queue) ::= If isEmpty(queue) return queue

else delete the element at the front of the queue

and return the new queue

Element getFront(stack) ::= If isEmpty(stack) return ERROR

else delete and return the element at the front of queue

Malek Mouhoub, CS340 Fall 2004 13

Page 14: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3. Linear Structures

2 41

2 41

front^

front^

front^

back^

back^

3

3 2 41 3

2 4

front^

back^

(1)Initial State

2 41

front^

back^

(2)After enqueue(1)

(3)After enqueue(3) (4)After dequeue, which returns 2

2 41

front^

back^

3

(5)After dequeue, which returns 4 (6)After dequeue, which returns 1

back

(7)After dequeue, which return 3 and Makes the Queue Empty

front^

back^

2 41 3

Figure 7: Array Implementation of Queues

Malek Mouhoub, CS340 Fall 2004 14

Page 15: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

Impl

emen

tatio

n

#ifn

def

Link

edLi

stH

#de

fine

Link

edLi

stH

#in

clud

e"

dsex

cept

ions

.h"

#in

clud

e<

iost

ream

.h>

//F

orN

ULL

tem

plat

e<

clas

sO

bjec

t>

clas

sLi

st;

//In

com

plet

ede

clar

atio

n.

tem

plat

e<

clas

sO

bjec

t>

clas

sLi

stItr

;//

Inco

mpl

ete

decl

arat

ion.

tem

plat

e<

clas

sO

bjec

t>10

clas

sLi

stN

ode

{Li

stN

ode(

cons

tO

bjec

t&

theE

lem

ent

=O

bjec

t(),

List

Nod

e*

n=N

ULL

)

:el

emen

t(th

eEle

men

t),

next

(n

){}

Obj

ect

elem

ent;

List

Nod

e*n

ext;

frie

ndcl

ass

List<

Obj

ect>

;

frie

ndcl

ass

List

Itr<

Obj

ect>

;

};20

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0415

Page 16: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

clas

sLi

st

{pu

blic

:

List

();

List

(co

nst

List

&rh

s);

˜Lis

t();

30

bool

isE

mpt

y()

cons

t;

void

mak

eEm

pty(

);

List

Itr<

Obj

ect>

zero

th(

)co

nst;

List

Itr<

Obj

ect>

first

()

cons

t;

void

inse

rt(

cons

tO

bjec

t&

x,co

nst

List

Itr<

Obj

ect>

&p

);

List

Itr<

Obj

ect>

find(

cons

tO

bjec

t&

x)

cons

t;

List

Itr<

Obj

ect>

findP

revi

ous(

cons

tO

bjec

t&

x)

cons

t;

void

rem

ove(

cons

tO

bjec

t&

x);

cons

tLi

st&

oper

ator

=(

cons

tLi

st&

rhs

);

priv

ate:

40

List

Nod

e<O

bjec

t>*h

eade

r;

};

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0416

Page 17: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

clas

sLi

stItr

{pu

blic

:

List

Itr(

):

curr

ent(

NU

LL){}

bool

isP

astE

nd(

)co

nst

{re

turn

curr

ent

==

NU

LL;}

void

adva

nce(

)

{if

(!is

Pas

tEnd

()

)cu

rren

t=

curr

ent−

>ne

xt;}

cons

tO

bjec

t&

retr

ieve

()

cons

t10

{if

(is

Pas

tEnd

()

)th

row

Bad

Itera

tor(

);

retu

rncu

rren

t−>

elem

ent;}

priv

ate:

List

Nod

e<O

bjec

t>*c

urre

nt;

//C

urre

ntpo

sitio

n

List

Itr(

List

Nod

e<O

bjec

t>*t

heN

ode

)

:cu

rren

t(th

eNod

e){}

frie

ndcl

ass

List<

Obj

ect>

;//

Gra

ntac

cess

toco

nstr

ucto

r

}; #in

clud

e"

Link

edLi

st.c

pp"

#en

dif

20

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0417

Page 18: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#in

clud

e"

Link

edLi

st.h

"

tem

plat

e<

clas

sO

bjec

t>

List<

Obj

ect>

::Lis

t()

{he

ader

=ne

wLi

stN

ode<

Obj

ect>

;

} tem

plat

e<

clas

sO

bjec

t>

List<

Obj

ect>

::Lis

t(co

nst

List<

Obj

ect>

&rh

s)

{he

ader

=ne

wLi

stN

ode<

Obj

ect>

;10

*thi

s=

rhs;

} tem

plat

e<

clas

sO

bjec

t>

List<

Obj

ect>

::˜Li

st(

)

{m

akeE

mpt

y();

dele

tehe

ader

;

} tem

plat

e<

clas

sO

bjec

t>

bool

List<

Obj

ect>

::isE

mpt

y()

cons

t20

{re

turn

head

er−>

next

==

NU

LL;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0418

Page 19: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

void

List<

Obj

ect>

::mak

eEm

pty(

)

{30

whi

le(

!isE

mpt

y()

)

rem

ove(

first

().

retr

ieve

()

);

} tem

plat

e<

clas

sO

bjec

t>

List

Itr<

Obj

ect>

List<

Obj

ect>

::zer

oth(

)co

nst

{re

turn

List

Itr<

Obj

ect>

(he

ader

);

} tem

plat

e<

clas

sO

bjec

t>

List

Itr<

Obj

ect>

List<

Obj

ect>

::firs

t()

cons

t40

{re

turn

List

Itr<

Obj

ect>

(he

ader−>

next

);

} tem

plat

e<

clas

sO

bjec

t>

void

List<

Obj

ect>

::ins

ert(

cons

tO

bjec

t&

x,co

nst

List

Itr<

Obj

ect>

&p

)

{if

(p.

curr

ent

!=N

ULL

)

p.cu

rren

t−>

next

=ne

wLi

stN

ode<

Obj

ect>

(x,

p.cu

rren

t−>

next

);

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0419

Page 20: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

}50

tem

plat

e<

clas

sO

bjec

t>

List

Itr<

Obj

ect>

List<

Obj

ect>

::find

(co

nst

Obj

ect

&x

)co

nst

{ /*1*

/Li

stN

ode<

Obj

ect>

*itr

=he

ader−>

next

;

/*2*

/w

hile

(itr

!=N

ULL

&&

itr−>

elem

ent

!=x

)

/*3*

/itr

=itr−>

next

;

60

/*4*

/re

turn

List

Itr<

Obj

ect>

(itr

);

} tem

plat

e<

clas

sO

bjec

t>

List

Itr<

Obj

ect>

List<

Obj

ect>

::find

Pre

viou

s(co

nst

Obj

ect

&x

)co

nst

{ /*1*

/Li

stN

ode<

Obj

ect>

*itr

=he

ader

;

/*2*

/w

hile

(itr−>

next

!=N

ULL

&&

itr−>

next−>

elem

ent

!=x

)

/*3*

/itr

=itr−>

next

;

70

/*4*

/re

turn

List

Itr<

Obj

ect>

(itr

);

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0420

Page 21: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

void

List<

Obj

ect>

::rem

ove(

cons

tO

bjec

t&

x)

{Li

stItr

<O

bjec

t>p

=fin

dPre

viou

s(x

);80

if(

p.cu

rren

t−>

next

!=N

ULL

)

{Li

stN

ode<

Obj

ect>

*old

Nod

e=

p.cu

rren

t−>

next

;

p.cu

rren

t−>

next

=p.

curr

ent−

>ne

xt−>

next

;//

Byp

ass

dele

ted

node

dele

teol

dNod

e;

}} te

mpl

ate

<cl

ass

Obj

ect>

cons

tLi

st<

Obj

ect>

&Li

st<

Obj

ect>

::ope

rato

r=(

cons

tLi

st<

Obj

ect>

&rh

s)

{90

List

Itr<

Obj

ect>

ritr

=rh

s.fir

st(

);

List

Itr<

Obj

ect>

itr=

zero

th(

);

if(

this

!=&

rhs

)if

{m

akeE

mpt

y();

for(

;!r

itr.is

Pas

tEnd

();

ritr.

adva

nce(

),itr

.adv

ance

()

)

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0421

Page 22: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

inse

rt(

ritr.

retr

ieve

(),

itr);

} retu

rn*t

his

;

}10

0

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0422

Page 23: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

Test

Pro

gram

#in

clud

e<

iost

ream

.h>

#in

clud

e"

Link

edLi

st.h

"

//S

impl

epr

int

met

hod

tem

plat

e<

clas

sO

bjec

t>

void

prin

tLis

t(co

nst

List<

Obj

ect>

&th

eLis

t)

{if

(th

eLis

t.isE

mpt

y()

)

cout

<<

"E

mpt

ylis

t"<

<en

dl;

else

10

{Li

stItr

<O

bjec

t>itr

=th

eLis

t.firs

t();

for(

;!it

r.is

Pas

tEnd

();

itr.a

dvan

ce(

))

cout

<<

itr.r

etrie

ve(

)<

<"

";

} cout

<<

endl

;

}

20

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0423

Page 24: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

int

mai

n()

mai

n{

List<

int>

theL

ist;

List

Itr<

int>

theI

tr=

theL

ist.z

erot

h();

int

i;

prin

tLis

t(th

eLis

t);

for(

i=

0;i<

10;

i++

)

{30

theL

ist.i

nser

t(i,

theI

tr);

prin

tLis

t(th

eLis

t);

theI

tr.a

dvan

ce(

);

} for(

i=

0;i<

10;

i+

=2

)

theL

ist.r

emov

e(i

);

for(

i=

0;i<

10;

i++

)

if(

(i

%2

==

0)

!=(

theL

ist.fi

nd(

i).

isP

astE

nd(

))

)

cout

<<

"F

ind

fails

!"<

<en

dl;

cout

<<

"F

inis

hed

dele

tions

"<

<en

dl;

40

prin

tLis

t(th

eLis

t);

List<

int>

list2

;

list2

=th

eLis

t;

prin

tLis

t(lis

t2);

retu

rn0;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0424

Page 25: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#ifn

def

STA

CK

AR

H

#de

fine

STA

CK

AR

H

#in

clud

e"

vect

or.h

"

#in

clud

e"

dsex

cept

ions

.h"

tem

plat

e<

clas

sO

bjec

t>

clas

sS

tack

{pu

blic

:

expl

icit

Sta

ck(

int

capa

city

=10

);

bool

isE

mpt

y()

cons

t;10

bool

isF

ull(

)co

nst;

cons

tO

bjec

t&

top(

)co

nst;

void

mak

eEm

pty(

);

void

pop(

);

void

push

(co

nst

Obj

ect

&x

);

Obj

ect

topA

ndP

op(

);

priv

ate:

vect

or<

Obj

ect>

theA

rray

;

int

topO

fSta

ck;

20

}; #in

clud

e"

Sta

ckA

r.cpp

"

#en

dif

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0425

Page 26: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#in

clud

e"

Sta

ckA

r.h"

tem

plat

e<

clas

sO

bjec

t>

Sta

ck<

Obj

ect>

::Sta

ck(

int

capa

city

):

theA

rray

(ca

paci

ty)

{to

pOfS

tack

=−

1;

} tem

plat

e<

clas

sO

bjec

t>

bool

Sta

ck<

Obj

ect>

::isE

mpt

y()

cons

t

{re

turn

topO

fSta

ck=

=−

1;10

} tem

plat

e<

clas

sO

bjec

t>

bool

Sta

ck<

Obj

ect>

::isF

ull(

)co

nst

::isF

ull

{re

turn

topO

fSta

ck=

=th

eArr

ay.s

ize(

)−

1;

} tem

plat

e<

clas

sO

bjec

t >

void

Sta

ck<

Obj

ect>

::mak

eEm

pty(

)20

{to

pOfS

tack

=−

1;

} tem

plat

e<

clas

sO

bjec

t>

cons

tO

bjec

t&

Sta

ck<

Obj

ect>

::top

()

cons

t

{if

(is

Em

pty(

))

thro

wU

nder

flow

();

retu

rnth

eArr

ay[

topO

fSta

ck];

}30

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0426

Page 27: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

void

Sta

ck<

Obj

ect>

::pop

()

::pop

{if

(is

Em

pty(

))

thro

wU

nder

flow

();

topO

fSta

ck−−

;}

tem

plat

e<

clas

sO

bjec

t>40

void

Sta

ck<

Obj

ect>

::pus

h(co

nst

Obj

ect

&x

)

{if

(is

Ful

l()

)

thro

wO

verfl

ow(

);

theA

rray

[+

+to

pOfS

tack

]=

x;

} tem

plat

e<

clas

sO

bjec

t>

Obj

ect

Sta

ck<

Obj

ect>

::top

And

Pop

()

{if

(is

Em

pty(

))

50th

row

Und

erflo

w(

);

retu

rnth

eArr

ay[

topO

fSta

ck−−

];

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0427

Page 28: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#ifn

def

STA

CK

LIH

#de

fine

STA

CK

LIH

#in

clud

e"

dsex

cept

ions

.h"

#in

clud

e<

iost

ream

.h>

//F

orN

ULL

tem

plat

e<

clas

sO

bjec

t>

clas

sS

tack

{pu

blic

:

Sta

ck(

);

Sta

ck(

cons

tS

tack

&rh

s);

10˜S

tack

();

bool

isE

mpt

y()

cons

t;is

Em

pty

bool

isF

ull(

)co

nst;

cons

tO

bjec

t&

top(

)co

nst;

void

mak

eEm

pty(

);

void

pop(

);

void

push

(co

nst

Obj

ect

&x

);

Obj

ect

topA

ndP

op(

);

cons

tS

tack

&op

erat

or=

(co

nst

Sta

ck&

rhs

);

priv

ate:

20st

ruct

List

Nod

e

{O

bjec

tel

emen

t;

List

Nod

e*n

ext;

List

Nod

e(co

nst

Obj

ect

&th

eEle

men

t,Li

stN

ode

*n

=N

ULL

)

:el

emen

t(th

eEle

men

t),

next

(n

){}

}; List

Nod

e*t

opO

fSta

ck;

}; #in

clud

e"

Sta

ckLi

.cpp

"30

#en

dif

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0428

Page 29: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#in

clud

e"

Sta

ckLi

.h"

#in

clud

e<

iost

ream

.h>

tem

plat

e<

clas

sO

bjec

t>

Sta

ck<

Obj

ect>

::Sta

ck(

)::S

tack

{to

pOfS

tack

=N

ULL

;

} tem

plat

e<

clas

sO

bjec

t>

Sta

ck<

Obj

ect>

::Sta

ck(

cons

tS

tack

<O

bjec

t>&

rhs

)::S

tack

{10

topO

fSta

ck=

NU

LL;

*thi

s=

rhs;

} tem

plat

e<

clas

sO

bjec

t>

Sta

ck<

Obj

ect>

::˜S

tack

()

::˜S

tack

{m

akeE

mpt

y();

} tem

plat

e<

clas

sO

bjec

t>20

bool

Sta

ck<

Obj

ect>

::isF

ull(

)co

nst

::isF

ull

{re

turn

fals

e;

} tem

plat

e<

clas

sO

bjec

t>

bool

Sta

ck<

Obj

ect>

::isE

mpt

y()

cons

t::i

sEm

pty

{re

turn

topO

fSta

ck=

=N

ULL

;

}30

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0429

Page 30: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

void

Sta

ck<

Obj

ect>

::mak

eEm

pty(

)

{w

hile

(!is

Em

pty(

))

pop(

);

} tem

plat

e<

clas

sO

bjec

t>

cons

tO

bjec

t&

Sta

ck<

Obj

ect>

::top

()

cons

t40

::top

{if

(is

Em

pty(

))

thro

wU

nder

flow

();

retu

rnto

pOfS

tack−

>el

emen

t;

} tem

plat

e<

clas

sO

bjec

t>

void

Sta

ck<

Obj

ect>

::pop

()

::pop

{if

(is

Em

pty(

))

50th

row

Und

erflo

w(

);

List

Nod

e*o

ldTo

p=

topO

fSta

ck;

topO

fSta

ck=

topO

fSta

ck−

>ne

xt;

dele

teol

dTop

;

} tem

plat

e<

clas

sO

bjec

t>

Obj

ect

Sta

ck<

Obj

ect>

::top

And

Pop

()

::top

And

Pop

{60

Obj

ect

topI

tem

=to

p();

pop(

);

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0430

Page 31: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

retu

rnto

pIte

m;

} tem

plat

e<

clas

sO

bjec

t>

void

Sta

ck<

Obj

ect>

::pus

h(co

nst

Obj

ect

&x

)::p

ush

{to

pOfS

tack

=ne

wLi

stN

ode(

x,to

pOfS

tack

);

}70

tem

plat

e<

clas

sO

bjec

t>

cons

tS

tack

<O

bjec

t>&

Sta

ck<

Obj

ect>

::

oper

ator

=(

cons

tS

tack

<O

bjec

t>&

rhs

)

{if

(th

is!=

&rh

s)

if{

mak

eEm

pty(

);

if(

rhs.

isE

mpt

y()

)

retu

rn*t

his

;

80Li

stN

ode

*rpt

r=

rhs.

topO

fSta

ck;

List

Nod

e*p

tr=

new

List

Nod

e(rp

tr−

>el

emen

t);

topO

fSta

ck=

ptr;

for(

rptr

=rp

tr−

>ne

xt;

rptr

!=N

ULL

;rp

tr=

rptr−

>ne

xt)

ptr

=pt

r−>

next

=ne

wLi

stN

ode(

rptr−

>el

emen

t);

} retu

rn*t

his

;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0431

Page 32: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#ifn

def

QU

EU

EA

RH

#de

fine

QU

EU

EA

RH

#in

clud

e"

vect

or.h

"

#in

clud

e"

dsex

cept

ions

.h"

tem

plat

e<

clas

sO

bjec

t>

clas

sQ

ueue

{pu

blic

:

expl

icit

Que

ue(

int

capa

city

=10

);

bool

isE

mpt

y()

cons

t;10

bool

isF

ull(

)co

nst;

cons

tO

bjec

t&

getF

ront

()

cons

t;

void

mak

eEm

pty(

);

Obj

ect

dequ

eue(

);

void

enqu

eue(

cons

tO

bjec

t&

x);

priv

ate:

vect

or<

Obj

ect>

theA

rray

;

int

curr

entS

ize;

int

fron

t;

int

back

;20

void

incr

emen

t(in

t&

x);

}; #in

clud

e"

Que

ueA

r.cpp

"

#en

dif

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0432

Page 33: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#in

clud

e"

Que

ueA

r.h"

tem

plat

e<

clas

sO

bjec

t>

Que

ue<

Obj

ect>

::Que

ue(

int

capa

city

):

theA

rray

(ca

paci

ty)

{m

akeE

mpt

y();

} tem

plat

e<

clas

sO

bjec

t>

bool

Que

ue<

Obj

ect>

::isE

mpt

y()

cons

t

{re

turn

curr

entS

ize

==

0;10

} tem

plat

e<

clas

sO

bjec

t>

bool

Que

ue<

Obj

ect>

::isF

ull(

)co

nst

{re

turn

curr

entS

ize

==

theA

rray

.siz

e();

} tem

plat

e<

clas

sO

bjec

t>

void

Que

ue<

Obj

ect>

::mak

eEm

pty(

)

{cu

rren

tSiz

e=

0;20

fron

t=

0;

back

=−

1;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0433

Page 34: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

cons

tO

bjec

t&

Que

ue<

Obj

ect>

::get

Fron

t()

cons

t

{if

(is

Em

pty(

))

thro

wU

nder

flow

();

retu

rnth

eArr

ay[

fron

t];

30

} tem

plat

e<

clas

sO

bjec

t>

Obj

ect

Que

ue<

Obj

ect>

::deq

ueue

()

{if

(is

Em

pty(

))

thro

wU

nder

flow

();

curr

entS

ize−−

;

Obj

ect

fron

tItem

=th

eArr

ay[

fron

t];

incr

emen

t(fr

ont

);

retu

rnfr

ontIt

em;

40

} tem

plat

e<

clas

sO

bjec

t>

void

Que

ue<

Obj

ect>

::enq

ueue

(co

nst

Obj

ect

&x

)

{if

(is

Ful

l()

)

thro

wO

verfl

ow(

);

incr

emen

t(ba

ck);

theA

rray

[ba

ck]

=x;

curr

entS

ize+

+;}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0434

Page 35: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

tem

plat

e<

clas

sO

bjec

t>

void

Que

ue<

Obj

ect>

::inc

rem

ent(

int

&x

)50

{if

(+

+x

==

theA

rray

.siz

e()

)

x=

0;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0435

Page 36: 3. Linear Structuresmouhoubm/=postscript/=c3620/c36203.pdf3. Linear Structures Array Implementation of Lists • Contiguous allocation of memory to store the elements of the list.

3.Li

near

Str

uctu

res

#in

clud

e<

iost

ream

.h>

#in

clud

e"

Que

ueA

r.h"

int

mai

n()

mai

n

{Q

ueue

<in

t>q;

for(

int

j=

0;j<

5;j+

+)

{fo

r(in

ti

=0;

i<

5;i+

+)

q.en

queu

e(i

);

10

whi

le(

!q.is

Em

pty(

))

cout

<<

q.de

queu

e()

<<

endl

;

} retu

rn0;

}

Mal

ekM

ouho

ub,C

S34

0Fa

ll20

0436