1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Chapter 4: Trees Part II - AVL Tree
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Linked Lists
Linked Lists.
hashing1 Hashing It’s not just for breakfast anymore!
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Pointers OVERVIEW.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Classes and Data Structures Jeffrey S. Childs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Lecture 6 of Computer Science II
Cinda Heeren / Geoffrey Tien
Linked Lists Chapter 6 Section 6.4 – 6.6
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
C++ Classes and Data Structures Jeffrey S. Childs
Linked List Intro CSCE 121 J. Michael Moore.
Linked Lists.
CENG 218 Classes and Data Structures
Linked Lists.
Linked List Intro CSCE 121.
Presentation transcript:

1 Chapter 6 Methods for Making Data Structures

2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and removing data. When dynamic arrays are used, the insertion function would add data to the array, while the removal function would “eliminate” data from the array (make it unusable). The correct size of a dynamic array may not be determined at the beginning. If we allocate a size that is too small, we need to expand the size when the array becomes full.

3 Dynamic Arrays in Data Structures If we allocate a size or expand to a size that is too big, memory wastage will occur when the actual usage is less than the allocation, or when many elements have been removed.

4 Array Expansion/Contraction One possible method to avoid memory wastage: –When an element is inserted by the client, increase the size of the array by 1. –When an element is removed by the client, decrease the size of the array by 1. The problem with this method is that it is inefficient – every time an element is inserted or removed, the changeSize function is called…

5 changeSize Function … New element needs to be put into array, so changeSize function is called

6 changeSize Function (cont.) … … new array is made

7 changeSize Function (cont.) … elements are copied over one by one using a for loop …

8 changeSize Function (cont.) Then, the new element can be put in … This process would take place every time a new element needs to be inserted.

9 changeSize Function (cont.) Suppose the element at the end of the array needs to be removed … Likewise, when an element needs to be removed, this method contracts the array by one to conserve memory.

10 changeSize Function (cont.) The changeSize function is called and a new, smaller array is made … …

11 changeSize Function (cont.) The elements are copied over one by one, using a for loop … …

12 changeSize Function (cont.) This method of array expansion/contraction is largely inefficient, because there is too much element copying …

13 Linked Structures Sometimes it is best to store data in a linked structure (an alternative to an Array) A linked structure consists of a group of nodes – each node is made from a struct / class. An object of the Node struct contains an element of data.

14 A Node Struct Template template struct Node { T item; Node *next; }; The item member is for the data. It can anything (T), but it is often the object of a class, used as a record of information. The next pointer stores the address of a Node of the same type! This means that each node can point to another node. next item

15 A Node Struct Template template struct Node { T item; Node *next; }; The item member is for the data. It can anything (T), but it is often the object of a class, used as a record of information. The next pointer stores the address of a Node of the same type! This means that each node can point to another node. Note that the Node can be also implemented as a class.

16 Nodes In a data structure, each node is made in the heap; therefore, a node can only be accessed by a pointer. The client does not deal with nodes. When the client uses an insertion function, an element of data is passed into the insert function, and the function places it in a node.

17 Nodes (cont.) When the client wants to retrieve data, the data in a node is returned to the client (but not the node itself). The node struct/class template exists for use by the data structure.

18 Example of a Linked Structure (cont.) next item

19 Example of a Linked Structure start next item

20 Example of a Linked Structure (cont.) start The last node doesn’t point to another node, so its pointer (called next) is set to nullptr (indicated by slash). The start pointer would be saved in the private section of a data structure class.

21 Linked Lists The arrangement of nodes in the linked structure on the previous slide is often called a linked list. We can access any element of the linked list, for retrieval of information. We can also remove any element from the linked list (which would shorten the list). We can also insert any element into any position in the linked list.

22 Linked List Advantages … … Removing an element from the middle of a linked list is fast.

23 Linked List Advantages (cont.) … … 5321 Removing an element from the middle of a linked list is fast.

24 Removal Problem in Array …… Removing elements from the middle of an array (without leaving gaps) is more problematic

25 Removal Problem in Array (cont.) …… A loop must be used to slide each element on the right one slot to the left, one at a time…

26 Removal Problem in Array (cont.) …… …… ……

27 Removal Problem in Array (cont.) …… Only 100,000 more to go!

28 Linked List Advantages (cont.) Linked lists also waste less memory for large elements (records of information). Wasted memory is memory space in the data structure not used for data. In arrays, the wasted memory is the part of the array not being utilized. In linked lists, the wasted memory is the pointer in each node.

