Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &

Similar presentations


Presentation on theme: "Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &"— Presentation transcript:

1 Lists Chapter 8

2 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate & initialize object EmptyCheck if it is empty TraverseGo through list, process elements in order stored InsertAdd an item to list at any point Delete Remove an item from the list at any point

3 3 Array/Vector Implementation of List Data Members: –Store the list items in consecutive array or vector locations a 1, a 2, a 3,... a n     a[0] a[1] a[2]... a[n-1], a[n]... a[CAPACITY-1] For an array, add a mySize member to store the length (n) of the list.

4 4 Array/Vector Implementation of List Operations Construction: Set mySize to 0; if run-time array, allocate memory for it For vector: let its constructor do the work Empty: mySize == 0 For vector: Use its empty() operation

5 5 Array/Vector Implementation of List Traverse: or for (int i = 0; i < size; i++) { Process(a[i]); } i = 0; while (i < size) { Process(a[i]); i++; }

6 6 Array/Vector Implementation of List Insert: Delete Insert 6 after 5 in 3, 5, 8, 9, 10, 12, 13, 15 3, 5, 6, 8, 9, 10, 12, 13, 15 Have to shift array elements to make room. Delete 5 from 3, 5, 6. 8, 9, 10, 12, 13, 15 3, 6, 8, 9, 10, 12, 13, 15 Have to shift array elements to close the gap.

7 7 Array/Vector Implementation of List This implementation of lists is inefficient for dynamic lists –Those that change frequently –Those with many insertions and deletions So … We look for an alternative implementation.

8 8 Linked List For the array/vector-based implementation: 1.First element is at location 0 2.Successor of item at location i is at location i + 1 3.End is at location size – 1 Fix: 1.Remove requirement that list elements be stored in consecutive location. 2.But then need a "link" that connects each element to its successor Linked Lists !!

9 9 Linked Lists Definition A linked list of self- referential class objects –called nodes –connected by pointer links (thus, a "linked" list) Subsequent nodes accessed via link- pointer member stored in each member Link pointer in last node set to null (zero) –marks the end of the list Nodes created dynamically, as needed

10 10 Self-Referential Classes A self-referential class contains a pointer member that points to a class object of the same class type class Part_node { public: Part_node ( ); … private: char part_num[8], descrip[20]; int qty; float price; Part_node *next_part; } ;

11 11 Self-Referential Classes This pointer to an object of the type being declared enables class objects to be linked together in various ways –This is how we get linked lists 0 Pointer variable Link Pointer Member Null Pointer

12 12 Dynamic Memory Allocation If the data structures are to be dynamic, then dynamic memory allocation is required –operators new and delete are essential part_node *newPtr = new part_node; Creates the new pointer Allocates the space for a new part node 0

13 13 Dynamic Memory Allocation The delete operator deallocates memory allocated with the new Note: newPtr is not itself deleted -- rather the space newPtr points to 0 delete newPtr;

14 14 Linked Lists Operations Construction: first = null_value; Empty: first == null_value? Traverse ptr = first; while (ptr != null_value) { Process data part of node pointed to by ptr ptr = next part of node pointed to by ptr; }

15 15 ptr = first; while (ptr != null_value) { Process data part of node pointed to by ptr; ptr = next part of node pointed to by ptr; } ptr = first; while (ptr != null_value) { Process data part of node pointed to by ptr; ptr = next part of node pointed to by ptr; } Traverse

16 16 Operations: Insertion Insertion –To insert 20 after 17 –Need address of item before point of insertion –predptr points to the node containing 17 –Get a new node pointed to by newptr and store 20 in it –Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. –Reset the next pointer of its predecessor to point to this new node 917 22 2634 first 20 newptr predptr And voila! The node is inserted (linked) into the list

17 17 Operations: Insertion Note: insertion also works at end of list –pointer member of new node set to null Insertion at the beginning of the list –predptr must be set to first –pointer member of newptr set to that value –first set to value of newptr  Note: In all cases, no shifting of list elements is required !

18 18 Operations: Deletion Delete node containing 22 from list. –Suppose ptr points to the node to be deleted –predptr points to its predecessor (the 20) Do a bypass operation : –Set the next pointer in the predecessor to point to the successor of the node to be deleted –Deallocate the node being deleted. 517222934 first 209 predptr ptr To free space

19 19 Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

20 20 Linked Lists - Disadvantages Overhead of links: –used only internally, pure overhead If dynamic, must provide –destructor –copy constructor No longer have direct access to each element of the list –Many sorting algorithms need direct access –Binary search needs direct access O(1) access becomes O(n) access –must go through first element, and then second, and then third, etc.

