Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C...

47
Queues, Stack Modules, and Abstract Data Types CS2023 Winter 2004

Transcript of Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C...

Page 1: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Queues, Stack Modules, and Abstract Data Types

CS2023 Winter 2004

Page 2: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Outcomes: Queues, Stack Modules, and Abstract Data Types

� C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter 19

� After the conclusion of this section you should be able to

� Write queue and stack modules using a linked list

� Use � � � � ��� to modify the linkage of functions and global variables

� Use opaque data types to create modules that implement abstract data types

Page 3: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Queue

Queue can be implemented as a linked list:

� � �� � �� ��� �� � � � � ��� � � �

� � �� � ��� � � � �� �� �

� � � � � �

� � � � � � �� �� � �� � � �

� �� �� � �

�� �� � � � � � � �

�� �� � � � � � � �

� � � � � � � � � � �! � "# �

� � � � $ �% & & �

� � � � $ �% & & �

Page 4: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

')( *+ ,- ./ , / , 0 + 1 2 1 + 3 4

5 ( + , 6 7 - ,8 5 ( + ,9

- ,8 5 ( + , : ; 1 < < ( = 0�> *!? , ( @ 0 5 ( + , 6 3 3 9

* @ 0 - ,8 5)( + , : : 5A B B 3 4

@!C D *- 2 @ 0�> 2 + , D DFE G ./ , / , *> @/ < <H - G 3 9

,I * 2 0J KL 6M

NO L B A PJ 3 9

Q- ,8 5 ( + ,SR T + : + 9

- ,8 5 ( + ,SR T - ,I 2 : 5A B B9

* @ 0U , ; C 2WV 0 3 3 4

D , 1 DR T - ,I 2 : - ,8 5 ( + ,9

D , 1 D : - ,8 5)( + ,9

Q , <> ,

@ D ( - 2 : D , 1 D : - ,8 5 ( + ,9

Q

Page 5: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

X�Y Z Y X�[ \] [ ] [ ^ X Y Z Y X_ `

X Y Z Y X!a

b)c X�[ d e c f X bc X[ a

X g h!i c j Zlk m X a

c f X bc X[ g h!i c j Z a

h i c j Z g h!i c j Zk m j[ n Z a

h i [ [ ^ c f X bc X�[ _ a

i [ Z ] i j X!a

oX�Y Z Y p[ Zrqhi c j Z ^ _ `

i [ Z ] i j hi c j Zlk m X!a

os j Z s�t q [ uv ZWw ^ _ `

i [ Z ] i j hi c j Z g g bx y y a

o

Page 6: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Module

z A well-designed module often keeps information secret from its clients

{ information hiding

z Clients of a stack module have no need to know whether it is stored in an array, in a linked list, or in some other way

z In C, the major tool for enforcing information hiding is the | } ~ } ��� storage class

Page 7: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Storage Duration, Scope & Linkage

� Every variable in a C program has three properties:

1. Storage duration

� automatic

� static2. Scope

� block scope

� file scope

3. Linkage

Page 8: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Linkage

� Linkage of a variable determines extent to which it can be shared by different files in a program.

� Variables with external linkage may be shared by several files in a program

� Variables with internal linkage are restricted to a single file, but may be shared by the functions in that file

� Variables with no linkage belong to a single function and can't be shared at all

Page 9: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Storage Duration, Scope & Linkage

��� � ���

� � �� � ��

�� � �

� � �� � � � � � � �

���� � � �

static storage durationfile scopeexternal linkage

automatic storage durationblock scopeno linkage

}

Page 10: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Static keyword

� ��� � ��� ��� � ���

� ��� � ��� � � ��   ¡¢

£¤ ¤ ¤

¥

� � �� ¦ ¡ � � �� ¢

£� ��� � ��� ��� � § �

¥

static storage durationfile scopeinternal linkage

block scopeno linkage

}

static storage duration

Page 11: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Extern keyword

¨© ª ¨« ¬ ­ ¬ ª ­�®

¯ ° ­± ²³ ¯ ° ­ ± ´

µ¨© ª ¨« ¬ ­ ¬ ª ¶ ®

·

static storage durationfile scopeexternal linkage

static storage durationblock scopeexternal linkage

Page 12: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

What Gets Printed?

¸�¹ º »¼

½ ¾ ¸¿ À Á ½ ¾ ¸¿  ¼

½ ¾ ¸¿ à Á ¸�¹ º Ä Â

ÅÄ Æ Ç ¼

» È Æ Ä¼

ɸ�¹ º ÊË ¸ ¹ Á Â

Å» Æ Ì¼

à Á »  ¼

À Á  ¼ÍÎ ¸ ¹ º à ÁÏ » Æ Ð¿ ѹ Ï Ò »  ¼

Î Ó ºWÔ Î ¹ Õ¼

É

Ó Ä º ÓÎ ¹ ¸ ¹ º »¼

½ ¾ ¸ ¿ À Á ½ ¾ ¸¿ Â

Å» Ö Æ Ç ¼

É

Page 13: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

What Gets Printed Now?

× Ø�Ù Ø Ú�Û Ú�Ü Ø ÝÞ

ß à Úá â ã ß à Úá ä Þ

ß à Úá å ã Ú�Ü Ø æ ä

çæ è é Þ

Ý ê è æÞ

ëÚ�Ü Ø ìÙ Ú Ü ã ä

çÝ è íÞ

å ã Ý ä Þ

â ã ä Þîï Ú Ü Ø å ãð Ý è ñá òÜ ð ó Ý ä Þ

ï ô ØWõ ï Ü öÞ

ë

ô æ Ø ôï Ü Ú Ü Ø ÝÞ

ß à Ú á â ã ß à Úá ä

çÝ ÷ è é Þ

ë

Page 14: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Module

ø ù�ú û üý

þ

ÿ � ��� ��� � �� ��

ÿ �� � � � � �� ��

� � � � �ú ü� �� �� ù�� � � � � �� �

� � ù � ø �� �� ù�� � � � � �� �

� � � � � � ø þ � � � ù � � �

� � ù � � � � � � � �� �

ÿ� � � � �

Page 15: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack as array

� ��� � ��� �! "�# $&% � ')( * "

� �! + ��� ! ,- . /0 1,2 34 5 6 6

# $&% $ � � �� $ �7 � $ ! � $# 8 ,- . /0 1, 2 3 4 9�:

# $&% $ � � �� $ $ 7 ; < 6:

=7 � >% '! 1! > ; $@? A =7 � B C

$ 7 ; < 6:

D�� $ �# 1! > ; $@? A =7 � B C

E ! $ � E� $ 7 ; < < 6:

D

Page 16: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

F G&H G I�J I�K G I FMLNPO Q QR S T IU VW

XY G O X K G TZ [ [ \] ^ _` L\a bc d

eS T I U Z O F fR I K G I V W

I N R I FLNPO Q QR V V W

NZ X I�K G NR F G U Y X Xhg i Z O F f�j F G H J k I F N O Q Ql K i V d

Y m I G R c na ] L o ^a pq rc V d

eJ T K G Y K G F s G TZ t t u [ I d

eI�K G Z TZ R S T I U V W

I NR I FML Y vZ G@w R V V W

NZ X I�K G NR F G U Y X Xhg i Z T Z j F G&H J k I F Y vZ G@w l K i V d

Y m I G R c na ] L o ^a pq rc V d

eXY G O X K J T K G Y K G F syx x G TZ u d

e

Page 17: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack as Linked List

z {�| } ~�� ��� ��� �&� } �)� � �

�@� � � ��� � � �&� � } � | � ��� �

{ | � �� � � �

� � � � } � | � ��� � | � � � �

� � � ��� � �

� �&� � { } � � �� � � � � � � �� � � �

Page 18: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

� � �� �� ���  � �¡ ¢@£ ¤ � � �� ¥

¦§ �� � ¨ ©«ª � ¬ ¢@­

® ¯ �° � ¤ ¢ � ¡ ±³² §´ µ µ ¥ ¦

ª � ¬ ¢ ² ¢ � ¡ ¶ ·ª � ¬ ¢@­

¸�¹ � � ¤ ¢ � ¡ ¥ ­

¢ � ¡ ² ª � ¬ ¢@­

ºº

�ª ¢ ��»  � � ¡ ¢@£ ¤ � � � � ¥ ¦

¹ � ¢@¼ ¹ ª ¢ � ¡ ² ² §´ µ µ­

º

Page 19: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

½ ¾ ¿À Á à ÄÅ ¿�Æ Ç ¿È É

Ê ¾À Ë Ì Í Æ ËÎ Ê ¾À ËÏ

Æ ËÎ Ê ¾À Ë Ð ÑÒ Ó Ó ¾Ô Å Ã ¿�Õ Ë ¾ ÖÅ Ê ¾À Ë ÌÈ È Ï

¿ Ö Å Æ ËÎ Ê ¾À Ë Ð Ð Ê× Ø ØÈ É

Ö ÁÙ ¿Æ Ç ÖÅ Ã Ç À Ë Ù Ù Ú Û Á à Ä�Ü Ã Ç Ò Ô Ý ¿ à ÖÂ Ó ÓÞ Æ Û È Ï

Ëß ¿ Ç Å àá â Ìã äå â Ø × æ à È Ï

çÆ ËÎ Ê ¾À Ëéè ê À Ò Ç Ò Ð ¿Ï

Æ ËÎ Ê ¾À Ëéè ê Æ Ëß Ç Ð Ç ¾ ÁÏ

Ç ¾ Á Ð Æ ËÎ Ê ¾À ËÏ

ç

Page 20: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

ë�ì í îï î ð�ñ ï ëò ó ô

õï ò ö ÷ ø ï ùò ÷ï îú

ë ì í ë�ú

ë û ð ë�ü ý öþ î í@ÿ ð ó ó ô

û î� ë�ì í û ð ü í ò ö � � � � îï î� ü í�� � � ë�ü öþ î í@ÿ �ì � ó ú

ö ë í ð �� ÷ ý � � �� � ó ú

�ï ùò ÷ï î � í ï î ú

ë � í ï î�� � ò � í�� ú

í ï î � í ï î� � ì ö í ú

û� ö ö ð ï ù ò ÷ï î ó ú

� ö í�� � ì ëú

Page 21: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Data Type

� Can only have one instance of preceding stack modules

� Need to create a stack type:

� ��� � "! #�$ %�& '�( � )+* , %

� � ' -( � � ./

01 '( � )2 & 354 & 687

� $ 9;: & '�( � ) .< & 3/ 7

� $ 9;: & '�( � ) .< & 6/ 7

= ! & , .< & 3 4 3/ 7

� > .? �& : $ - = '�@ . < & 3/ /

=A � � ' > . %B #C � % 4 =D = . < & 3/ / 7 EF =A � � '& % 3 % F E

* * *G

Page 22: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Data Type

H I�J K L+M N

I�O PQ RQ S H I�T U K I VW RQ X

Y V I RJ I J Z

H IT U K I VW RQ [ VQ \ I Z

] ^W RQ _ Z

I�O PQ RQ S H I�T U K I X

^W RQ _ [ I W P Z

] ` I�J K L _

aW Y R VQ b;c H I�J K Ld ` I J K L _ [ H e Z

aW Y R fJ LQ c Q f P I�O d ` I�J K L _ [ H e Z

Y V I Y H c Q f P IO d KW V H I ` I J K L _ [ H e Z

aW Y R P U H N d ` I J K L _ [ Hg Y V I Y e Z

Y V I P W P d ` I J K L _ [ H e Z

Page 23: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Type as Linked List

h i�j k l"m n�o p�q r�s k t+u v p

w x i n j o y;z q r�s k t{ | rs k t} ~q ��

q � � r x� � �� � ���

�w x i n �s to z o �� r�� { | r�s k t} ~q ��

� x n�o } ~ j o � r �

y v i l o { q � � r x� � � �� � � � �

j o � r � q � � r x � � � j o � r �

�"� o o { q � � r x� � �

q � � r x � � j o � r �

��

Page 24: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

� � �� �� � �� �� �� �� � �  ��¡ � �¢ £

¤ �� ¥ � � ¡ ¥¦ ¤ �� ¥§

¡ ¥¦ ¤ �� ¥ ¨ © � ª ª � � � � �"« ¥ � ¬� ¤ �� ¥ � ¢ ¢ §

� ¬ � ¡ ¥¦ ¤ �� ¥ ¨ ¨ ¤­ ® ®¢ £

¬ �¯ ��¡ � ¬� � � � ¥ ¯ ¯   ° �� � �"± � � �� � � � ¬� ª ª² ¡ ° ¢ §

¥³ �� � ´µ ¶ �·

¸¹ ¶ ® ­ º ´¢ §

»¡ ¥¦ ¤ �� ¥�¼ ½ � �� � ¨ �§

¡ ¥¦ ¤ �� ¥�¼ ½ ¡ ¥³ � ¨ � ¼ ½� � �§

� ¼ ½� � � ¨ ¡ ¥¦ ¤ � � ¥§

»

Page 25: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

¾�¿ À ¾�Á  ÃÄ Å À�Æ Ç�È É ¿ Á À Ê À�Ë È ÌÍ Î Á Ï Ð

Ñ Ã À�Ò Ñ ¿ Á Ó Ô À É Å Õ Õ Ö× Ø Ø�Ù

Ú¾�¿ À Å É Å Ç Ê ÀË È Ì Í Î Á Ï Ð

Ö É Û Ã Í Î É Ü Û Í É ÅÙ

¾ ¿ À ¾Ù

¾ Ý Ç ¾�Á Â Ã Ä Å À�Æ Ç Ï Ï Ð

Ý Å Ñ ¾�¿ À Ý Ç Á À Û Ã Ñ ÑßÞ à Å É Åá Á À�Ë È Ì ¾�Á Ã Ä Å À�Æ â¿ à Ï Ù

Ãã ¾ À Çä åæ ÍÂ

çè æ Ø × éä Ï Ù

ÚÉ Ü Û Í É Å Õ Á Ó Ô À É ÅÙ

¾ Õ Á Ó Ô À É Å Ó Ô ÛË ÀË Ù

Á Ó Ô À É Å Õ Á Ó Ô À É Å Ó Ô ¿ Ãã À Ù

Ý Ñ Ã Ã Ç É Ü ÛÍ É Å Ï Ù

Ñ Ã À�Ò Ñ ¿ ¾Ù

Ú

Page 26: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack Type

ê The previous module allowed for multiple instances, but at the expense of information hiding!

ë Nothing prevents a client from using aìîí ïð ñò

variable as a structure:

óô õö ÷ø ù úüû

ù ú5ý ô þÿ � �� � �û

ý ý ý

Page 27: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Opaque Data Type

� Incomplete structure definition:

� can define the type of a structure that hasn't been defined yet:

��� �� � ��� � � � �� � �� � �� � � � �� �

� allows one to use the type � ��� �� �! as a synonym for

" #�$ % & # '( ) )�* + ,

.

Page 28: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Opaque Data Type

- A data type is opaque because the client cannot access its full representation

. all the client knows is that it is represented by another data type

/ client doesn't know that that data type is

0 Consider module

132 4

that exports a data type called 5 687 9;: <= 9

to the client

> ?@ ACB D

defines a type

E FHG IKJ LM I

as a pointer to a structure type called

NPO QR ST U T

U;V WT XT Y Z U S[ R U NPO QR ST U T \] ^ Z U S_ R U;`

a there is no definition of

bPc de fg h g in this file

Page 29: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Client Implementation

ij kml n ij k l o

pq rs ts u v pw x y p

z|{ } yw s p s ~ � � v p w � y p�

v pw x y p z|{ } yw s p s ��� � � � �pq rs ts u v pw x y p

z|{ } yw s p s ~ � � v p w � y p�

Page 30: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Opaque Data Type

� The type

��� �� �� � � is defined in an implementation file

� The client can use the type provided there are no attempts to dereference values of this type

� � �� �� � �8� �;� �� � � �¡ 

¢ is legal, but

£ ¤8¥ ¦;§ ¨© ¦ ª«

ª�¬ ­® «

¯ is illegal, because the type

° ±8² ³;´ µ¶ ³

represents a pointer to the

·�¸ ¹º »¼ ½ ¼ type, and the compiler has no information about this type

Page 31: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Module

1) the type

¾3¿ À ¿ ÁÃÂ ÄÅ of the data stored in the stack is known to the implementation

2) any number of stacks can be created; all stacks must have elements of the same type,

Æ3Ç È Ç ÉÃÊ ËÌ

3) the representation of the stack and stack elements are not visible to the client.

The first version will operate on a stack of integers.

Page 32: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Header

Í Î ÏÑÐ Ò�Ó Ï ÔÕ Ö ×ØÙ

Ú

Í Ò�Ó Ï Î Ð Ó ÔÕ Ö ×ØÙ

Ú

Û�Ü Ý Ó Ò�Ó Ï Î Ð Û Þàß Û ß Õ Ü Ý Ó á

Û�Ü Ý Ó Ò�Ó Ï â Û�ã ä å Û Ô Û ß å æ × ÞÕ ç Ô Û ß å æ Ö ÞÕ á

Ô Û ß å æ Ö ÞÕ Ô Û ß å æÙÐ Ó è éÑê ë Î Òì á

Î Ð Û Ô Û ß å æÙÓ í Ý Û Ü é Ô Û ß å æ Ö ÞÕ â ì á

ê ë Î Ò Ô Û ß å æÙ Ý ä â î é Ô Û ß å æ Ö ÞÕ âðï Þàß Û ß Õ Ü Ý Ó Îì á

Þß Û ß Õ Ü Ý Ó Ô Û ß å æÙ Ý ë Ý é Ô Û ß å æ Ö ÞÕ â ì á

ê ë Î Ò Ô Û ß å æÙ

Ïã Ó Ó é Ô Û ß å æ Ö ÞÕ ç â ì á

ÍàÓ Ð Ò Î Ï

Page 33: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Application of Stack ADT

ñ ò�ó ô õÑö ÷�ø ù�ú û�ü ô ýmþ ÿ ù

� û�ü ô ý� �� ú ��� ú ��

ú � � û�ü ô ý� ó ø � � �

ú � � û�ü ô ý� ó ø � � �

� û�ü ô ý� � ö ú ÿ ú ��� �� �

ò � � � û�ü ô ý� ø � � û�� �ú �� �

�� ò�ó û � ù� ÷� ó ù � � û�ü ô ý� �� � ú �� � �

þ þ þ

Page 34: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation

� ��� � � � !�" #�$ %'& � (*) + #

%�, - " !�" . $ %'/ � � % � 0 !�" 1

2& %'& 3, - " ! 4

$ % / � � % � 0 !�" 5 � " 6 % 4

7 8 0 !�" 3 4

%�, - " !�" . $ %'/ � � % 9 %& � (: 2 3 1

� � % � 0� � % 4

8 0 !�" 3 5 % 0 - 4

7 9 %'& � (: 2 3;4

Page 35: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation

<= >? @A BC <= >? @D EF G H I J KL M N

< = >? @A B C OP

K Q H H O R S > T T J? H O K U F J Q H < = >? @V BC M M M R R WX Y Y M

F Z K= H[ \] CD

^ A ] Y X _[ M P

O;` a ? Jb E= R cP

O;` a= Jd R WX Y YP

e F = b e E OP

fK E= < = >? @

D F Sd = g H <= >? @A B C O M N

e F = b e E O` a ? Jb E= R R cP

f

Page 36: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation

h i jk lm no pq rs t uv lm n o pw xy t{z x nm ny | r} k ~ �

� ik } y ��� } � � ik } �

j � v v � } � � ik } � � n � � i o v t j � } i �v � ik } y ~ ~ ~ � �

�� � � ~ �

� r� j� m �v t m k } � � z � rs t u � t m n o p j t �s � �� � � ~ �

} � jm v �� � yq

� w � � � � � ~ �

�� } � � ik } � � k � k �

� } � � ik } � �� } �m � t � �m i r �

