1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
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.
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
CS 367 – Introduction to Data Structures
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSCI2100B Linked List Jeffrey
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Binary Search Trees Chapter 6.
Chapter 17 Linked List.
1 Binary Search Trees III Delete Chapter 6. 2 Objectives You will be able to write code to delete a node from a Binary Search Tree.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Classes and Data Structures Jeffrey S. Childs
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
An Example of Windows Forms Applications Windows-based application –Win Forms Control structures (selection and repetition) Graphics Read integers from.
1 Writing a Good Program 8. Elementary Data Structure.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
CSCE 210 Data Structures and Algorithms
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
LAB#4#5 Linked List.
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 4 Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
LAB#4 Linked List.
Data Structures & Programming
Presentation transcript:

1 Linked Lists II Doubly Linked Lists Chapter 3

2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.

3 Some Issues Issues in current implementation: Inserting and deleting at either end of the list requires special case code. Empty list requires special case code. First node and last node are different from other nodes. deleteFromTail is inefficient Requires scanning entire list. Solution: Doubly linked list with node as list head. (Not in book!)

4 Some Other Issues Issues in current implementation: No way to retrieve a specified node. No way to insert in order. No way to traverse the list. Solution: Add new methods.

5 Each node has a pointer to the next node and a pointer to the previous node. Section 3.2 in the textbook. Version in the book still has pointers to the head and tail as members of the list class. Requires special case code for adding and deleting at the head and the tail. Doubly Linked List Figure 3.9

6 Doubly Linked List with List Head Node We can avoid all special case code by using a Node object as a List Head. Next pointer points to first element of List. Prev pointer points to last element of List If List is empty, both point to the List Head.

7 Doubly Linked List with List Head Node With a Node as List Head, all nodes in the list are alike. Each node has a Node as next. Each node has a Node as prev. No pointer is ever null. An empty list is not a special case. There is still a List Head Node, with next and prev pointers. The same Insert and Delete code works for all cases!

8 Download Download example from last class _01_12_Singly_Linked_List/ _01_12_Singly_Linked_List/ File intSLLst.zip

9 Download example from last class

10 Download example from last class

11 Expand the.zip file Right click the.zip file icon.

12 Expand the.zip file

13 Select Destination for Expanded File

14 Extraction Complete

15 Downloaded Folder Expanded Drill down.

16 The Solution Folder Double click the.sln file to open the solution in Visual Studio

17 Open in Visual Studio Rebuild solution and test. Be sure we have a known starting point.

18 Program in Action Now let's modify the list to be a doubly linked list with Listhead Node.

19 Add Class intDLList

20 Add Class intDLList

21 Add Class intDLList

22 Defining Class intDLList Copy intSLLst.h as initial version of intDLList.h. Update class names and guard. Add prev pointer to Node class.

23 intDLList.h #pragma once //************************ intDLList.h *************** // Doubly-linked list class to store integers class IntDLLNode { public: int info; class IntDLLNode *next; class IntDLLNode *prev; IntDLLNode(int el, IntDLLNode *ptr1 = 0, IntDLLNode *ptr2 = 0) { info = el; next = ptr1; prev = ptr2; } };

intDLList.h class IntDLList { public: IntDLList(); ~IntDLList(); int isEmpty() const { return ListHead->next == ListHead; } void addToHead(int); void addToTail(int); int deleteFromHead(); // delete the head and return its info; int deleteFromTail(); // delete the tail and return its info; void deleteNode(int); bool isInList(int) const; void printAll() const; private: IntDLLNode* ListHead; };

25 main.cpp Update list class. Comment out most of body So that we can compile before adding all methods to IntDLList.cpp

26 main.cpp #include #include "intDLList.h" using namespace std; int main() { IntDLList myList; //myList.addToHead(3); //myList.addToHead(2); //myList.addToHead(1); //myList.addToHead(0); //cout << "Initial List:\n"; //myList.printAll(); //while (!myList.isEmpty()) //{... //} cin.get(); // Hold window open. }

27 Implementing Class intDLList Start by adding constructor and destructor to intDLList.cpp Add remaining methods one at a time. Build after adding each method

28 Constructor //************************ intDLList.cpp ************* #include #include "intDLList.h" using namespace std; IntDLList::IntDLList() { ListHead = new IntDLLNode(0); ListHead->next = ListHead; ListHead->prev = ListHead; } IntDLList::~IntDLList(void) { } Build

29 addToHead void IntDLList::addToHead(int el) { IntDLLNode *p = new IntDLLNode(el); p->next = ListHead->next; p->prev = ListHead; ListHead->next->prev = p; ListHead->next = p; } Build

30 addToTail void IntDLList::addToTail(int el) { IntDLLNode *p = new IntDLLNode(el); p->next = ListHead; p->prev = ListHead->prev; ListHead->prev->next = p; ListHead->prev = p; }

31 deleteFromHead int IntDLList::deleteFromHead() { if (isEmpty()) { throw("Attempt to delete from empty list"); } int el = ListHead->next->info; IntDLLNode *tmp = ListHead->next; ListHead->next->next->prev = ListHead; ListHead->next = ListHead->next->next; delete tmp; return el; }

32 deleteFromTail int IntDLList::deleteFromTail() { if (isEmpty()) { throw("Attempt to delete from empty list"); } int el = ListHead->prev->info; IntDLLNode *tmp = ListHead->prev; tmp->prev->next = ListHead; ListHead->prev = tmp->prev; delete tmp; return el; }

33 deleteNode // Delete node with a specified value void IntDLList::deleteNode(int el) { IntDLLNode *p = ListHead->next; while (p != ListHead && p->info != el) { p = p->next; } if (p == ListHead) { return; // It's not there. Claim success! } // p points to node to be deleted. p->prev->next = p->next; p->next->prev = p->prev; delete p; }

34 isInList bool IntDLList::isInList(int el) const { IntDLLNode *p = ListHead->next; while (p != ListHead && p->info != el) { p = p->next; } return p != ListHead; }

35 printAll void IntDLList::printAll() const { if (isEmpty()) { cout << "List is empty\n"; return; } IntDLLNode *p = ListHead->next; while (p != ListHead) { cout info << " "; p = p->next; } cout << endl; }

36 Destructor Add following the constructor: IntDLList::~IntDLList(void) { while (!isEmpty()) { deleteFromTail(); } delete ListHead; }

main.cpp #include #include "intDLList.h" using namespace std; int main() { IntDLList myList; myList.addToHead(3); myList.addToHead(2); myList.addToHead(1); myList.addToHead(0); cout << "Initial List:\n"; myList.printAll(); while (!myList.isEmpty()) { cout << "Deleting head node\n"; myList.deleteFromHead(); cout << "Current List:\n"; myList.printAll(); } cin.get(); // Hold window open. }

38 Program Running

39 Check Error Handling cout << "\nCalling deleteFromHead for empty list\n"; cout << "This should output an error message\n\n"; try { myList.deleteFromHead(); } catch (const char* err) { cout << err << endl; } cout << "\nTest complete\n";

40 Program Running

41 Test Thoroughly We need to test the class code much more thoroughly. Call every method. Set up potential special cases. Empty List Exactly one node. Multiple nodes