DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
David Burdett May 11, 2004 Package Binding for WS CDL.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
Red Tag Date 13/12/11 5S.
PP Test Review Sections 6-1 to 6-6
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Linked Lists.
Linked Lists Chapter 4.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Using C++
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010.
Double-Linked Lists and Circular Lists
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSE Lecture 12 – Linked Lists …
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Hash Tables Dr. Li Jiang School of Computer Science,
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
The List ADT Textbook Sections
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Overview Hash Table Hash Function Hash table ADT operations
Adding Up In Chunks.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Linked Lists CENG 213 Data Structures.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
CMSC 341 Lists 3. 2 Doubly-Linked Lists Option: add pointer to previous node Issues –doubles number of pointers –allows immediate (O(1)) access to previous.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CMSC 341.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
CMSC 341.
CMSC 341.
CMSC 341 List 2.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set:
Presentation transcript:

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI Lecture Notes 3 Prepared by İnanç TAHRALI

ROAD MAP Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists

Abstract Data Types (ADT) Definition : Is a set of operation Mathematical abstraction No implementation detail Example : Lists, sets, graphs, stacks are examples of ADT along with their operations

Why ADT ? Modularity Reuse Easy to change of implementation divide program into small functions easy to debug and maintain easy to modify group work Reuse do some operations only once Easy to change of implementation transparent to the program

THE LIST ADT Ordered sequence of data items called elements A1, A2, A3, …,AN is a list of size N size of an empty list is 0 Ai+1 succeeds Ai Ai-1 preceeds Ai position of Ai is i first element is A1 called “head” last element is AN called “tail” Operations ?

THE LIST ADT Operations PrintList Find FindKth Insert Delete Next Previous MakeEmpty

THE LIST ADT Example: the elements of a list are 34, 12, 52, 16, 12 Find (52)  3 Insert (20, 3)  34, 12, 52, 20, 16, 12 Delete (52)  34, 12, 20, 16, 12 FindKth (3)  20

Implementation of Lists Many Implementations Array Linked List Cursor (linked list using arrays)

ROAD MAP Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists

Array Implementation of List ADT Need to define a size for array High overestimate (waste of space) Operations Running Times PrintList O(N) Find Insert O(N) (on avarage half needs to be moved) Delete FindKth Next O(1) Previous

Array Implementation of List ADT Disadvantages : insertion and deletion is very slow need to move elements of the list redundant memory space it is difficult to estimate the size of array

ROAD MAP Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists

Linked List Implementation of Lists Series of nodes not adjacent in memory contain the element and a pointer to a node containing its succesor Avoids the linear cost of insertion and deletion !

Linked List Implementation of Lists Insertion into a linked list

Linked List Implementation of Lists Deletion from a linked list

Linked List Implementation of Lists Need to know where the first node is the rest of the nodes can be accessed No need to move the list for insertion and deletion operations No memory waste

Linked List Implementation of Lists Linked List Array PrintList O(N) (traverse the list) O(N) Find FindKth (L,i) O(i) O(1) Delete O(1) O(N) Insert

Programming Details There are 3 special cases for linked lists Insert an element at the front of the list there is no really obvious way Delete an element from the front of the list changes the start of the list Delete an element in general requires to keep track of the node before the deleted one How can we solve these three problems ?

Programming Details Keep a header node in position 0 Write a FindPrevious routine returns the predecessor of the cell To delete the first element FindPrevious routine returns the position of header Use of header node is controversial !

Type decleration for link list node template <class Object> class List; // Incomplete declaration. class ListItr; // Incomplete declaration. class ListNode { ListNode( const Object & theElement = Object( ), ListNode*n=NULL) : element(theElement),next(n) {} Object element; ListNode *next; friend class List<Object>; friend class ListItr<Object>; };

