CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.

Slides:



Advertisements
Similar presentations
Linked Lists Mohammed Almashat CS /03/2006.
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Stacks, Queues, and Linked Lists
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
Linked Lists Linked Lists Representation Traversing a Linked List
CHP-5 LinkedList.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Chapter 17 Linked List.
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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked List by Chapter 5 Linked List by
Subject Name : Data Structure Using C Title : Linked Lists
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
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.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structure & Algorithms
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.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
UNIT-II Topics to be covered Singly linked list Circular linked list
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.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
Stack and Queue APURBO DATTA.
Introduction to C++ Recursion
Arrays and 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.
Queues A first-in, first-out or FIFO data structure.
Linked Lists in C and C++
LINKED LISTS.
Introduction to C++ Linear Linked Lists
Stacks and Queues.
Linked Lists.
Presentation transcript:

CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions

CS162 - Deallocating all The purpose of the destructor is to –perform any operations necessary to clean up memory and resources used for an object’s whose lifetime is over –this means that when a linked list is managed by the class that the destructor should deallocate all nodes in the linear linked list –delete head won’t do it!!!

CS162 - Deallocating all So, what is wrong with the following: list::~list() { while (head) { delete head; head = head->next; } –We want head to be NULL at the end, so that is not one of the problems –We are accessing memory that has been deallocated. Poor programming!

CS162 - Deallocating all The answer requires the use of a temporary pointer, to save the pointer to the next node to be deleted prior to deleting the current node: list::~list() { node * current; while (head) { current = head->next; delete [] head->data->title; delete head->data; delete head; head = current; }

CS162 - Insert in Order Next, let’s insert nodes in sorted order. Like deleting the last node in a LLL, –we need to keep a pointer to the previous node in addition to the current node –this is necessary to re-attach the nodes to include the inserted node. So, what special cases need to be considered to insert in sorted order?

CS162 - Insert in Order Special Cases: –Empty List inserting as the head of the list –Insert at head data being inserted is less than the previous head of the list –Insert elsewhere

CS162 - Insert in Order Empty List –if head is null, then we are adding the first node to the list: head Before 1ST head After if (!head) { head = new node; head->data =... head->next = 0; }

CS162 - Insert in Order Inserting at the Head of the List –if head is not null but the data being inserted is less than the first node 1STNTH head Before new node head After NTH current 1ST bz a bz

CS162 - Insert in Order Here is the “insert elsewhere” case: 1STNTH head Before 1ST new node head After NTH currentprevious

CS162 - Special Cases When inserting in the middle –can the same algorithm and code be used to add at the end (if the data being added is “larger” than all data existing in the sorted list)? –Yes?No?

CS162 - In Class, Work thru: Any questions on how to: –insert (at beginning, middle, end) –remove (at beginning middle, end) –remove all Next, let’s examine how similar/different this is for –circular linked lists –doubly linked lists

CS162 - In Class, Work thru: For circular linked lists: –insert the very first node into a Circular L L (i.e., into an empty list) –insert a node at the end of a Circular L L –remove the first node from a Circular L L –Walk through the answers in class or as an assignment (depending on time available)

CS162 - In Class, Work thru: For doubly linked lists: –write the node definition for a double linked list –insert the very first node into a Doubly L L (i.e., into an empty list) –insert a node at the end of a Doubly L L –remove the first node from a Doubly L L –Walk through the answers in class or as an assignment (depending on time available)

CS162 - What if... What if our node structure was a class –and that class had a destructor –how would this change (or could change) the list class’ (or stack or queue class’) destructor? //Discuss the pros/cons of the following design.... class node { public: node(); node(const video &); ~node(); private: video * data; node * next; };

CS162 - What if... OK, so what if the node’s destructor was: node::~node() { delete [] data->title; delete data; delete next; }; list::~list() { delete head; //yep, this is not a typo } –This is a “recursive” function.... (a preview of our Recursion Lecture; saying delete next causes the destructor to be implicitly invoked. This process ends when the next ptr of the last node is null.)

CS162 - For Next Time Practice Linked lists Do the following: –Examine how the insert and remove functions you have written would change if you had a tail pointer in addition to a head pointer Any Questions on Stage #3?