# Data Structures Intro/Linked List Review CIS 237 – Data Structures.

## Presentation on theme: "Data Structures Intro/Linked List Review CIS 237 – Data Structures."— Presentation transcript:

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

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 + 1 3 write page heading 3 end if 4 write report line 5lines = lines + 1 4 end loop 5 close file 6 return lines

Kinds of Data Atomic Data –integer –floating point –character Data Structure –arrays –classes 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

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

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

Keyed List Data Structure applegrapepeachpear 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 Algorithm Algorithm orderedInsert (val dataIn data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr 6. if create fails return false 3. end if 4. count = count + 1 5. return true

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 -1 11.return true

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

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

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

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

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

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 next 3. while (p is not Null and dataIn is greater than p->data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr 6. if create fails return false 7. count = count + 1 8. return true

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 header Algorithm orderedInsert (val dataIn next 3. while (p is not Null and dataIn is greater than p->data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr and trailp as prev ptr 6.if create fails return false 7.if trailp is the rear//either new rear or must set prev pointer of after node 1.rear is now trailp->next 8.else 1.p->prev = trailp->next 9.end if 10. count = count + 1 11. return true

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

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

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