data structures intro/linked list review cis 237 – data structures

Post on 30-Mar-2015

238 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Data StructuresIntro/Linked List Review

CIS 237 – Data Structures

PseudocodeAlgorithm sample (ref pageNumber <integer>)This algorithm reads a file and prints a report.

Pre pageNumber must be initializedPost Report printed. pageNumber contains number of

pages in the report.Return number of lines printed.

1 Open file2 lines = 03 loop (not end of file) 1 read file 2 if (full page) 1 form feed

2 pageNumber = pageNumber + 13 write page heading

3 end if 4 write report line 5 lines = lines + 14 end loop5 close file6 return lines

Kinds of Data• Atomic Data

– integer

– floating point

– character

• Data Structure

– arrays

– classes

• Abstract Data Type

Abstract Data Type

• Characteristics– Declaration of data– Declaration of operations– Encapsulation of data and operations

• Abstraction

• Encapsulation

Designing Abstract Data Types

• classes/templates

• data

• member functions

• the “Big-3”

• overloading operators

ListsLinear Lists

General Restricted

Unordered

Ordered FIFO (queue)

LIFO (stack)

Linked Lists

• Component Type– Must work with operators used on

them

• Nodes

• The list

Linked Lists - Nodes

data

link

apple grape peach pear

Linked List - ADT

apple grape peach pear

head

count 4

head

count 0

Keyed List Data Structure

apple grape peach pear

head

count 4

• Simple List Operations– Empty– Create– Full*

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

orderedInsert AlgorithmAlgorithm orderedInsert (val dataIn <dataType)

Inserts data into a new node in the linked list so the list is in ascending orderPre dataIn contains the data to be insertedPost data is inserted in sequenceReturn true if successful, false otherwise1. if list empty or dataIn < data at the head //inserting at head of list

• Create a new head with dataIn• If create fails return false

2. else

1. trailp = Null2. p = head3. while (p is not Null and dataIn is greater than p->data)

1. trailp = p2. p = p->next

4. end while5. trailp->next = a new node with data in as data and p as its next ptr6. if create fails return false

3. end if

4. count = count + 15. return true

Remove AlgorithmAlgorithm remove( dataout<dataType>)Deletes data from a linked list and returns success or failurePre dataOut is the dataPost node with data removed from listReturn true if found and removed, false if not there1. p = head2. trailp = Null3. while p is not Null and p->data not dataout

1. trailp = p2. p = p->next

4. end while5. if p is Null return false //not in list6. if p is head

1. head = head->next7. else

1. trailp->next = p->next8. end if9. delete p10. count = count -111. return true

Find Algorithm

Algorithm findList(target<dataType>)

Searches for the target and return success or failure

Pre target is the item with the value being sought

Return true 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

Destroy the list starting at LAlgorithm destroy(ref node L)

Deletes all the nodes in the list starting at L

Pre L contains the node at which to start destroying

Post nodes from L to end gone

1. while L is not null

1. doomed = L

2. L = L ->next

3. delete doomed

2. end while

AssignmentAlgorithm operator=(const ref rightList)

Makes a deep copy of the list from rightList to the current instance

Pre rightList is an initialized list

Return returns the current instance1. If (the current instance and the rightList are not the same)

1. destroy the current instance2. head is assigned the copy of the rightList3. count = rightList->count

2. end if3. return *this

Copy A ListAlgorithm copy (pointer Node L)

Makes a copy from the list that begins with L, does a deep copy

Pre L 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

Operator <<

Algorithm operator<<(ostream &out, list l)

Prints each data item to out using an iterator. Data type must have << operator overloaded

Pre out 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

Dummy Header

grape peach pear

head

count 3

head

count 0

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

Sample - orderInsert

Algorithm orderedInsert (val dataIn <dataType)

Inserts data into a new node in the linked list so the list is in ascending orderPre dataIn contains the data to be insertedPost data is inserted in sequenceReturn true if successful, false otherwise1. trailp = head2. p = head->next3. while (p is not Null and dataIn is greater than p->data)

1. trailp = p2. p = p->next

4. end while5. trailp->next = a new node with data in as data and p as its next ptr6. if create fails return false

7. count = count + 18. return true

Doubly Linked List

grape peach pear

head

count 3

rear

count

head

0

rear

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

Ascending Insert - doubly linked/dummy headerAlgorithm orderedInsert (val dataIn <dataType)

Inserts data into a new node in the linked list so the list is in ascending orderPre dataIn contains the data to be insertedPost data is inserted in sequenceReturn true if successful, false otherwise1. trailp = head2. p = head->next3. while (p is not Null and dataIn is greater than p->data)

1. trailp = p2. p = p->next

4. end while5. trailp->next = a new node with data in as data and p as its next ptr and trailp as prev ptr6. if create fails return false7. if trailp is the rear //either new rear or must set prev pointer of after node

1. rear is now trailp->next8. else

1. p->prev = trailp->next9. end if

10. count = count + 111. return true

Remove - doubly linked/dummy headerAlgorithm remove( dataout<dataType>)

Deletes data from a linked list and returns success or failurePre dataOut is the dataPost node with data removed from listReturn true if found and removed, false if not there1. p = head->next2. trailp = head3. while p is not Null and p->data not dataout

1. trailp = p2. p = p->next

4. end while5. if p is Null return false //not in list6. trailp->next = p->next7. if p is rear //if deleting last node reset rear else set next node’s prev

ptr1. rear = trailp

8. else1. trailp->next->prev = trailp

9. end if10. delete p11. return true

Reverse Iterator• Parts

– Curr– Itr the list being iterated

• Last

• More

– Stops at itr.head not NULL

• Back

• Value

Complex Component Types

• Use any type

• Should have relational operators overloaded

• << overloaded if using overloaded << in

application

• Constructor in node/list

• Find

Circular List with a Dummy Header

grape peach pear

head

count 3

head

count 0

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

top related