• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • Linked Lists

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 18

Linked Lists - all with Video Answers

Educators


Chapter Questions

Problem 1

What are some of the advantages that linked lists have over arrays?

Check back soon!

Problem 2

What advantage does a linked list have over the STL vector?

Check back soon!

Problem 3

What is a list head?

Check back soon!

Problem 4

What is a self-referential data structure?

Check back soon!

Problem 5

How is the end of a linked list usually signified?

Check back soon!

Problem 6

Name five basic linked list operations.

Check back soon!

Problem 7

What is the difference between appending a node and inserting a node?

Check back soon!

Problem 8

What does "traversing the list" mean?

Check back soon!

Problem 9

What are the two steps required to delete a node from a linked list?

Check back soon!

Problem 10

What is the advantage of using a template to implement a linked list?

Check back soon!

Problem 11

What is a singly linked list? What is a doubly linked list? What is a circularly linked list?

Check back soon!

Problem 12

What type of linked list is the STL list container? What type of linked list is the STL forward_1ist container?

Check back soon!

Problem 13

The __________ points to the first node in a linked list.

Check back soon!

Problem 14

A data structure that points to an object of the same type as itself is known as a(n) __________ data structure.

Check back soon!

Problem 15

After creating a linked list’s head pointer, you should make sure it points to __________ before using it in any operations.

Check back soon!

Problem 16

__________ a node means adding it to the end of a list.

Check back soon!

Problem 17

__________ a node means adding it to a list, but not necessarily to the end.

Check back soon!

Problem 18

__________ a list means traveling through the list.

Check back soon!

Problem 19

In a(n) __________ list, the last node has a pointer to the first node.

Check back soon!

Problem 20

In a(n) __________ list, each node has a pointer to the one before it and the one after it.

Check back soon!

Problem 21

Consider the following code:
struct ListNode
{
int value;
struct ListNode *next;
};
ListNode *head; // List head pointer
Assume a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member.

Check back soon!

Problem 22

Write code that destroys the linked list described in Question 21.

Check back soon!

Problem 23

Write code that defines an STL list container for holding float values.

Check back soon!

Problem 24

Write code that stores the values 12.7, 9.65, 8.72, and 4.69 in the list container you defined for Question 23.

Check back soon!
02:18

Problem 25

Write code that reverses the order of the items you stored in the list container in Question 24.

SS
Sarvesh Somasundaram
Numerade Educator

Problem 26

True or False
T F The programmer must know in advance how many nodes will be needed in a linked list.

Check back soon!

Problem 27

True or False
T F It is not necessary for each node in a linked list to have a self-referential pointer.

Check back soon!

Problem 28

True or False
T F In physical memory, the nodes in a linked list may be scattered around.

Check back soon!

Problem 29

True or False
T F When the head pointer points to nullptr, it signifies an empty list.

Check back soon!

Problem 30

True or False
T F Linked lists are not superior to STL vectors.

Check back soon!

Problem 31

True or False
T F Deleting a node in a linked list is a simple matter of using the delete operator to free the node’s memory.

Check back soon!

Problem 32

True or False
T F A class that builds a linked list should destroy the list in the class destructor.

Check back soon!

Problem 33

Each of the following member functions has errors in the way it performs a linked list operation. Find as many mistakes as you can.

void NumberList::appendNode(double num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new listNode;
newNode–>value = num;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode.
{
// Find the last node in the list.
while (nodePtr–>next)
nodePtr = nodePtr–>next;
// Insert newNode as the last node.
nodePtr–>next = newNode;
}
}

Check back soon!

Problem 34

Each of the following member functions has errors in the way it performs a linked list operation. Find as many mistakes as you can.

void NumberList::deleteNode(double num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head–>value == num)
delete head;
else
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr–>value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr–>next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode–>next = nodePtr–>next;
delete nodePtr;
}
}

Check back soon!

Problem 35

Each of the following member functions has errors in the way it performs a linked list operation. Find as many mistakes as you can.

NumberList::~NumberList()
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr–>next;
nodePtr–>next = nullptr;
nodePtr = nextNode;
}
}

Check back soon!