CS2 Exam 2: LINKED LISTS (1-35) & STACK/QUEUE (36-67)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Suppose the following operations are performed on an empty stack: push(0); push(9); push(12); push(1); Insert numbers in a list like "a1, a2, a3, a4" where a1 is the top of the stack and a4 is the bottom. Feel free to use NULL if needed.

1, 12, 9, 0

Suppose the following operations are performed on an empty stack: push(8); push(7); pop(); push(19); push(21); pop(); Insert numbers in a list like "a1, a2, a3, a4" where a1 is the top of the stack and a4 is the bottom. Feel free to use NULL if needed.

19, 8,

Suppose the following operations are performed on an empty queue: enqueue(5); enqueue(7); enqueue(9); enqueue(12); Insert numbers in a list like "a1, a2, a3, a4" where a1 is the front of the queue and a4 is the rear of the queue. Feel free to use NULL if needed.

5, 7, 9, 12

Suppose the following operations are performed on an empty queue: enqueue(5); enqueue(7); dequeue(); enqueue(9); enqueue(12); dequeue(); enqueue(10); Insert numbers in a list like "a1, a2, a3, a4" where a1 is the front of the queue and a4 is the rear of the queue. Feel free to use NULL if needed.

9, 12, 10

What is a self-referential data structure?

A data structure that has a pointer to a structure of the same type.

What type of linked list is the STL list container?

A doubly linked list.

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

A linked list can easily grow or shrink in size. In fact, the programmer doesn't need to know how many nodes will be in the list. They are simply created in memory as they are needed. Also, when a node is inserted into or deleted from a linked list, none of the other nodes have to be moved.

What is a list head?

A pointer that simply points to the first node in the list.

What is the difference between a static stack and a dynamic stack?

A static stack has a fixed size and is usually implemented as an array. A dynamic stack expands as items are added to it. Dynamic stacks are implemented as linked lists.

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

Appending

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

Appending a node means that a new node is added to the end of the list. Inserting a node means that a new node is inserted somewhere in the middle of the list.

Write two different code segments that may be used to wrap an index back around to the beginning of an array when it moves past the end of the array. Use an if/else statement in one segment and modular arithmetic in the other.

Code segment using an if/else statement: if (rear == queueSize - 1) rear = 0; else rear++; Code segment using modular arithmetic: rear = (rear + 1) % queueSize;

Describe two operations that all queues perform.

Enqueue and dequeue. To enqueue means to insert an element at the rear of a queue. To dequeue means to remove an element from the front of a queue.

The two primary queue operations are __________ and __________.

Enqueuing and dequeuing.

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

F

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

F

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

F

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

F

T or F, The push operation inserts an element at the end of a stack.

F

T or F, The size of a dynamic stack or queue must be known in advance.

F

What does FIFO mean?

First in first out.

Find the error: 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; } }

If the first node is the one to be deleted, this algorithm simply deletes it, unlinking any other nodes that may be in the list.

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

In a singly linked list each node is linked to a single other node. In a doubly linked list each node not only points to the next node, but also the previous one. In a circularly linked list the last node points to the first,

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

Inserting

The STL stack is considered a container adapter. What does that mean?

It adapts other types of containers so they may be used as a stack.

What problem is overcome by using a circular array for a static queue?

It allows the queue elements to "wrap around" the end of the array. This, in turn, makes the queue more efficient by eliminating the need to copy the queue elements forward each time an item is enqueued.

What does LIFO mean?

Last in first out.

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

ListNode *nodePtr, *nextNode; nodePtr = head; while (nodePtr != NULL) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; }

Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that 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.

ListNode *nodePtr; nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; }

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

NULL

The two primary stack operations are __________ and __________.

Push and pop.

Describe two operations that all stacks perform.

Push and pop. The push operation causes a value to be stored, or pushed onto the stack. The pop operation retrieves (and hence, removes) a value from the stack.

The two ADTs in the Standard Template Library that exhibit queue-like behavior are __________ and __________.

Queue and deque.

What two queue-like containers does the STL offer?

Queue and deque.

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

Remove the node from the list without breaking the links created by the next pointers. Deleting the node from memory.

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

T

T or F, A static stack or queue is built around an array.

T

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

T

T or F, The STL stack container's pop operation does not retrieve the top element of the stack, it just removes it.

T

T or F, The pop operation retrieves an element from the top of a stack.

T

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

T

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

The advantage that linked lists have over vectors, however, is the speed at which a node may be inserted into or deleted from the list. To insert a value into the middle of a vector requires all the elements below the insertion point to be moved one position toward the vector's end, thus making room for the new value. Likewise, removing a value from a vector requires all the elements below the removal point to be moved one position toward the vector's beginning. When a node is inserted into, or deleted from a linked list, none of the other nodes have to be moved.

Name five basic linked list operations.

The basic linked list operations are appending a node, traversing the list, inserting a node, deleting a node, and destroying the list.

When an element is removed from a queue, where is it removed from?

The front

What element is always retrieved from a stack?

The last data element placed in the stack.

How is the end of a linked list usually signified?

The last node in the list usually points to address 0, the null address.

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

The list can hold values of any data type.

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

The node pointers are simply set to NULL. The nodes themselves are not deleted from memory.

When an element is added to a queue, where is it added?

The rear

What does "traversing the list" mean?

To travel through the list, node by node.

__________ a list means traveling through the list.

Traversing

What types may the STL stack be based on? By default, what type is an STL stack based on?

Vector, list, or deque. By default it is base on the deque type.

The STL stack container is an adapter for the __________, __________, and __________ STL containers.

Vectors, lists, and deques.

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

circular

The queue ADT, by default, adapts the __________ container.

deque

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

doubly-linked

_________ stacks and queues are implemented as linked lists.

dynamic

The __________ element saved in a queue is the first one retrieved.

first

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

head pointer

Describe two operations that static stacks must perform.

isFull and isEmpty. The isFull operation returns true if the stack is full, and false otherwise. This operation is necessary to prevent a stack overflow in the event a push operation is attempted when all of the stack's elements have values stored in them. The isEmpty operation returns true when the stack is empty, and false otherwise. This prevents an error from occurring when a pop operation is attempted on an empty stack.

The __________ element saved onto a stack is the first one retrieved.

last

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

list<float> myList;

Write code that stores the values 12.7, 9.65, 8.72, and 4.69 in the list container you defined for Question 23. (Q. 23: Write code that defines an STL list container called "myList" for holding float values.)

myList.push_back(12.7); myList.push_back(9.65); myList.push_back(8.72); myList.push_back(4.69);

Write code that reverses the order of the items you stored in the list container in Question 24. (Q. 22: Write code that stores the values 12.7, 9.65, 8.72, and 4.69 in the list container you defined for Question 23.) (Q. 23: Write code that defines an STL list container called "myList" for holding float values.)

myList.reverse();

Find the error: 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; } }

nodePtr is never properly initialized.

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

self-referential data structure

_________ stacks and queues are implemented as arrays.

static


संबंधित स्टडी सेट्स

ECPI 2020 NUR164 CHAPTER 5 CULTURAL DIVERSITY

View Set

Student Exploration: Covalent Bonds

View Set

BIOL 102 EXAM 2 Essay Question #4

View Set

Advertising Procedures Chapter 9

View Set