21 21 Linked Lists - Disadvantages List-processing algorithms that require fast access to each element cannot be done as efficiently with linked lists. Consider adding an element at the end of the list ArrayLinked List a[size++] = value; or for a vector: v.push_back(value); Get a new node; set data part = value next part = null_value If list is empty Set first to point to new node. Else Traverse list to find last node Set next part of last node to point to new node. This is the inefficient part

22 22 Using C++ Pointers and Classes To Implement Nodes class Node { public: DataType data; Node * next; }; Note: The definition of a Node is recursive –(or self-referential) It uses the name Node in its definition The next member is defined as a pointer to a Node

23 23 Working with Nodes Declaring pointers Node * ptr; or typedef Node * NodePointer; NodePointer ptr; Allocate and deallocate ptr = new Node;delete ptr; Access the data and next part of node (*ptr).data and (*ptr).next or ptr->data and ptr->next

24 24 Working with Nodes Note data members are public This class declaration will be placed inside another class declaration for LinkedList The data members data and next of struct Node will be public inside the class –will accessible to the member and friend functions –will be private outside the class class Node { public: DataType data; Node * next; };

25 25 Class Template LinkedList template ; class LinkedList { private: class Node { public: DataType data; Node * next; }; typedef Node * NodePointer;... }; data is public inside class Node class Node is private inside LinkedList

26 26 Data Members for LinkedList A linked list will be characterized by: –A pointer to the first node in the list. –Each node contains a pointer to the next node in the list –The last node contains a null pointer As a variation first may –be a structure – also contain a count of the elements in the list 917222634 first

27 27 Function Members for LinkedList Constructor –Make first a null pointer and –set mySize to 0 Destructor –Nodes are dynamically allocated by new –Default destructor will not specify the delete –All the nodes from that point on would be "marooned memory" –A destructor must be explicitly implemented to do the delete L first mySize 0

28 28 Function Members for LinkedList Copy constructor –By default, when a copy is made of a LinkedList object, it only gets the head pointer –Copy constructor will make a new linked list of nodes to which copyOfL will point L first mySize 5 917222634 copyOfL first mySize 5 917222634

29 29 Variations An empty head node –Every node has a predecessor –Does away with special case insertions An empty trailer node –Every node has a successor Doubly linked list last prev L first mySize 5 9 17 22 26 34 next

30 30 The STL list Class Template A sequential container –Optimized for insertion and erasure at arbitrary points in the sequence. –Implemented as a circular doubly-linked list with head node.

31 31 The STL list Class Template Node structure struct list_node { pointer next, prev; T data; }

32 32 The STL list Class Template But it's allo/deallo-cation scheme is complex –Does not simply using new and delete operations. Using the heap manager is inefficient for large numbers of allo/deallo-cations –Thus it does it's own memory management.

33 33 The STL list Memory Management When a node is allocated 1.If there is a node on the free list, allocate it. This is maintained as a linked stack 2.If the free list is empty: a)Call the heap manager to allocate a block of memory (a "buffer", typically 4K) b)Carve it up into pieces of size required for a node of a list.

34 34 The STL list Memory Management When a node is deallocated –Push it onto the free list. When all lists of this type T have been destroyed –Return it to the heap

35 35 Comparing List With Other Containers Note : list does not support direct access –does not have the subscript operator [ ]. PropertyArray vector deque list Direct/random access ( [] )  (exclnt)  (good)X Sequential access  Insert/delete at front  (poor)  Insert/delete in middle  Insert/delete at end  Overheadlowestlowlow/mediumhigh

36 36 list Iterators list 's iterator is "weaker" than that for vector.  vector : random access iterators  list : bidirectional iterators Operations in common  ++Move iterator to next element (like ptr = ptr-> next)  --Move iterator to preceding element (like ptr = ptr-> prev)  *dereferencing operator (like ptr-> data)

37 37 list Iterators Operators in common  =assignment (for same type iterators) it1 = it2 makes it1 positioned at same element as it2  == and != (for same type iterators) checks whether iterators are positioned at the same element

38 38 Using list Iterators Example: Construct a list containing first 4 even integers; then output the list. list l; for (int i = 1; i <= 4; i++) l.push_back(2*i); for (list ::iterator it = l.begin(); it != l.end(); it++) cout << *it << " "; cout << endl;

39 39 Limitations of list Iterators Directional iterators don't have: +, -, +=, -=, [] list iterators cannot do "jumping" –No iterator ± n –No direct access Result, cannot implement some sort() algorithms –Solution: list has it's own sort() operation

40 40 Basic list Operations See page 451,2 –Constructors –Destructors –Empty, Size –Push, insert, pop, remove –Front, back –Iterator functions: begin, end, –Sort –Merge, splice –Comparisons


Download ppt "Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &"

Similar presentations


Ads by Google