Section 1.11 Homework 11 -- Stacks & Queues
Exercises Exercises
2.
You have a singly-linked-list implemented stack containing 24 items. Where is the item at the top of the stack located?
- at the head of the linked list
- at the tail of the linked list
3.
4.
5.
6.
Which of the following container classes can be described as first in first out (FIFO)?
- stack
- queue
- bag
- list
7.
Which of the following container classes can be described as last in first out (LIFO)?
- stack
- queue
- bag
- list
8.
A stack class template is provided for you, with the following definition:
template <typename T>class Stack {Â private:Â Â class Node {Â Â Â public:Â Â Â Â T value;Â Â Â Â Node* next;Â Â Â Â Node(T v, Node* n = nullptr) : value(v), next(n) {}Â Â };Â Â Â Â Node* head;Â Â Â public:Â Â class EmptyException {};Â Â Stack(): head(nullptr) {}Â Â void push(T v);Â Â T pop();Â Â bool isEmpty();};
In this and the subsequent questions you will be asked to implement the various functions declared above. You will need to to write function templates that suitably implement these functions. For this assignment, you have to implement isEmpty. This is an easy question, you don’t have to write much. The template syntax is also provided for you. In the subsequent questions you will need to provide your own.
9.
A stack class template is provided for you, with the following definition:
template <typename T>class Stack {Â private:Â Â class Node {Â Â Â public:Â Â Â Â T value;Â Â Â Â Node* next;Â Â Â Â Node(T v, Node* n = nullptr) : value(v), next(n) {}Â Â };Â Â Â Â Node* head;Â Â Â public:Â Â class EmptyException {};Â Â Stack(): head(nullptr) {}Â Â void push(const T& v);Â Â T pop();Â Â bool isEmpty() const;};
For this question, you have to implement push.
10.
A stack class template is provided for you, with the following definition:
template <typename T>class Stack {Â private:Â Â class Node {Â Â Â public:Â Â Â Â T value;Â Â Â Â Node* next;Â Â Â Â Node(T v, Node* n = nullptr) : value(v), next(n) {}Â Â };Â Â Â Â Node* head;Â Â Â public:Â Â class EmptyException {};Â Â Stack(): head(nullptr) {}Â Â void push(const T& v);Â Â T pop();Â Â bool isEmpty() const;};
For this question, you have to implement pop.
11.
A queue class template is provided for you, with the following definition:
};
This implementation uses a linked list with two pointers, one pointing to the head node and another pointing to the tail node. You can enqueue new values by placing them at the tail and advancing the tail pointer, and you can dequeue values by taking them out of the head and advancing the head pointer. As a result, the values in the linked list are stored effectively in opposite order, with newly inserted elements at the tail.
For this question, you have to implement the function enqueue. It must place a new node with the provided value at the tail of the list (and you can use the tail pointer to get to that location directly, then adjust the tail pointer). You must be careful for the special case where the list is empty.
12.
A queue class template is provided for you, with the following definition:
};
This implementation uses a linked list with two pointers, one pointing to the head node and another pointing to the tail node. You can enqueue new values by placing them at the tail and advancing the tail pointer, and you can dequeue values by taking them out of the head and advancing the head pointer. As a result, the values in the linked list are stored effectively in opposite order, with newly inserted elements at the tail.
For this question, you have to implement the function dequeue. It must throw an error in the case of an empty list, and otherwise it must remove and return the next appropriate value, in a FIFO form. You must be careful for the special case where the list will be empty after the removal.
You have attempted of activities on this page.
