CMP2003 LectureNotes Week3 4
CMP2003 LectureNotes Week3 4
- What type of data structures and algorithms can we use for an efficient
solution?
Array
0 1 2 3 4 5 6 7 8 9
10 15 20 90 45 80 90 100 120
NOTES
- insert(40)
- search(90)
- deleteAt(0)
- Use a variable to store the size of the list.
- arrayListType<char> list;
- list.insert(‘a’)
- list.delete(‘f’)
Array-based list Linked-list based list
Chapter 3
Array-based Lists
Array-Based Lists
• List
– Collection of elements of same type
• Length of a list
– Number of elements in the list
• Many operations may be performed on a list
• Store a list in the computer’s memory
– Using an array
• Template insertAt
• Copy constructor
– Called when object passed as a (value) parameter to
a function
– Called when object declared and initialized using the
value of another object of the same type
– Copies the data members of the actual object into the
corresponding data members of the formal parameter
and the object being created
• Inserting an element
• Removing an element
• Chapter 3 – Exercise 1
• Chapter 3 – Exercise 2
• Chapter 3 – Exercise 3
• Chapter 3 – Exercise 4
Chapter 5
Linked Lists
Objectives
• 11 basic operations
• Two types of linked lists: sorted, unsorted
• class linkedListType
– Implements basic linked list operations as an ADT
– Derive two classes using inheritance
• unorderedLinkedList and orderedLinkedList
• Unordered linked list functions
– buildListForward and buildListBackward
– Two more functions accommodate both operations
• insertFirst and insertLast
Data Structures Using C++ 2E 38
Structure of Linked List Nodes
• Default constructor
– Initializes list to an empty state
• Destroy the list
– Deallocates memory occupied by each node
• Initialize the list
– Reinitializes list to an empty state
• Must delete the nodes (if any) from the list
– Default constructor, copy constructor
• Initialized list when list object declared
• Destructor
– When class object goes out of scope
• Deallocates memory occupied by list nodes
– Memory allocated dynamically
• Resetting pointers first and last
– Does not deallocate memory
– Must traverse list starting at first node
• Delete each node in the list
– Calling destroyList destroys list
• Copy constructor
– Makes identical copy of the linked list
– Function copyListc checks whether original list
empty
• Checks value of first
– Must initialize first to NULL
• Before calling the function copyList
• Overloading the assignment operator
– Similar to copy constructor definition
• Delete a node
– Consider the following cases:
• The list is empty
• The list is nonempty and the node to be deleted is the
first node
• The list is nonempty and the node to be deleted is not
the first node, it is somewhere in the list
• The node to be deleted is not in the list
– See pseudocode on page 295
– See definition of function deleteNode on page 297
• Insert a node
– Find place where new item goes
• Insert item in the list
– See code on page 304
• Definition of the function insert
• Delete a node
– Several cases to consider
– See function deleteNode code on page 306
• Default constructor
– Initializes the doubly linked list to an empty state
• isEmptyList
– Returns true if the list empty
• Otherwise returns false
– List empty if pointer first is NULL
• Insert a node
– Four cases
• Case 1: Insertion in an empty list
• Case 2: Insertion at the beginning of a nonempty list
• Case 3: Insertion at the end of a nonempty list
• Case 4: Insertion somewhere in a nonempty list
• Cases 1 and 2 requirement: Change value of the
pointer first
• All Cases: After inserting an item, count incremented by
one