t � �m i r � � } � � i k } �

t � � o is � m � � �

Page 37: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation

��� � � �;�  ¡ ¢ � � £ ¤¥  ¦   § ¢ � � £ ¤¨ � � © ª «

� � � � �;�   ¡ ¬ ­

®¦ ¬¡ � ¯ ¦ ° ¬ ®¦ ¬¡ ­

¦ ° ¬ ®¦ ¬¡ ± ©;² ³ � ¦   ­

© ² ³ � ¦   ± ¦ ° ¬ ®¦ ¬¡ ² ³´ ¡ µ � ­

© ² ³ £¦ ¶´ �·² ² ­

¬ ± ¦ ° ¬ ®¦ ¬¡ ² ³ ¬ ­

¸ ¹ ¡ ¡ § ¦ ° ¬ ®¦ ¬¡ ª ­

¹ ¡ � ¶ ¹ ´ ¬­

º

Page 38: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation

» ¼ ½¾ ¿À ÁÂ ÃÄ

Å Æ Ç Ç È ¿À Á  ÃÉ ÊË Ì�Í Î Ï

Ð ¼¾ ÇË Ì�Ñ Ò Ì�Ó Ô

Å ¼ Æ È Ñ Õ È Ì�Í Î×Ö ØÀ ¼Ñ Ô Ñ Ô Ñ Õ Ó Î Ï

