Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENERGY 211 / CME 211 Lecture 12 October 17, 2008.

Similar presentations


Presentation on theme: "ENERGY 211 / CME 211 Lecture 12 October 17, 2008."— Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 12 October 17, 2008

2 Drawbacks of Arrays Even a dynamically-allocated array is still a block of a fixed size What if you have a data structure that changes frequently, due to addition or deletion of data? You can preallocate, but what if you want to keep elements ordered? Shifting array elements is inefficient

3 Memory Issues With arrays, one must frequenly deallocate and reallocate to avoid wasting space This can result in fragmentation of your program’s memory, thus degrading performance Every time you use the new operator, the runtime system looks for an available block, and this takes time!

4 Remedy: Lists An alternative is to use a data structure that represents a list of elements In a list, each element is associated with its own block of memory, called a node, independent of other elements No need to shift elements if one is added or deleted in the middle of the list Iteration through list somewhat slower than in an array, using pointers

5 Singly-linked Lists Each element’s node contains:
the element itself a pointer to the next element’s node In the last node, this pointer is NULL To iterate, keep track of the first node (the head) and follow next pointers: current = current->next; The -> operator performs dereferencing and member access: x->y is (*x).y

6 Visualization 2-node singly-linked list of doubles
3.14 0xbbbbff18 2.72 NULL 1.41 0xbbbbff0c 2-node singly-linked list of doubles New node (1.41) added to list after head node (3.14), which updates its next pointer

7 Doubly-linked Lists In a doubly-linked list, each node contains two pointers, to the next and previous nodes This allows iteration in either direction For first element, previous pointer is NULL More storage required, and additions/deletions more complicated Need to keep track of head and tail nodes

8 Visualization 3.14 0xbbbbff10 NULL 3.14 0xbbbbff20 NULL 2.72 NULL 0xbbbbff20 2.72 NULL 0xbbbbff00 1.41 0xbbbbff10 0xbbbbff00 Node for 1.41 added to 2nd position in doubly-linked list of doubles Must update twice as many pointers!

9 Type Definitions Need to define a type for each element’s node, including pointer(s) Example: doubly-linked list of ints typedef struct tagNode { int data; struct tagNode *prev, *next; } NodeType; Need tagNode since structure refers to itself in its definition Keep track of pointer to first node, and use -> operator to access other node

10 Queues and Stacks Many data structures can be implemented using a linked list A queue maintains a list in a FIFO manner (first in, first out) A stack maintains a list in a LIFO manner (last in, first out) Stacks are used by the runtime system to implement function calls, also useful for evaluating mathematical expressions

11 Lists in the Standard Library
The C++ standard library includes a number of classes that implement different kinds of collections Examples: vector and list A vector uses a dynamically allocated array for random access A list uses a doubly-linked list Each collection class has its faster and slower operations, so choose wisely!

12 Next Time Recursion The MDS debugger


Download ppt "ENERGY 211 / CME 211 Lecture 12 October 17, 2008."

Similar presentations


Ads by Google