1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CHP-5 LinkedList.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Summary of lectures (1 to 11)
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.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Linked List by Chapter 5 Linked List by
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
CSCE 210 Data Structures and Algorithms
Stack ADT (Abstract Data Type) N …
Lecture 6 of Computer Science II
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Chapter 4 The easy stuff.
CHP - 9 File Structures.
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
CE 221 Data Structures and Algorithms
More Linking Up with Linked Lists
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
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.
Data Structures Lectures: Haim Kaplan, Uri Zwick Exam: 80%
Linked Lists Chapter 4.
Problem Understanding
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
File Organization.
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Lecture 3 – Data collection List ADT
Problem Understanding
Presentation transcript:

1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested Remove a chunk off the list when it is allocated. Put a chunk back in the list when it is deallocated Example 2: A simple text editor Maintain a sequence of lines Example 3: Graph representation (e.g. network) For each vertex, maintain a list of its neighbors.

2 Data Organization All these examples have a common organizational model: A sequence of similar items (memory blocks, text lines, vertices) Certain desired operations find, insert, delete

3 Data organization Major components: The data The operations allowed on it These make up an Abstract Data Type A Data Structure is a construct that implements a particular ADT.

4 The List ADT Data: a collection of homogeneous elements arranged in a sequence Operations: Insert Delete Find Update Retrieve Length

5 The List ADT: Operations General access Which element? Idea 1: Specify an index The user will be able to give an index and perform an operation on the element at that index. Idea 2: Use a pointer to the current location The user is only given access to the element where the current pointer points. What additional operations will be needed to support this?

6 The List ADT: Operations General access Which element? Idea 1: Specify an index The user will be able to give an index and perform an operation on the element at that index. Idea 2: Use a pointer to the current location The user is only given access to the element where the current pointer points. What additional operations will be needed to support this? At the very least, an Increment and Decrement operation to move the current pointer to a new location.

7 The List ADT: Operations Retrieve Which element? Specify an index : Retrieve(i) The user will be able to give an index and retrieve the element at that index. Use a pointer to the current location : Retrieve() The user will have to move the current pointer to the requested element and then retrieve it.

8 The List ADT: Operations Insert Where? Specify an index : Insert(i, value) Insert an element at index i. The existing element at that index will move to i+1 Use a pointer to the current location : Insert(value) Insert an element at the current location. The user will need to move the pointer to that location first. What complications arise in this case?

9 The List ADT: Operations Insert Where? Specify an index : Insert(i, value) Insert an element at index i. The existing element at that index will move to i+1 Use a pointer to the current location : Insert(value) Insert an element at the current location. The user will need to move the pointer to that location first. Inserting an element at the end is problematic: how do we move the current pointer to a location that doesn't exist? We'll need some sort of "dummy" element at the end of the list. But this introduces more complications!

10 The List ADT: Operations Insert Where? Specify an index : Insert(i, value) Insert an element at index i. The existing element at that index will move to i+1 Use a pointer to the current location : Insert(value) Insert an element at the current location. The user will need to move the pointer to that location first. Inserting an element at the end is problematic: how do we move the current pointer to a location that doesn't exist? We'll need some sort of "dummy" element at the end of the list. But this introduces more complications! The Retrieve operation is not defined for that dummy element.

11 The List ADT: Operations Insert Where? Use a pointer to the current location : Insert(value) Insert an element at the current location. The user will need to move the pointer to that location first. Inserting an element at the end is problematic: how do we move the current pointer to a location that doesn't exist? Idea: "Dummy" element at the end of the list. Alternative 1 : provide a special InsertAtEnd() operation. Is this a good idea? Alternative 2: provide InsertBefore(), InsertAfter(). Is this a good idea?

12 The List Data Structure Implementation 1 : Contiguous memory Use a dynamic array with a current pointer How is each operation implemented? How efficient is each operation? Random access capability good for retrieval when we use index rather than current pointer. Important: the list ADT does NOT provide random access. We just use it internally to speed up retrieval. We will need to shift elements every time we insert or delete. In addition, if the array fills up, we need to reallocate a bigger chunk of memory.

13 The List Data Structure Implementation 2 : Singly-linked memory Use a node structure to store the data and a pointer to the next node, to create a chain of nodes. Use a current pointer Uses more space than the array (due to the pointers) but insert/delete do not require shifting. However, insert / delete require us to traverse the whole list in order to access the predecessor of the current node. Can we avoid this?

14 The List Data Structure Implementation 2 : Singly-linked memory insert / delete require us to traverse the whole list in order to access the predecessor of the current node. Trick solution (works for deleting any node but the last): move the next node's contents into the one to be deleted and then physically remove the next node. This maintains the correct abstract picture of the structure. We can use a similar trick for the insert operation. Using this method makes insert/delete take constant time in all cases except when inserting/deleting the last node. That takes linear time.

15 Other list flavors Doubly-linked list Each node has a pointer to its successor and its predecessor. Faster insert/delete, but more space. Circular list The last node points back to the head (or the array wraps around). Sorted list Items stored in sorted order. Which implementation provides faster operations? Array or linked memory?

16 Other list flavors XOR list A space saving list Instead of both a previous and next pointer, store the XOR of the predecessor and successor. Node B stores &A XOR &C If you are at B and know the address of A, you can compute the address of C. The list can be traversed in any direction, provided you know where you came from. Interesting, but not very useful...

17 Other list flavors Unrolled linked list A space saving list Store k data items in each node It's a list of arrays Reduces cache misses Each node should be at least half-full If a node is too empty after a delete, merge it with a neighbor. If a node overflows after an insert, split it.

18 Implementing our ideas template class LinkedList { private: class Node { public: Node *next; Node *prev; T item; Node(); Node(T element, Node *nextcell = NULL, Node *prevcell = NULL); ~Node(); }; Node *head; Node *current; public:... };