Ó Õ Ñ Ö ØÙ ÇÚ À Ô

Å Æ Ç Ç È Ñ Î Ô

ÛÅ Æ Ç Ç È ÌÍ Î Ô

Ì Í Õ ÐÜ Ý Ý

Û

Page 39: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Shallow and Deep Copy

Þ To push a new element

ß

onto the stack:

àá â ãä åæá ç è å é åëê

ì If íî ï ðñ òî ó ô ò

and

õ

are pointers, this results in a shallow copy.

ö If client deallocates variable pointed to by

÷

then

øù ú ûü ýù þ ÿ ý

becomes a dangling reference

Page 40: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Shallow and Deep Copy

For a deep copy, use a callback function

�� �� ��� � � � � � � � �

. For example, for strings and doubles:

�� � � ��� �� �� � � � � � �� � � � � � �� �� � � � � �� � � � �

! � �#" ! � � � ! $" � � � �&%

' �� � � ��� �� �� � � � � � �� � � � � � �� �� � � � � �� � � � �

! � �#" ! � �%

'

Page 41: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

( )+* , -�. / , 021 3 4 (5 6. / ( ) * , -�. 7 8

- ,9 / 1 , 6 * ( 5 3: 5 ; . -

( ) * , - < 5 3: = - ( 5 3: 5 ; . -

> ; 4 4 <5 3: ? ( * @ @ 5 ( 4 . / , @+9 6 4 . 7 A BC

. >2D 9 5 ; 4 ( )+* , 7 7 7 ? ? EF G G 7

,9 / 1 , 6 EF G G =

. / , ( 3: 4 <5 3: C . 7 =

,9 / 1 , 6 <5 3: =

H

Page 42: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Shallow and Deep Copy

We need another callback function,

I2J K K L�M N M OP N M Q RS T

For example, for string and doubles:

U V WX Y2Z [ [ \�] ^ ] _` ^ ] a bc \] ^ ] dfe g [ U h i

Y2Z [ [ c U h2j

kU V WX Y2Z [ [ \�] ^ ] _

` ^ ] a bc \] ^ ] dfe g [ U h i

k

Page 43: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Header with Deep Copy

l#m no po q r sut v w x t l t ym no z

l#m no po q { l v| r l } l t r ~� x y w } l t r ~� x y z

x t l t ym no r� nm x t l t �} l t r ~� r � � { l x t l t l m no � � z

�� � p q vo o x t l t �} l t r ~� x t l t ym no � � z

} l t r ~� x y } l t r ~� �o �� �� � p � z

� � l } l t r ~�o � n l m � } l t r ~� x y { � z

�� � p } l t r ~� n | { s� } l t r ~� x y {�� x t l t ym no � � z

x t l t ym no } l t r ~� n � n� } l t r ~� x y { � z

�� � p } l t r ~�

