CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 10. 150209. CARRANO CHAPT. 9.

Slides:



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

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Data Structures ADT List
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CSE Lecture 12 – Linked Lists …
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures: A Pseudocode Approach with C
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
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.
Chapter 4 Link Based Implementations
Link Based Implementations
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Dr. Mohamed Khafagy.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Link Based Implementations
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
Arrays and Linked Lists
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
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.
Review & Lab assignments
Indirection.
Chapter 4 Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 9 Linked Lists.
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9

Agenda HW3 Questions Linked Lists Midterm Prep

Review from last time… Built a Push/Pop Stack using a linked list Determined semantics of a stack Determined that overloading of = required, also copy constructor, and destructor

Copy Constructor Can be implemented with call to default constructor and then assignment MyObj o1 = o2 Pass by Value Return by Value

Array v. Linked List for Stack Arrays easy to use, but have fixed size Increasing size of dynamically allocated array can waste storage, time Array based implementation good for small bag Linked chains do not have fixed size

Sorted singly linked list

Sorted list using linked list impl Singly linked list remains the same structure as the stack but need to implement Insert Remove

insert( int x ) Node Insertion Find node to insert after: current, Node *insNode = new Node; insNode->val = x; insNode->next = current->next; current->next = insNode; header valnextvalnext NULL valnext current new Value next insNode X Three cases for finding node to insert after 1) Empty List 2) First in line 3) All others Some recommend a dummy front node to collapse 2 and 3

bool IntSortedList::Insert(int val) { Node *insNode = new Node; insNode->value = val; if (head == NULL) { head = insNode; return true; } if (val value) { insNode->next = head; head = insNode; return true; } Node *pNode = head; while ((pNode->next != NULL) && ((pNode->next)->value < val)) { pNode = pNode->next; } insNode->next = pNode->next; pNode->next = insNode; return true; } Insert

Dangling References: common causes A pointer which is initialized but not set to NULL Delete or free is called and pointer is not set to NULL Aliasing of pointers which are not updated in tandem

Computer Scientist of the week Ada Lovelace Aka, Augustus Ada Byron, aka Augustus Ada Lovelace, aka the Countess of Lovelace Daughter of the famous Poet, Lord Byron Partnered with Charles Babbage on Analytical Engine Wrote first computer program! Algorithm to be executed by a machine Computer Bernoulli numbers Also contributed to mathematics and phrenology

remove( int ) Stop the current pointer at the node previous to a deleted node ◦First node is special case or use a dummy first node Memorize the node to remove Node *dNode = current->next; Remove this node current->next = current->next->next; Deallocating the node delete deletedNode; header current Node to be deleted

Remove

List can also be implemented with an array Easy implementation Fixed list size Necessity to shift data down to an extended space upon an insertion and to shift data up upon a deletion list[0] list[1] list[3] list[2] list[4] list[n] list[n-1]

Circular Linked Lists Linearly linked lists: Circular linked lists: Traverse in circular linked lists: Node *first = head; Node *cur = head->next; while ( cur != first ) { display(cur->item); cur = cur->next; }; 4551 NULL head 4551 head dummy

Doubly Linked Lists NULL NULL head NULL head Single Linked Lists To be deleted Doubly Linked Lists cur->prev->next = cur->next But pointer operations become more complicated.

Inserting a Node before the current pointer newPtr->next = cur;// (a) newPrt->prev = cur->prev;// (b) cur->prev = newPtr;// (c) newPtr->prev->next = newPtr;// (d) cur (a) (b)(c) (d)