1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
An introduction to pointers in c
Data Structures: A Pseudocode Approach with C
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Lecture No.01 Data Structures Dr. Sohail Aslam
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
File Storage Organization The majority of space on a device is reserved for the storage of files. When files are created and modified physical blocks are.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Chapter 11 Data Structures. Understand arrays and their usefulness. Understand records and the difference between an array and a record. Understand the.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Subject Name : Data Structure Using C Title : Linked Lists
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
1 Binary trees Outline In this talk, we will look at the binary tree data structure: –Definition –Properties –A few applications Ropes (strings) Expression.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Top 50 Data Structures Interview Questions
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Linked Lists Chapter 6 Section 6.4 – 6.6
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Data Structure Interview Question and Answers
CS522 Advanced database Systems
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
week 1 - Introduction Goals
LINKED LISTS CSCD Linked Lists.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
structures and their relationships." - Linus Torvalds
Object Oriented Programming COP3330 / CGS5409
Node-based storage with arrays
Doubly Linked List Implementation
Chapter 17: Linked Lists.
Linked Lists.
structures and their relationships." - Linus Torvalds
Lecture 3 – Data collection List ADT
Presentation transcript:

1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms of memory allocation Contiguous Linked Indexed –The prototypical examples of these: arrays and linked lists –Other data structures: Trees Hybrids Higher-dimensional arrays –Finally, we will discuss the run-time of queries and operations on arrays and linked lists 2.2

2 Data Structures and Algorithms Memory Allocation Memory allocation can be classified as either –Contiguous –Linked –Indexed Prototypical examples: –Contiguous allocation:arrays –Linked allocation:linked lists 2.2.1

3 Data Structures and Algorithms Memory Allocation Contiguous, adj. Touching or connected throughout in an unbroken sequence. Meriam Webster Touching, in actual contact, next in space; meeting at a common boundary, bordering, adjoining

4 Data Structures and Algorithms Contiguous Allocation An array stores n objects in a single contiguous space of memory Unfortunately, if more memory is required, a request for new memory usually requires copying all information into the new memory –In general, you cannot request for the operating system to allocate to you the next n memory locations

5 Data Structures and Algorithms Linked Allocation Linked storage such as a linked list associates two pieces of data with each item being stored: –The object itself, and –A reference to the next item In C++ that reference is the address of the next node