Iterator class for linked lists template <class Object> class ListItr { public: ListItr( ) : current( NULL ) { } bool isPastEnd( ) const { return current == NULL; } void advance( ) { if( !isPastEnd( ) ) current = current->next; } const Object & retrieve( ) const { if( isPastEnd( ) ) throw BadIterator( ); return current->element; } private: ListNode<Object> *current; // Current position ListItr(ListNode<Object> *theNode):current( theNode ) { } friend class List<Object>; // Grant access to constructor };

List class interface template <class Object> class List { public: List( ); List( const List & rhs ); ~List( ); bool isEmpty( ) const; void makeEmpty( ); ListItr<Object> zeroth( ) const; ListItr<Object> first( ) const; void insert( const Object & x, const ListItr<Object> & p ); ListItr<Object> find( const Object & x ) const; ListItr<Object> findPrevious( const Object & x ) const; void remove( const Object & x ); const List & operator=( const List & rhs ); private: ListNode<Object> *header; };

Function to print a list template <class Object> void printList( const List<Object> &the List) { if (theList.isEmpty()) cout<< “Empty list” << endl; else ListItr<Object> itr = theList.first(); for (; !itr.isPastEnd(); itr.advance()) cout << itr.retrieve() <<“ ”; } cout << endl;

Some list one-liners /* Construct the list */ template <class Object> List<Object>::List( ) { header = new ListNode<Object>; } /* Test if the list is logically empty */ bool List<Object>::isEmpty( ) const return header->next == NULL;

Some list one liners /* Return an iterator representing the header node template <class Object> ListItr<Object> List<Object>::zeroth( ) const { return ListItr<Object>( header ); } /* Return an iterator representing the first node in the list. This operation is valid for empty lists. */ ListItr<Object> List<Object>::first( ) const return ListItr<Object>( header->next );

Find routine /* Return iterator corresponding to the first node containing an item x. Iterator isPastEnd if item is not found. */ template <class Object> ListItr<Object> List<Object>::find( const Object & x ) const { ListNode<Object> *itr = header->next; while( itr != NULL && itr->element != x ) itr = itr->next; return ListItr<Object>( itr ); }

Deletion routine for linked lists /* Remove the first occurrence of an item x. */ template <class Object> void List<Object>::remove( const Object & x ) { ListItr<Object> p = findPrevious( x ); if( p.current->next != NULL ) ListNode<Object> *oldNode = p.current->next; p.current->next = p.current->next->next; delete oldNode; }

findPrevious-the find routine for use with remove /*Return iterator prior to the first node containing an item x. template <class Object> ListItr<Object> List<Object>::findPrevious( const Object & x ) const { ListNode<Object> *itr = header; while( itr->next != NULL && itr->next->element != x ) itr = itr->next; return ListItr<Object>( itr ); }

Insertion routine for linked lists /* Insert item x after p. */ template <class Object> void List<Object>::insert( const Object & x, const ListItr<Object> & p ) { if( p.current != NULL ) p.current->next = new ListNode<Object> ( x, p.current->next ); }

makeEmpty and List destructor /* Make the list logically empty. */ template <class Object> void List<Object>::makeEmpty( ) { while( !isEmpty( ) ) remove( first( ).retrieve( ) ); } /* Destructor */ List<Object>::~List( ) makeEmpty( ); delete header;

List copy routines: operator= /*Deep copy of linked lists. template <class Object> const List<Object> & List<Object>::operator=( const List<Object> & rhs ) { ListItr<Object> ritr = rhs.first( ); ListItr<Object> itr = zeroth( ); if( this != &rhs ) makeEmpty( ); for( ; !ritr.isPastEnd( ); ritr.advance( ),itr.advance( )) insert( ritr.retrieve( ), itr ); } return *this;

List copy routines : copy constructor template <class Object> List<Object>::List( const List<Object> & rhs ) { header = new ListNode<Object>; *this = rhs; }

Doubly Linked List Traversing list backwards not easy with regular lists Insertion and deletion more pointer fixing Deletion is easier Previous node is easy to find

Circulary Linked List Last node points the first

ROAD MAP Abstract Data Types (ADT) The List ADT Implementation of Lists Array implementation of lists Linked list implementation of lists Cursor implementation of lists

Cursor Implementation of Linked List Problems with linked list implementation: Same language do not support pointers ! Then how can you use linked lists ? new and free operations are slow Actually not constant time

Cursor Implementation of Linked List SOLUTION: Implement linked list on an array called CURSOR

Cursor Implementation of Linked List Cursor operation simulates the features Collection of structures uses array for nodes Array index is pointer new and delete operation Keep a free list new returns an element from freelist delete place the node in freelist Freelist Use cell 0 as header All nodes are free initially 0 is a NULL pointer

Cursor Implementation of Linked List If L = 5, then L represents list (A, B, E) If M = 3, then M represents list (C, D, F)

Iterator for cursor implementation of linked lists template <class Object> class ListItr { public: ListItr( ) : current( 0 ) { } bool isPastEnd( ) const {return current == 0; } void advance( ){ if( !isPastEnd( ) ) current = List<Object>::cursorSpace[ current ].next; } const Object & retrieve( ) const { if( isPastEnd( ) ) throw BadIterator( ); return List<Object>::cursorSpace[ current ].element; } private: int current; // Current position friend class List<Object>; ListItr( int theNode ) : current( theNode ) { } };

Class skeleton for cursor-based List template <class Object> class ListItr; // Incomplete declaration. class List { public: List( ); List( const List & rhs ); ~List( ); bool isEmpty( ) const; void makeEmpty( ); ListItr<Object> zeroth( ) const; ListItr<Object> first( ) const; void insert( const Object & x, const ListItr<Object> & p ); ListItr<Object> find( const Object & x ) const; ListItr<Object> findPrevious( const Object & x ) const; void remove( const Object & x );

Class skeleton for cursor-based List public: struct CursorNode { CursorNode( ) : next( 0 ) { } private: CursorNode( const Object & theElement, int n ) : element( theElement ), next( n ) {} Object element; int next; friend class List<Object>; friend class ListItr<Object>; }; const List & operator=( const List & rhs );

Class skeleton for cursor-based List private: int header; static vector<CursorNode> cursorSpace; static void initializeCursorSpace( ); static int alloc( ); static void free( int p ); friend class ListItr<Object>; };

cursorSpace initialization /* Routine to initialize the cursorSpace. */ template <class Object> void List<Object>::initializeCursorSpace( ) { static int cursorSpaceIsInitialized = false; if( !cursorSpaceIsInitialized ) cursorSpace.resize( 100 ); for( int i = 0; i < cursorSpace.size( ); i++ ) cursorSpace[ i ].next = i + 1; cursorSpace[ cursorSpace.size( ) - 1 ].next = 0; cursorSpaceIsInitialized = true; }

Routines : alloc and free /* Allocate a CursorNode template <class Object> int List<Object>::alloc( ) { int p = cursorSpace[ 0 ].next; cursorSpace[ 0 ].next = cursorSpace[ p ].next; return p; } /* Free a CursorNode void List<Object>::free( int p ) cursorSpace[ p ].next = cursorSpace[ 0 ].next; cursorSpace[ 0 ].next = p;

Short routines for cursor-based lists /* Construct the list template <class Object> List<Object>::List( ) { initializeCursorSpace( ); header = alloc( ); cursorSpace[ header ].next = 0; } /* Destroy the list List<Object>::~List( ) makeEmpty( ); free( header );

Short routines for cursor-based lists /* Test if the list is logically empty. return true if empty template <class Object> bool List<Object>::isEmpty( ) const { return cursorSpace[ header ].next == 0; } /* Return an iterator representing the first node in the list. This operation is valid for empty lists. ListItr<Object> List<Object>::first( ) const return ListItr<Object>( cursorSpace[ header ].next );

find routine - cursor implementation /*Return iterator corresponding to the first node containing an item x. Iterator isPastEnd if item is not found. template <class Object> ListItr<Object> List<Object>::find( const Object & x ) const { int itr = cursorSpace[ header ].next; while( itr != 0 && cursorSpace[ itr ].element != x ) itr = cursorSpace[ itr ].next; return ListItr<Object>( itr ); }

insertion routine-cursor implementation /* Insert item x after p. template <class Object> void List<Object>::insert(const Object & x,const ListItr<Object> & p) { if( p.current != 0 ) int pos = p.current; int tmp = alloc( ); cursorSpace[ tmp ] = CursorNode( x, cursorSpace[ pos ].next ); cursorSpace[ pos ].next = tmp; }

deletion routine - cursor implementation /* Remove the first occurrence of an item x. template <class Object> void List<Object>::remove( const Object & x ) { ListItr<Object> p = findPrevious( x ); int pos = p.current; if( cursorSpace[ pos ].next != 0 ) int tmp = cursorSpace[ pos ].next; cursorSpace[ pos ].next = cursorSpace[ tmp ].next; free ( tmp ); }