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.

Slides:



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

Linked Lists Mohammed Almashat CS /03/2006.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
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.
LIST PROCESSING.
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 Linked List Position (1). 2 Linked List Position (2)
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.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Information and Computer Sciences University of Hawaii, Manoa
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
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.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
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,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Chapter 4 The easy stuff.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Stack and Queue APURBO DATTA.
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.
Doubly linked lists.
Lists.
Chapter 16-2 Linked Structures
Linked Lists.
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 ]
CMSC 341.
CMSC 341 Skip Lists.
Doubly Linked List Implementation
Review & Lab assignments
Linked Lists Chapter 4.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
CMSC 341.
CMSC 341.
Lecture No.02 Data Structures Dr. Sohail Aslam
CMSC 341 List 2.
Doubly Linked List Implementation
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
Presentation transcript:

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 node

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

4 Doubly-Linked Circular Linked List May or may not have a header Useful for applications requiring fast access to head and tail

5 Vector Implementation template void List :: insert(const Object &x, const ListItr &p) { if (!p.isPastEnd()) { // shift all elements down for (i = this.last; i > p.current+1; i--) nodes[i + 1] = nodes[i]; // put new element in hole nodes[p.current + 1] = x; }

6 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

7 Cursor List Class template 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 ; friend class ListItr ; };

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

9 Cursor Initialization template void List ::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; }

10 cursorSpace

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

12 Cursor Implementation (cont.) template ListItr List :: find(const Object &x) const { int itr = cursorSpace[header].next; while (itr!=0 && cursorSpace[itr].element != x) itr = cursorSpace[itr].next; return ListItr (itr); }

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

14 Cursor Implementation (cont.) template void List :: remove(const Object &x) { ListItr 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); }

15 Comparing Performance