CMSC 341 Lists 3.

Slides:



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

Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Data Structures ADT List
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.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structure Lecture-5
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Data Structures and Algorithms
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
EEL 4854 IT Data Structures Linked Lists
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Doubly linked lists.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Lists.
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
Lists List: finite sequence of data elements
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Sequences 12/8/2018 3:02 AM 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.
CS212D: Data Structures Week 5-6 Linked List.
CMSC 341.
CS2013 Lecture 4 John Hurley Cal State LA.
CMSC 341 Skip Lists.
CMSC 341 Skip Lists.
Doubly Linked List Implementation
Data Structures and Algorithms
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
CMSC 341 Lists 3.
CMSC 341.
Introduction to C++ Linear Linked Lists
Lecture 16 Section 6.2 Thu, Mar 1, 2007
CMSC 341.
CMSC 341 List 2.
CMSC 341 Lecture 7.
Programming II (CS300) Chapter 07: Linked Lists
CMSC 341 Skip Lists.
Doubly Linked List Implementation
Topic 11 Linked Lists -Joel Spolsky
Data Structures & Programming
Presentation transcript:

CMSC 341 Lists 3

Doubly-Linked Lists Option: add pointer to previous node Issues doubles number of pointers allows immediate (O(1)) access to previous node Questions: Is there an advantage for remove? No, since it still has to scan to find element to remove. Does it make sense to have a “retreat” method for IstItr? How come there was no such method for singly-linked? -- Because it would be too expensive -- O(N) per call.

Circular Linked Lists Configurations header in circle header before circle no header

Doubly-Linked Circular Linked List May or may not have a header Useful for applications requiring fast access to head and tail theList->prev is a pointer to the last node. Therefore, access to end of list is O(1). Also, header->prev is last node header->next is first node

Vector Implementation template <class Object> void List<Object>:: insert(const Object &x, const ListItr<Object> &p) { if (!p.isPastEnd()) { for (i=this.last; i > p.current+1; i--) this.nodes[i+1] = this.nodes[i]; this.nodes[p.current+1] = x; }

Cursor Implementation Linked list look&feel data stored as collection of nodes: data and next nodes allocated and deallocated without dynamic memory allocation Basic concepts have node pool of all nodes, keep list of free ones use pool indices in place of pointers; 0 == NULL

Cursor List Class template <class Object> class List { List(); // same old list public interface 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>; };

Cursor List Class (cont.) … private: int header; static vector<CursorNode> cursorSpace; static void initializeCursorSpace(); static int alloc(); static void free (int p); friend class ListItr<Object>; };

Cursor Initialization template <class Object> void List<Object>::initializeCursorSpace() { static int p 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; }

cursorSpace

Cursor Allocation template <class Object> void List<Object>::alloc() { int p = cursorSpace[0].next; cursorSpace[0].next = cursorSpace[p].next; return p; } void List<Object>::free(int p) { cursorSpace[p].next = cursorSpace[0].next; cursorSpace[0].next = p;

Cursor Implementation (cont.) 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); }

Cursor Implementation (cont.) template <class Object> void List<Object>:: insert(const Object &x, const ListItr<Object> &p) { if (!p.isPastEnd()) { int pos = p.current; int tmp = alloc(); cursorSpace[tmp] = CursorNode(x, cursorSpace[pos].next); cursorSpace[pos].next = tmp; }

Cursor Implementation (cont.) 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); }

Comparing Performance Constants: higher for doubly linked list lower for cursor