29 Linked List Advantages (cont.) start Linked List Array

30 Accessing item To access the item in the first node: start->item start dereference and member access in one shot

31 Accessing item (cont.) To access the item in the second node: start->next->item start

32 Finding a Possible Mercedes Let’s solve the problem, but let’s assume that item is passed in as a parameter (of type T). This is normally what would happen. Instead of the CarType class having an overloaded != operator, it will have an overloaded == operator. item maker: Mercedes price: year: operator == start … Mercedes

33 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == Finding a Possible Mercedes (cont.) start … Mercedes

34 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr Finding a Possible Mercedes (cont.) start … Mercedes

35 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

36 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

37 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

38 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

39 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

40 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

41 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

42 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false Finding a Possible Mercedes (cont.) start … Mercedes ptr

43 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false After going through the loop several times… Finding a Possible Mercedes (cont.) start … Mercedes ptr

44 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false Notice that found is only set to true if ptr is not nullptr and Mercedes is found … Finding a Possible Mercedes (cont.) start … Mercedes ptr

45 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false then, !found is false and the loop exits Finding a Possible Mercedes (cont.) start … Mercedes ptr

46 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false If Mercedes is not found, ptr eventually gets set to nullptr. What If Mercedes Does Not Exist? start … ptr

47 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false What If Mercedes Does not Exist? (cont.) start … If Mercedes is not found, ptr eventually gets set to nullptr.

48 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false What If Mercedes Does not Exist? (cont.) start … Exit from loop because ptr is nullptr.

49 What If Finding in an Empty Linked List? When a linked list is empty, the start pointer should always be set to nullptr. The start pointer would be set to nullptr inside the constructor, when an empty linked list is first made.

50 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == SAME CODE Finding in an Empty List

51 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr Finding in an Empty List (cont.)

52 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false Finding in an Empty List (cont.)

53 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false Finding in an Empty List (cont.) Exit loop because ptr is nullptr.

54 Inserting a New Node Let’s assume that we want to insert a new node at the beginning of a linked list. Assume that the client passes in a parameter called element (of type T). We would like to: 1.place the element into a node and 2.insert the node at the beginning of the linked list.

55 Inserting a Node at Front element start All new nodes must be made in the heap, SO…

56 element start Node *ptr = new Node ; ptr Inserting a Node at Front (cont.)

57 element start Node *ptr = new Node ; ptr Now we have to store element into the node Inserting a Node at Front (cont.)

58 element start Node *ptr = new Node ; ptr->item = element; ptr Inserting a Node at Front (cont.)

59 element start Node *ptr = new Node ; ptr->item = element; ptr Now we have to think about how to make the pointer called “next” point to the first node in the list, to link it in Inserting a Node at Front (cont.)

60 element start Node *ptr = new Node ; ptr->item = element; ptr You can’t successfully write code like this without thinking about addresses. Inserting a Node at Front (cont.)

61 element start Node *ptr = new Node ; ptr->item = element; ptr REMEMBER…when you want to change the way a pointer points, you HAVE to assign a different address to it Inserting a Node at Front (cont.)

62 element start Node *ptr = new Node ; ptr->item = element; ptr Right now, the pointer called “next” doesn’t have a valid address assigned to it. Inserting a Node at Front (cont.)

63 element start Node *ptr = new Node ; ptr->item = element; ptr To store the correct address in it, we have to find the address of the first node of the linked list. Inserting a Node at Front (cont.)

64 element start Node *ptr = new Node ; ptr->item = element; ptr Where is the address of the first node stored? Inserting a Node at Front (cont.)

65 element start Node *ptr = new Node ; ptr->item = element; ptr Now think, the address would be stored in something that points to it. So where is it stored? Inserting a Node at Front (cont.)

66 element start Node *ptr = new Node ; ptr->item = element; ptr That’s right, in the start pointer. Inserting a Node at Front (cont.)

67 element start Node *ptr = new Node ; ptr->item = element; ptr So now, all we have to do is copy that address into the pointer called “next” Inserting a Node at Front (cont.)

68 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.)

69 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.) Well, it’s been inserted. But start should point to the first node now.

70 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.) REMEMBER…when you want to change the way a pointer points, you have to assign a different address to it

71 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.) We’d like start to point to the new node, so what stores the address of the new node?

72 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.) That’s right, ptr. So now all we have to do is assign the address stored in ptr to the start pointer.

