ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
Linked Lists CENG 213 Data Structures.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Templates and the STL.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Templates Zhen Jiang West Chester University
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
C++ Classes and Data Structures Jeffrey S. Childs
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 6 – September 6 th, 2001.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 9 – September 18, 2001.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
C++ Programming:. Program Design Including
Big-O notation Linked lists
CC 215 Data Structures Stack ADT
OOP-4-Templates, ListType
Stack and Queue APURBO DATTA.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Linked List Yumei Huo Department of Computer Science
Recursion.
Chapter 16-2 Linked Structures
CS 2308 Exam II Review.
C++ Plus Data Structures
CS 2308 Exam II Review.
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked List Implementation
Chapter 16 Linked Structures
Lecture No.02 Data Structures Dr. Sohail Aslam
Programming II (CS300) Chapter 07: Linked Lists
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

ICOM 4035Dr. Manuel Rodriguez Martinez2 Readings Chapter 17 of textbook: Linked Lists

ICOM 4035Dr. Manuel Rodriguez Martinez3 Limitations in first Linked List version The first linked list version had two main drawback –Need to re-define the Object data type for each different type of data object that we want to store in the list. Linked list behavior does not change, whether we store integers of strings. Could we have a generic linked list that is used for all types? –Returning pointers to positions in the linked list is a bad idea. People can delete these pointers, breaking the linked list Can we return some object that provides access to the nodes in the list, but without giving the pointer.

ICOM 4035Dr. Manuel Rodriguez Martinez4 Limitation 1: fixed Object type  BobbyAnaBecky header  header No difference between string list and integer list

ICOM 4035Dr. Manuel Rodriguez Martinez5 Limitation 2: Returning pointers  BobbyAnaBecky header  Bobby header By deleting the P pointer, half the list is lost ? P = L.find(“Ana”); delete P; Corrupted Linked list

ICOM 4035Dr. Manuel Rodriguez Martinez6 Solutions Implement the linked list as generic container class using templates. –Parameterized the type for the element to be used in the linked list. –Write the linked list code once, and keep re-using (“recycling”) it. To return positions in the list, don’t return pointers to the node, but return objects holding those pointers as private members. This is called an iterator. –It allows you to move over the nodes in the list, one after the other. Iterate over the nodes in the list.

ICOM 4035Dr. Manuel Rodriguez Martinez7 C++ Templates A template provides a pattern for implementing an algorithm whose basic behavior is independent of the data types of its input. Example: Swap operation. Integer swap: Character swap: void swap(int& a, int&b){ int temp = a; a = b; b= temp } void swap(char& a, char&b){ char temp = a; a = b; b= temp }

ICOM 4035Dr. Manuel Rodriguez Martinez8 A Swap pattern As we can see swap is the same regardless of the types being swapped. Template provide mechanism to create generic swap function. –A swap pattern –Specific type use is a parameter of the template. –Swap template function: template void swap(Type& a, Type& b){ Type temp = a; a = b; b = temp }

ICOM 4035Dr. Manuel Rodriguez Martinez9 A Swap pattern (Cont) In the template declaration, the Type parameter specifies the data type to be used in the template. We can now call swap in many different ways, without re-writing code, or without defining the Object type. char c1 = ‘a’, c2 = ‘b’; int num1 = -1, num2 = 20; swap(c1, c2); swap(num1, num2);

ICOM 4035Dr. Manuel Rodriguez Martinez10 A note about templates Templates are not actual function instances, but rather a “recipe” for making functions based on the parameterized types. The compiler will bind specific data types as parameters to the templates during compilation. Then it will generate actual code, using the pattern provide by the template to create a function instance for that type. Compile time errors with templates are complex to understand, because they are given based on the code generated by the compiler.

ICOM 4035Dr. Manuel Rodriguez Martinez11 Linked list and templates We can implement a linked list as container class that uses templates. The type of data element stored in a node of the list is the parameter to the template. This will be a general purpose linked list that be used in any situation.

ICOM 4035Dr. Manuel Rodriguez Martinez12 Singly linked list node // define a node for the linked list // node stores “generic” data type. template struct Node { // data field ListData data; // pointer to the next node in the list Node *next; }; data next

ICOM 4035Dr. Manuel Rodriguez Martinez13 Sample linked lists  header Empty singly Linked list  BobbyAnaBecky Singly linked list with three elements header

ICOM 4035Dr. Manuel Rodriguez Martinez14 Linked list interface Same as before, but using templates and return iterators from find() and first() methods. Available from class web page.

ICOM 4035Dr. Manuel Rodriguez Martinez15 Constructor Makes an empty list. template Linkedlist ::linkedlist(){ header = new Node ; // node of ListData header->next = NULL; list_size = 0; }  header

ICOM 4035Dr. Manuel Rodriguez Martinez16 Destructor Removes all elements from the list and then deletes the header.    Bob Tom 

ICOM 4035Dr. Manuel Rodriguez Martinez17 Destructor template Linkedlist ::~linkedlist(){ // remove all the stuff from the list make_empty(); // remove the header delete header; }

ICOM 4035Dr. Manuel Rodriguez Martinez18 Insert operation Adds a new element after the header of the list  Bob Tom  Bob Tom Will header

ICOM 4035Dr. Manuel Rodriguez Martinez19 Insert operation template void linkedlist::insert(const ListData& obj){ temp = new Node ; // creates the new node temp->data = obj; // adds the data to it temp->next = header->next; // adds it to the list header->next = temp; // make header point to it ++list_size; // increase list size }

ICOM 4035Dr. Manuel Rodriguez Martinez20 Erase operation Removes the first occurrence of an object from the list. Returns true if the element is deleted, false if not found.  Bob Tom Bill erase(“Bill”);  Bob Tom Bill header

ICOM 4035Dr. Manuel Rodriguez Martinez21 Erase operation template bool linkedlist ::erase(const ListData& obj){ Node *temp1 = NULL, *temp2 = NULL; for ( temp1 = header->next, temp2 = header; temp1 != NULL; temp1 = temp1->next, temp2 = temp2->next){ if (temp1->data == obj){ // remove from the list temp2->next = temp1->next; temp1->next = NULL; delete temp1; --list_size; return true; } return false; // if we get here, the element obj was not in the list. }

ICOM 4035Dr. Manuel Rodriguez Martinez22 Is Empty operation Determines if the list is empty –If header->next is null template bool linkedlist ::is_empty() const { // test is header->next is the null pointer. return header->next == null; }

ICOM 4035Dr. Manuel Rodriguez Martinez23 Make Null operation Erase all elements from the list until is becomes an empty list.    Bob Tom

ICOM 4035Dr. Manuel Rodriguez Martinez24 Make Null operation template void linkedlist ::make_empty() { while (!is_empty()){ erase(header->next); }

ICOM 4035Dr. Manuel Rodriguez Martinez25 Size operation Returns the size of the list template int linkedlist ::size() const { return list_size; }