CS-244 Quiz #5 (Arrays vs Linked Lists)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Why is it harder to remove elements of the list?

Because we can only follow links forward in the list, not backwards.

How do we give the function access to the private variables of LinkedList?

By declaring the << operator to be a friend of LinkedList.

To add a new value at the beginning of the list, there are four steps:

1. Create a new linked list node. 2. Store the new value in the new node. 3. Link the new node to the old first node. Set the new node as the first node.

Adding to the end of the list: We can add to the end of the list just as easily as the beginning of the list:

1. Create a new node 2. Store the element in the new node. 3. Link old last node (if it exists) to the new node. 4. Set the new node as the last node. 5. We also need to set the new node as the first node if the list was empty. 6. Increment size.

To remove an element from the front of the list, we need to do three things:

1. Find the second element of the list and set it as the first element. 2. Delete the old first node. 3. Decrement size. 4. If the list is now empty, set last to nullptr.

linked list

A linear data structure, much like an array, that consists of nodes, where each node contains data as well as a link to the next node, but that does not use contiguous memory.

What is the time complexity of operator <<?

operator << has linear/ O(n) time complexity.

Advantages of Arrays

• O(1) time complexity for randomly accessing any element in the list by its index in the list. However, the element referred to will change if items are inserted at the beginning of the list. • Uses less memory than linked lists when the array is full (size == capacity) or nearly full. • All operations may be slightly faster than linked lists if resizing can be avoided. • Simplest to implement correctly without an existing data structure library.

Advantages of Linked Lists

• O(1) worst case time complexity for adding at either end of the list. • Uses less memory than arrays when all of the following are true: o Elements are large (more bits than a pointer) o Number of items is not known ahead of time o Add / remove operations are common (so the array will not generally be full) • O(1) time complexity for randomly accessing any element by pointer. Nodes are guaranteed to have a fixed location in memory, so a pointer will always point to the same item, even after inserting other elements at the beginning or end of the list.


Set pelajaran terkait

Physics Exam 2 - Conceptual Questions

View Set

test 4 econ, Econ module 26, Econ module 25, module 24 econ, module 23 econ, Module 21, Module 20 Econ, Economics Module 19, Econ test 3, Module 21, Module 20 Econ, Econ test 3, Economics Module 19

View Set

Chapter 26: Documentation and Informatics

View Set

Human Anatomy & Physiology: Chapter 5: Skin and the Integumentary System: Pre-Test Quiz

View Set

Public Speaking Chapters 1 and 21

View Set