linked lists ii doubly linked lists

of 41 /41
1 Linked Lists II Doubly Linked Lists Chapter 3

Author: ananda

Post on 15-Feb-2016

139 views

Category:

Documents


2 download

Embed Size (px)

DESCRIPTION

Linked Lists II Doubly Linked Lists. Chapter 3. Objectives. You will be able to: Describe, implement, and use a Doubly Linked List of integers. Some Issues. Issues in current implementation: Inserting and deleting at either end of the list requires special case code. - PowerPoint PPT Presentation

TRANSCRIPT

Doubly Linked Lists

1Linked Lists IIDoubly Linked ListsChapter 32ObjectivesYou will be able to:Describe, implement, and use a Doubly Linked List of integers.

3Some IssuesIssues 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 inefficientRequires scanning entire list.

Solution: Doubly linked list with node as list head. (Not in book!)

4Some Other IssuesIssues 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.

5Each 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.96Doubly Linked List with List Head NodeWe 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 ListIf List is empty, both point to the List Head.

7Doubly Linked List with List Head NodeWith 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!8DownloadDownload example from last class.

http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_01_12_Singly_Linked_List/ File intSLLst.zip9Download example from last class

10Download example from last class

11Expand the .zip file

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

13Select Destination for Expanded File

14Extraction Complete

15Downloaded Folder Expanded

Drill down.16The Solution Folder

Double click the .sln file to open the solution in Visual Studio17Open in Visual Studio

Rebuild solution and test.Be sure we have a known starting point.18Program in Action

Now let's modify the list to be a doubly linked list with Listhead Node.19Add Class intDLList

20Add Class intDLList

21Add Class intDLList

22Defining Class intDLListCopy intSLLst.h as initial version of intDLList.h.

Update class names and guard.Add prev pointer to Node class.

23intDLList.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.hclass 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;};

25main.cppUpdate list class.

Comment out most of bodySo that we can compile before adding all methods to IntDLList.cpp26main.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 next = ListHead; ListHead->prev = ListHead;}

IntDLList::~IntDLList(void){}Build

29addToHeadvoid IntDLList::addToHead(int el) { IntDLLNode *p = new IntDLLNode(el);

p->next = ListHead->next; p->prev = ListHead;

ListHead->next->prev = p; ListHead->next = p;}

Build

30addToTailvoid IntDLList::addToTail(int el) { IntDLLNode *p = new IntDLLNode(el);

p->next = ListHead; p->prev = ListHead->prev;

ListHead->prev->next = p; ListHead->prev = p;}31deleteFromHeadint 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;}32deleteFromTailint 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;}

33deleteNode// Delete node with a specified valuevoid 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;}34isInListbool IntDLList::isInList(int el) const { IntDLLNode *p = ListHead->next; while (p != ListHead && p->info != el) { p = p->next; }

return p != ListHead;}35printAllvoid IntDLList::printAll() const { if (isEmpty()) { cout next; while (p != ListHead) { cout info next; } cout