Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 04: Sequences structures

Similar presentations


Presentation on theme: "Lecture 04: Sequences structures"— Presentation transcript:

1 Lecture 04: Sequences structures
Topics: Problems with arrays Linked Lists basics Linked List operations STL variants of the above Other variants (stacks and queues)

2 Part I: Array problems Goal or arrays and linked structures: Arrays:
Represent an collection of data Arrays: Advantage: quick addressing Recall the pointer math involved Disadvantage: must be contiguous This applies regardless of whether it's on stack or heap Disadvantage: size is fixed Static arrays: fixed at compile time Dynamic arrays: fixed upon new call. We can re-allocate the array, but there is some overhead Linked Structures (for now, linked lists) are the reverse.

3 Part II: Linked structures
Goal: Represent an collection of data Comparisons to arrays: Arrays have this same goal Disadvantage: can’t “jump” to a specific element Instead, we need to traverse the list Advantage: needn’t be contiguous Each element can go to a different spot (on the heap) Advantage: can grow until we run out of memory Once we have the correct spot to insert a new element, adds are very fast. Disadvantage: can lead to heap-fragmentation (the way we’ll implement it)

4 Linked List data structures
class LLNode { // Put the data here (I call it the payload) double mData; // sample payload LLNode * mNext; // Put any delegated (recursive) calls here. }; class LList LLNode * mStart; // put methods here to add, remove, search, // etc. within the linked list. The interface

5 Manually creating a linked list
Do the memory and logical diagrams on the board. int main() { LLNode * temp; // mPayload is an double. LList L; temp = new LLNode(); L.mStart = temp; temp->mPayload = 5; temp->mNext = new LLNode(); temp->mNext->mPayload = 10; temp = temp->mNext; // "Advances" temp temp->mNext = new LLNode() temp->mNext->mPayload = 15; temp = temp->mNext; temp->mNext = NULL; // Traverse the linked list temp = L.mStart; while (temp != NULL) cout << temp->mPayload << endl; }

6 Part III: Linked List operations
Most applications of linked lists require that we are able to: Add new elements to a linked list At the end, beginning, middle, or "in-place" Remove elements of a linked list Find (by value or position) an element Size Two (at least…) approaches to each: Top-level: Let the LList class do the work, treat LLNode as a structure Often a bit faster, but less elegant Delegated: Let the LList class call a method of LLNode, which is where most of the work is done. A good example of recursion I’ll do some of both (just for practice)

7 IIIa: Add to Beginning (push_front)
[Top-level approach] Assumption: We're given a payload value (Dynamically) create a new LLNode and set payload. Two cases: This is the first element The new element should be pointed to by mStart. It isn't. The mNext field of the new element should point to the old last element.

8 IIIb: Add to End (push_end)
[Delegated approach] Allocate a new node as with push_front. Recursively calls this on the following node until we reach the end. Two cases at the top level: This is the first element in the linked list The new element should be pointed to by mStart. It isn't. We tell the existing first node to add the new node after itself (recursion) Two cases at the LLNode level: We have a node following us: tell them to add this new node after themselves. We don’t: attach it after us

9 IIIf. Find [Top-Level] Goal: Find a LLNode * Two approaches:
Find by value. Find by index. [Add operator overloading] Bot work the same way: traverse the list until you find the requested “thing”. If you walk off the list, return NULL (or some error code)

10 IIIc. Insert [Delegated, non-recursive] Often used together with find.
Find gets us a LLNode pointer We tell that LLNode to insert the given value after itself.

11 IIId. In-order [Top-Level] Set up a traversal pattern:
Make a temporary pointer, copy the value of mStart to it. Look at this node and the one following. Two possibilities: We have no following node or the new node should go between the current and next node: attach the new node there. All other cases: move on to the next node.

12 IIIe. Remove [Top-Level] Indicate the “target” by value.
Traverse to get the node before the one we want to remove. Why the one before? Re-route the pointers and call delete on the target node.

13 IIIg. [Hidden] Cleanup Since we’re using dynamic allocation, we must ensure delete is called. We want to trigger this in the Llist class’s destructor. Use a delegated approach to set up a cascading delete. Top level delete’s mStart. mStart deletes the thing after it. That deletes the thing after it.

14 IIIh. Size [Delegated] If this node has a successor, return their result + 1. Otherwise, just return 1.

15 Part IV: STL lists I want you to do Lab4 “manually”
But…the standard template library has a very nice linked list class. Works in many ways like vector’s, but internally it is a linked list (vectors are an array internally) Uses an iterator nested-class, which is basically a pointer to a LLNode structure.

16 STL List example #include <iostream> #include <list> #include <algorithm> // For the find function using namespace std; int main() { list<float> flist; list<string> slist; flist.push_back(14.3f); flist.push_front(7.9f); flist.push_back(15.6f); slist.push_back("ABC");

17 STL List Example, cont. //cout << flist[0] << endl; // Error! list<float>::iterator fiter; list<string>::iterator siter; for (fiter = flist.begin(); fiter != flist.end(); fiter++) cout << *fiter; // Find an element fiter = std::find(flist.begin(), flist.end(), 14.3f); // Insert an element after fiter flist.insert(fiter, -1.3f);

18 STL List Example, cont. // Remove all elements from flist that are
// between 5.0 and 10.0 for (fiter = flist.begin(); fiter != flist.end(); ) { if (*fiter > 5.0 && *fiter < 10.0) fiter = flist.erase(fiter); } // Remove all elements from flist. flist.clear();

19 Part V: Other linked structures
Many other data structures are based on [linked] lists Queues Stacks Doubly-Linked Lists [Talk about operations of each and some app’s]


Download ppt "Lecture 04: Sequences structures"

Similar presentations


Ads by Google