Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICOM 4015 Advanced Programming

Similar presentations


Presentation on theme: "ICOM 4015 Advanced Programming"— Presentation transcript:

1 ICOM 4015 Advanced Programming
Lecture 7 Dynamic Memory II Abstract Data Types Reading: LNN Chapter Prof. Bienvenido Vélez 5/27/2019 ICOM 4015

2 Dynamic Memory II Outline
Abstract data types (ADT) List Abstract Data Type Linked list implementation of List ADT 5/27/2019 ICOM 4015

3 Problem Need to Store set of Dynamic Objects Solution 1: Dynamic Arrays
PROGRAM code Array Pointer object Global static variables p STACK activation records HEAP Dynamic objects dynamic array 5/27/2019 ICOM 4015

4 Problem Need to Store set of Dynamic Objects Solution 2: Linked Structure
PROGRAM code Pointer object Global static variables p STACK activation records HEAP Dynamic objects dynamic array 5/27/2019 ICOM 4015

5 A 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); 5/27/2019 ICOM 4015

6 Linked lists implementation of List ADT (Part I)
// lists.cc // Implementes singly linked lists ADT extern "C" { #include <stdlib.h> } #include <iostream> #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->next = NullNode; temp->datum = d; if (l.first == NullNode) { l.first = temp; else { Node* n = l.first; while(n->next != NullNode) { n = n->next; n->next = temp; l.length++; return l; 5/27/2019 ICOM 4015

7 Linked lists implementation of List ADT (Part II)
List& listPrepend(List& l, DatumType d) { Node* temp = new Node; temp->datum = d; temp->next = 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->next; List& listInsert(List& l, DatumType d) Node* n = l.current; temp->next = l.current->next; l.current->next = temp; DatumType listCurrent(const List l) return l.current->datum; bool listEOL(const List l) return (l.current == NullNode); 5/27/2019 ICOM 4015

8 Using the List ADT IMPORTANT
#include <iostream> #include "lists.h" int main() { List l; listInit(l); for(int i = 1; i < 10; i++) { listPrepend(l,i); } cout << "Original list" << endl; listDump(l); // Demonstrate iteration without exposing pointers for (listStart(l); !listEOL(l); listNext(l)) { DatumType curr = listCurrent(l); cout << "Next datum: " << curr << endl; listStart(l); listNext(l); listInsert(l,100); cout << "List with 100 inserted" << endl; IMPORTANT The fact that the list is implemented using pointers remains hidden from users of the module. The implementation ABSTRACTS OUT irrelevant detail 5/27/2019 ICOM 4015

9 List ADT Output ~/icom4015/lec15 >>g++ lists.cc main.cc -o lists ~/icom4015/lec15 >>lists 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 100 ~/icom4015/lec15 >> 5/27/2019 ICOM 4015

10 Abstract 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 5/27/2019 ICOM 4015


Download ppt "ICOM 4015 Advanced Programming"

Similar presentations


Ads by Google