q vo o � } l t r ~� x y { � z

Page 44: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Header with Deep Copy

� Implementation of callback functions must be provided by the client

��� � � ��� �� �� � � � � � � �� � � � �� � � �� � ��� � � � � �� � �&�

�� �  ¡&¢ � � ��� � � �� � � � �� � � � � ��� �� � ��

£ They are declared in the header file so that the implementation code can call them

Page 45: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation with Deep Copy

¤ ¥ ¦§ ¨© ª« ¬­ ®¯ ° ±² ¨© ª « ¬³ ´µ °�¶ ´ ª© ªµ · ®¸ § ¹ º

» ¥§ ¸ µ ¼�½ ¸ ¾ » ¥§ ¸ ¿

¦ À ² ² ½ ¸ ¾ » ¥§ ¸ Á  ª à à ¥ « ² ° ¦&Ä ¸ ¥ À² » ¥§ ¸ µ ¹ ¹ ¹ Á Á

»Å Æ Æ ¹ º

À ®Ç ¦½ © À² ° © § ¸ Ç Ç ¶ È ®¯ ° ±&É ° © ª « ¬ ¦ ° À¯ à ÃÊ ½ È ¹ ¿

¸ Ë ¦© ² ÌÍ Î µ­

Ï ³ Î Æ Å Ð Ì ¹ ¿

ѽ ¸ ¾ » ¥§ ¸ Ò Ó § Á « ¥ ® · ´ ª© ª ­¨© ª « ¬ ² § ¹ ¿

½ ¸ ¾ » ¥§ ¸ Ò Ó½ ¸ Ë© Á ° Ò Ó© ¥ ® ¿

° Ò Ó© ¥ ® Á ½ ¸ ¾ » ¥ § ¸ ¿

° Ò Ó « ¥¯ ½ © Ô Ô ¿

Ñ

Page 46: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation with Deep CopyÕ�Ö × Ö Ø�Ù ÚÛ Ü × Ö Ý Þ

ß Úà Ú á Ü × Ö Ý Þâ Õ Ø ã ä å

Õ Ö × Ö Ø�Ù Ú Û æ&ç

èà æÛ Ø é à ê æ èà æÛ ç

à ê æ èà æÛ ë ã�ì í × à Ú ç

ã ì í × à Ú ë à ê æ èà æÛ ì íî Û ï × ç

ã ì í Ýà ðî ×ñì ì ç

æ ë Ýà Ú Ù Õ Ö × Ö ßÜ × Ö Ý Þ á à ê æ èà æÛ ì í æ ä ç

ò&ó Û Û Õ Ö × Ö ßÜ × Ö Ý Þ á à ê æ èà æuÛ ì í æ ä ç

ò&ó Û Û á à ê æ èà æÛ ä ç

ó Û × ð ó î æç

ô

Page 47: Queues, Stack Modules, and Abstract Data Typesaubanel/cs2023/ADT.pdf · and Abstract Data Types C for Java Programmers, Chapter 11 (11.5) and C Programming - a Modern Approach, Chapter

Stack ADT Implementation with Deep Copy

õ ö ÷ø ùú ûü ýþ

ÿ�� � � � ùú û ü ý� �� �� �

� öø �� � � ��

ÿ ö � � � � �� �� �ú ö � � � � �

� � � �� �� ú �

ÿ�� � � � ûú û þùú û ü ý � � � ø �

ÿ�� � � � �

�ÿ � � � � �� �

� � � �� � �