6 Data Structures and Algorithms Linked Allocation This is a class describing such a node template class Node { private: Type element; Node *next_node; public: //... };

7 Data Structures and Algorithms Linked Allocation The operations on this node must include: –Constructing a new node –Accessing (retrieving) the value –Accessing the next pointer Node( const Type& = Type(), Node* = nullptr ); Type retrieve() const; Node *next() const; Pointing to nothing has been represented as: C NULL Java/C# null C++ (old) 0 C++ (new) nullptr SymbolicallyØ

8 Data Structures and Algorithms Linked Allocation For a linked list, however, we also require an object which links to the first object The actual linked list class must store two pointers –A head and tail: Node *head; Node *tail; Optionally, we can also keep a count int count; The next_node of the last node is assigned nullptr

9 Data Structures and Algorithms Linked Allocation The class structure would be: template class List { private: Node *head; Node *tail; int count; public: // constructor(s)... // accessor(s)... // mutator(s)... };

10 Data Structures and Algorithms Indexed Allocation With indexed allocation, an array of pointers (possibly NULL) link to a sequence of allocated memory locations Used in the C++ standard template library Computer engineering students will see indexed allocation in their operating systems course

11 Data Structures and Algorithms Indexed Allocation Matrices can be implemented using indexed allocation:

12 Data Structures and Algorithms Indexed Allocation Matrices can be implemented using indexed allocation –Most implementations of matrices (or higher-dimensional arrays) use indices pointing into a single contiguous block of memory Row-major order Column-major order C, Python Matlab, Fortran

13 Data Structures and Algorithms Other Allocation Formats We will look at some variations or hybrids of these memory allocations including: –Trees –Graphs –Deques (linked arrays) –inodes 2.2.2

14 Data Structures and Algorithms Trees The linked list can be used to store linearly ordered data –What if we have multiple next pointers? A rooted tree is similar to a linked list but with multiple next pointers

15 Data Structures and Algorithms Trees A tree is a variation on a linked list: –Each node points to an arbitrary number of subsequent nodes –Useful for storing hierarchical data –We will see that it is also useful for storing sorted data –Usually we will restrict ourselves to trees where each node points to at most two other nodes

16 Data Structures and Algorithms Graphs Suppose we allow arbitrary relations between any two objects in a container –Given n objects, there are n 2 – n possible relations If we allow symmetry, this reduces to –For example, consider the network

17 Data Structures and Algorithms Arrays Suppose we allow arbitrary relations between any two objects in a container –We could represent this using a two-dimensional array –In this case, the matrix is symmetric ABCDEFGHIJKL A××× B××××× C×××××× D××× E×× F×× G××× H××× I×× J××× K××× L×××

18 Data Structures and Algorithms Array of Linked Lists Suppose we allow arbitrary relations between any two objects in a container –Alternatively, we could use a hybrid: an array of linked lists A B C D E F G H I J K L

19 Data Structures and Algorithms Linked Arrays Other hybrids are linked lists of arrays –Something like this is used for the C++ STL deque container For example, the alphabet could be stored either as: –An array of 26 entries, or –A linked list of arrays of 8 entries

20 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files –The first twelve entries can reference the first twelve blocks (48 KiB)

21 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files –The next entry is a pointer to an array that stores the next 1024 blocks This stores files up to 4 MiB on a 32-bit computer

22 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files –The next entry has two levels of indirection for files up to 4 GiB

23 Data Structures and Algorithms Hybrid data structures The Unix inode was used to store information about large files –The last entry has three levels of indirection for files up to 4 TiB

24 Data Structures and Algorithms Algorithm run times Once we have chosen a data structure to store both the objects and the relationships, we must implement the queries or operations as algorithms –The Abstract Data Type will be implemented as a class –The data structure will be defined by the member variables –The member functions will implement the algorithms The question is, how do we determine the efficiency of the algorithms? 2.2.3

25 Data Structures and Algorithms Operations We will use the following matrix to describe operations at the locations within the structure Front/ 1 st Arbitrary Location Back/ n th Find??? Insert??? Erase??? 2.2.3

26 Data Structures and Algorithms Operations on Sorted Lists Given an sorted array, we have the following run times: Front/ 1 st Arbitrary Location Back/ n th FindGoodOkayGood InsertBad Good * Bad EraseBad Good * only if the array is not full

27 Data Structures and Algorithms Operations on Lists If the array is not sorted, only one operations changes: Front/ 1 st Arbitrary Location Back/ n th FindGoodBadGood InsertBad Good * Bad EraseBad Good * only if the array is not full

28 Data Structures and Algorithms Operations on Lists However, for a singly linked list where we a head and tail pointer, we have: Front/ 1 st Arbitrary Location Back/ n th FindGoodBadGood InsertGoodBadGood EraseGoodBad

29 Data Structures and Algorithms Operations on Lists If we have a pointer to the k th entry, we can insert or erase at that location quite easily –Note, this requires a little bit of trickery: we must modify the value stored in the k th node Front/ 1 st Arbitrary Location Back/ n th FindGoodBadGood InsertGood EraseGood Bad

30 Data Structures and Algorithms Operations on Lists For a doubly linked list, one operation becomes more efficient: Front/ 1 st Arbitrary Location Back/ n th FindGoodBadGood InsertGoodBadGood EraseGoodBadGood

31 Data Structures and Algorithms Summary In this topic, we have introduced the concept of data structures –We discussed contiguous, linked, and indexed allocation –We looked at arrays and linked lists –We considered Trees Two-dimensional arrays Hybrid data structures –We considered the run time of the algorithms required to perform various queries and operations on specific data structures: Arrays and linked lists