73 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; start = ptr; ptr Inserting a Node at Front (cont.)

74 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; start = ptr; ptr Inserting a Node at Front (cont.) Easy, right?

75 REMEMBER… Use drawings when working with linked lists, until you become an expert. When you want to change the way a pointer points, you have to assign a different address to it. You can find the address you need by looking at other pointers (remember that they store addresses).

76 Inserting into the Middle of a Linked List Suppose we know that there is a Mercedes in a linked list. We would like to insert a node containing Honda right after it. We first find the Mercedes, using code that we looked at before.

77 Inserting a Node at Middle element maker: Mercedes price: year: operator != Node *ptr = start; while ( ptr->item != element ) // element is a parameter ptr = ptr->next; start After this code executes, ptr points to the node that has Mercedes. ptr … Mercedes

78 element maker: Mercedes price: year: operator != Now we would like to insert a CarType object called elementToInsert (containing Honda), which would also be passed in as a parameter, right after the Mercedes Inserting a Node at Middle (cont.) start ptr … Mercedes

79 Well, all new nodes are created in the heap, SO….. Inserting a Node at Middle (cont.) start ptr maker: Honda price: 5000 year: 1985 operator != elementToInsert … Mercedes

80 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

81 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode Now, how about placing elementToInsert into the new node? Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

82 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode->item = elementToInsert; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

83 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode->item = elementToInsert; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

84 Node *newNode = new Node ; newNode->item = elementToInsert; newNode Now, what we want is shown by the dashed arrows; this would cause the insertion of the node Inserting a Node at Middle (cont.) start ptr … Mercedes

85 Node *newNode = new Node ; newNode->item = elementToInsert; newNode We have two pointers we need to change – but we have to be careful about the way we change them Inserting a Node at Middle (cont.) start ptr … Mercedes

86 Node *newNode = new Node ; newNode->item = elementToInsert; newNode If we change the left pointer first, we will no longer be able to access the last node (memory leak) Inserting a Node at Middle (cont.) start ptr … Mercedes

87 Node *newNode = new Node ; newNode->item = elementToInsert; newNode So, we first have to assign the address of the last node into the “next” pointer of the new node Inserting a Node at Middle (cont.) start ptr … Mercedes

88 Node *newNode = new Node ; newNode->item = elementToInsert; newNode Where is the address of the last node stored? Inserting a Node at Middle (cont.) start ptr … Mercedes

89 Node *newNode = new Node ; newNode->item = elementToInsert; newNode That’s right, it is stored in ptr->next Inserting a Node at Middle (cont.) start ptr … Mercedes

90 Node *newNode = new Node ; newNode->item = elementToInsert; newNode->next = ptr->next; newNode Inserting a Node at Middle (cont.) start ptr … Mercedes

91 Node *newNode = new Node ; newNode->item = elementToInsert; newNode->next = ptr->next; ptr->next = newNode; newNode Inserting a Node at Middle (cont.) start Mercedes ptr … Mercedes

92 Removing a Node Let’s assume that we want to remove a new node at the beginning of a linked list. We would like to: 1.create a new pointer to point to the first node, 2.point the start node to the second node and 3.delete the first node by freeing the memory and set the pointer to nullptr

93 Removing the First Node start … Node *ptr = start; start = start->next; delete ptr; ptr = nullptr

94 Removing the First Node (cont.) start Node *ptr = start; start = start->next; delete ptr; ptr = nullptr ptr …

95 Removing the First Node (cont.) … Mercedes Node *ptr = start; start = start->next; delete ptr; ptr = nullptr; ptrstart

96 Removing the First Node (cont.) … Node *ptr = start; start = start->next; delete ptr; ptr = nullptr; startptr Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.

97 Removing the First Node (cont.) … Node *ptr = start; start = start->next; delete ptr; ptr = nullptr; startptr Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.

98 Working With Linked Lists As you can see, sometimes you have to do a lot of thinking and problem-solving when working with linked lists. It is not always obvious how to write code. You can’t memorize the code, because it will not quite fit situations that you will encounter. It is a matter of using logic (and knowing a few tricks of the trade).

99 Speed In some situations, an array can be faster than a linked list, such as when a calculated index is used to access an element. In other situations, a linked list can be faster than an array, such as when removing an element from the middle (as we saw before). –we usually need to search for the element to remove, but we search for it in both the array and linked list.

Reference Childs, J. S. (2008). Methods for Making Data Structures. C++ Classes and Data Structures. Prentice Hall. 100