of 41 /41

Author: ananda

Post on 15-Feb-2016

139 views

Category:

## Documents

Tags:

• #### linked list of integers

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

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.

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.

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.

11Expand the .zip file

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

13Select Destination for Expanded File

14Extraction Complete

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

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;

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;

IntDLList::~IntDLList(void){}Build

Build

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