Title: ICOM 4015 Advanced Programming
1ICOM 4015 Advanced Programming
- Lecture 7
- Dynamic Memory II
- Abstract Data Types
- Reading LNN Chapter
Prof. Bienvenido Vélez
2Dynamic Memory IIOutline
- Abstract data types (ADT)
- List Abstract Data Type
- Linked list implementation of List ADT
3ProblemNeed to Store set of Dynamic
ObjectsSolution 1 Dynamic Arrays
PROGRAM code
Array Pointer object
Global static variables
p
dynamic array
4ProblemNeed to Store set of Dynamic
ObjectsSolution 2 Linked Structure
PROGRAM code
Pointer object
Global static variables
p
dynamic array
5A List Abstract Data Type (ADT)
// lists.h // Global declarations for linked
lists module // List data structures typedef
int DatumType struct Node DatumType datum
Node next struct List Node first
Node current int length // Operations
on linked lists // List initialization void
listInit (List l) // List modification List
listAppend (List l, DatumType d) List
listPrepend(List l, DatumType d) List
listInsert (List l, DatumType d) // List
interation void listStart (List l) void
listNext (List l) DatumType
listCurrent(const List l) bool listEOL
(const List l) // List printing void listDump
(const List l)
6Linked lists implementation of List ADT (Part I)
// lists.cc // Implementes singly linked lists
ADT extern "C" include ltstdlib.hgt include
ltiostreamgt include "lists.h" Node NullNode
(Node )NULL // Operations on linked lists //
List initialization void listInit(List l)
l.first NullNode l.current NullNode
l.length 0 // List modification List
listAppend(List l, DatumType d) Node temp
new Node temp-gtnext NullNode temp-gtdatum
d if (l.first NullNode) l.first
temp else Node n l.first
while(n-gtnext ! NullNode) n n-gtnext
n-gtnext temp l.length
return l
7Linked lists implementation of List ADT (Part II)
List listPrepend(List l, DatumType d)
Node temp new Node temp-gtdatum d
temp-gtnext l.first l.first temp void
listStart(List l) l.current
l.first void listNext(List l) if
(l.current ! NullNode) l.current
l.current-gtnext List listInsert(List l,
DatumType d) Node n l.current Node
temp new Node temp-gtdatum d temp-gtnext
l.current-gtnext l.current-gtnext
temp DatumType listCurrent(const List l)
return l.current-gtdatum bool listEOL(const
List l) return (l.current NullNode)
8Using the List ADT
include ltiostreamgt include "lists.h" int
main() List l listInit(l) for(int i
1 i lt 10 i) listPrepend(l,i)
cout ltlt "Original list" ltlt endl listDump(l)
// Demonstrate iteration without exposing
pointers for (listStart(l) !listEOL(l)
listNext(l)) DatumType curr
listCurrent(l) cout ltlt "Next datum " ltlt
curr ltlt endl listStart(l) listNext(l)
listInsert(l,100) cout ltlt "List with 100
inserted" ltlt endl listDump(l)
IMPORTANT The fact that the list is implemented
using pointers remains hidden from users of the
module. The implementation ABSTRACTS OUT
irrelevant detail
9List ADT Output
bvelez_at_amadeus /icom4015/lec15 gtgtg lists.cc
main.cc -o lists bvelez_at_amadeus
/icom4015/lec15 gtgtlists Original
list 9 8 7 6 5 4 3 2 1 Next datum 9 Next datum
8 Next datum 7 Next datum 6 Next datum 5 Next
datum 4 Next datum 3 Next datum 2 Next datum
1 List with 100 inserted 9 8 100 7 6 5 4 3 2 1 bv
elez_at_amadeus /icom4015/lec15 gtgt
10Abstract Data Types (ADT)Summary
- Consists of the definition of a type of object
and a set of operations that can be performed on
object of the type - An ADT describes a type of object together with
the set of operations defined on the object - An ADT hides irrelevant implementation details
- An ADT shields its users from dependencies on
hidden details