Download presentation

Presentation is loading. Please wait.

Published byBrooklyn Sagraves Modified over 2 years ago

1
Data Structures Intro/Linked List Review CIS 237 – Data Structures

2
Pseudocode Algorithm sample (ref pageNumber ) This algorithm reads a file and prints a report. PrepageNumber must be initialized PostReport printed. pageNumber contains number of pages in the report. Return number of lines printed. 1 Open file 2 lines = 0 3 loop (not end of file) 1read file 2if (full page) 1 form feed 2 pageNumber = pageNumber write page heading 3 end if 4 write report line 5lines = lines end loop 5 close file 6 return lines

3
Kinds of Data Atomic Data –integer –floating point –character Data Structure –arrays –classes Abstract Data Type

4
Characteristics –Declaration of data –Declaration of operations –Encapsulation of data and operations Abstraction Encapsulation

5
Designing Abstract Data Types classes/templates data member functions the Big-3 overloading operators

6
Lists Linear Lists GeneralRestricted UnorderedOrderedFIFO (queue) LIFO (stack)

7
Linked Lists Component Type –Must work with operators used on them Nodes The list

8
Linked Lists - Nodes datalink applegrapepeachpear

9
Linked List - ADT applegrapepeachpear head count 4 head count 0

10
Keyed List Data Structure applegrapepeachpear head count 4 Simple List Operations –Empty –Create –Full*

11
Other List Operations Size Ordered Insert (Ascending) Remove a Value Find a Value in the List Print List (overload of the operator << ) The Destructor and Destroy = and the Copy Constructor and Copy

12
orderedInsert Algorithm Algorithm orderedInsert (val dataIn

13
Remove Algorithm Algorithm remove( dataout ) Deletes data from a linked list and returns success or failure PredataOut is the data Postnode with data removed from list Return true if found and removed, false if not there 1.p = head 2.trailp = Null 3.while p is not Null and p->data not dataout 1.trailp = p 2.p = p->next 4.end while 5.if p is Null return false //not in list 6.if p is head 1.head = head->next 7.else 1.trailp->next = p->next 8.end if 9.delete p 10.count = count return true

14
Find Algorithm Algorithm findList(target ) Searches for the target and return success or failure Pretarget is the item with the value being sought Returntrue if found, false otherwise 1.p = head 2.while p is not Null and p->data is not target 1.p = p->next 3.end while 4.if p is not Null and p->data equals target 1.return true 5.else 1.return false 6.end if

15
Destroy the list starting at L Algorithm destroy(ref node L) Deletes all the nodes in the list starting at L Pre L contains the node at which to start destroying Postnodes from L to end gone 1.while L is not null 1.doomed = L 2.L = L ->next 3.delete doomed 2.end while

16
Assignment Algorithm operator=(const ref rightList) Makes a deep copy of the list from rightList to the current instance PrerightList is an initialized list Returnreturns the current instance 1. If (the current instance and the rightList are not the same) 1. destroy the current instance 2. head is assigned the copy of the rightList 3. count = rightList->count 2. end if 3. return *this

17
Copy A List Algorithm copy (pointer Node L) Makes a copy from the list that begins with L, does a deep copy PreL is a node at which to start copying Post current instance contains a copy of the list 1.first = null 2.last = null 3.if L is not null 1.first = last = a new node with L->data as its data 2.for p = L->next; p not null; p = p->next, last=last->next 1.last->next = new node with p->data as data 3.end for 4.return first 4.end if

18
Operator << Algorithm operator<<(ostream &out, list l) Prints each data item to out using an iterator. Data type must have << operator overloaded Preout must contain an output stream and a list Post all values in list will be printed out 1.for itr.start(); itr.move(); itr.next() 1.out << itr.value() << 2.end for

19
Dummy Header grapepeachpear head count 3 head count 0

20
Dummy Header Changes Constructor must create dummy node Traversals begin at head->next (head for trailer) Empty must check head->next No special cases –orderedInsert –remove

21
Sample - orderInsert Algorithm orderedInsert (val dataIn

22
Doubly Linked List grape peachpear head count 3 rear count head 0 rear

23
Changes for Doubly Linked List Node –Must now have a previous pointer List –Has rear –Constructor must now initialize rear Copy –Must create node with prev pointer –Must return the last node copied –Calls to copy must then include rear

24
Ascending Insert - doubly linked/dummy header Algorithm orderedInsert (val dataIn

25
Remove - doubly linked/dummy header Algorithm remove( dataout ) Deletes data from a linked list and returns success or failure PredataOut is the data Postnode with data removed from list Return true if found and removed, false if not there 1.p = head->next 2.trailp = head 3.while p is not Null and p->data not dataout 1.trailp = p 2.p = p->next 4.end while 5.if p is Null return false //not in list 6.trailp->next = p->next 7.if p is rear//if deleting last node reset rear else set next nodes prev ptr 1.rear = trailp 8.else 1.trailp->next->prev = trailp 9.end if 10.delete p 11.return true

26
Reverse Iterator Parts –Curr –Itr the list being iterated Last More –Stops at itr.head not NULL Back Value

27
Complex Component Types Use any type Should have relational operators overloaded << overloaded if using overloaded << in application Constructor in node/list Find

28
Circular List with a Dummy Header grape peachpear head count 3 head count 0

29
Changes For Circular List No need for rear –Reverse iterator start at itr.head->prev Loop control –List operations head not Null –Iterator operations itr.head Constructor set up next and prev Insert/Remove eliminate rear check Copy mark first element

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google