1 / 10

ICOM 4015 Advanced Programming

ICOM 4015 Advanced Programming. Lecture 12 Dynamic Memory II Abstract Data Types Reading: Chapter 5. Prof. Bienvenido Vélez. Dynamic Memory II Outline. Abstract data types (ADT) List Abstract Data Type Linked list implementation of List ADT. STACK activation records. HEAP

ljennifer
Download Presentation

ICOM 4015 Advanced Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICOM 4015 Advanced Programming Lecture 12 Dynamic Memory II Abstract Data Types Reading: Chapter 5 Prof. Bienvenido Vélez ICOM 4015

  2. Dynamic Memory IIOutline • Abstract data types (ADT) • List Abstract Data Type • Linked list implementation of List ADT ICOM 4015

  3. STACK activation records HEAP Dynamic objects ProblemNeed to Store set of Dynamic ObjectsSolution 1: Dynamic Arrays PROGRAM code Array Pointer object Global static variables p dynamic array ICOM 4015

  4. STACK activation records HEAP Dynamic objects ProblemNeed to Store set of Dynamic ObjectsSolution 2: Linked Structure PROGRAM code Pointer object Global static variables p dynamic array 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); 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; } 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; Node* temp = new Node; temp->datum = d; 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); } ICOM 4015

  8. Using the List ADT #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; 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 ICOM 4015

  9. List ADT Output [bvelez@amadeus] ~/icom4015/lec15 >>g++ lists.cc main.cc -o lists [bvelez@amadeus] ~/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 9 8 100 7 6 5 4 3 2 1 [bvelez@amadeus] ~/icom4015/lec15 >> 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 ICOM 4015

More Related