7 5 3 + * 52 - 8 9 * +!
Write a postfix expression equivalent to 13 + 4 * (10 - 3). Note that this expression has the value 41.
Suppose that a Stack class is implemented and will be used to implement a Queue class. The only data members in the private section of the Queue class are an integer representing the number of elements in the queue and a stack for storing queue elements.
private:
Stack myStack;
int mySize;
The member function for Enqueue is
shown below (recall that both stack and queue classes are templated).
Describe briefly how to implement the function Dequeue that removes the first element from the queue. You do NOT need to write code (although you can), but do need to describe how to dequeue an element when elements are stored and enqueued as described above.
Part A: (3 points)
void Prepend(Node * list, int value)
// precondition: list points to last node of NON-empty circularly-linked list
// (first node is list->next)
// postcondition: new first node with info == value added to
// the circularly-linked list whose last node is list
{
}
Part B: (7 points)
| description | function call |
|---|---|
| determine if k is in s | s.Contains(k) |
| add k to set s if not already present | s.Insert(k) |
| is s a subset of t? (are all elements of s in t) | s < t |
| are s and t equal? (do s and t contain the same elements) | s == t |
For reference, the class declarations for Sequence and SequenceIterator are reproduced on a separate sheet.
Part A: (6 points)
Write a member function Contains that returns true if a set/sequence contains a number and false otherwise. This function should work regardless of how the class Sequence is implemented, i.e., whether Vectors or linked lists are used.
Part B: (4 points)
Write the member function Insert that inserts a number into a set if the number is NOT already in the set. In writing Insert you may call function Contains; assume that Contains works as specified regardless of what you wrote in part A.
Part C: (6 points)
The operator < will be overloaded so that s < t if the set s is a subset of the set t, that is, if every element of s is also an element of t. Conceptually,
(1,4,3) < (3,4,5,2,1) is true
but
(1,4,3) < (3,4,5,2,7) is false.
Write the body of the operator so that it works as intended. You may call function Contains; assume that Contains works as specified regardless of what you write in part A.
struct DormNode
{
string name; // name of a dorm
DormNode * next; // next dormitory in list
StudentNode * students; // linked list of students
};
struct StudentNode
{
string name; // first name of a student
StudentNode * next; // next student in list
};
In the example below, duke points to a linked list of DormNodes
for Southgate, Randolf, Aycock, Trent and Taylor; each DormNode
points to a linked list of students in the dorm.
Complete the function NumStudents whose header is given
below. NumStudents
returns the number of students in a dormitory. In the picture above,
NumStudents(duke->next->next) should return 4, the number of
students in Aycock dorm.
int NumStudents(DormNode * dorm)
// precondition: dorm != 0
// postcondition: returns the number of students in dorm
{
}
Part B: (4 points)
Complete the function NumDormStudents
whose header is shown below.
NumDormStudents returns the number of students in
the dormitory specified by dormName if that dormitory is in
the list pointed to by parameter list.
In the picture on the previous page,
NumDormStudents(duke,"Randolph") returns 5, the number
of students in Randolph dorm;
NumDormStudents(duke,"Roundtable") returns 0.
In writing NumDormStudents you may call function NumStudents from part A. Assume that NumStudents works as specified regardless of what you wrote in part A.
int NumDormStudents(DormNode * list, string dormName)
// postcondition: returns the number of students in the dormitory in list with name
// dormName (and 0 if there is no such dormitory in list)
{
}
Part C: (8 points)
Complete the function PopularName whose header is given below. PopularName returns the name used most frequently by students in a dormitory. If there is a tie, any of most frequently used names may be returned. For example, in the figure at the beginning of this problem the most frequent name in Southgate is Bill, so PopularName(duke) should return "Bill". Since all names in Trent are used equally often,
In writing PopularName you may call the function SameName whose specification is given below. You do NOT need to write SameName.
string PopularName(DormNode * dorm) // precondition: dorm != 0 // postcondition: returns the name that appears the most frequently // in the dorm; if there is a tie, return any of the // most frequently occuring names; if no students in dorm return "" { }
Part D: (4 points)
Assume that there are N dorms, and each dorm has at most M students. The function SameName is O(M). What is the running time of each of the functions you wrote above? Briefly justify your answers.
(Node is defined earlier in Problem 2.)