Due: Tuesday, Oct. 8 by 10am
NO LATE ASSIGNMENTS CAN BE TURNED IN
WRITTEN ASSIGNMENT TO BE TURNED IN WITH SUBMIT100
25 points
This assignment is to be TYPED, but NOT COMPILED and submitted using the submit100 command. No late assignments will be accepted since the answers will be distributed and gone over in class the same day. The name of the file should be assignment3
Since you are writing and not compiling these (similar environment to your test next week) you should draw a picture and trace through by hand to convince yourself that these are correct. For example with the linked list problems, trace through an example when the list is empty, contains 1 node, contains 2 nodes, and contains 3 nodes to make sure you are catching all cases.
Write the function MaxNameLength which takes a pointer to a doubly-linked list of DNodes (defined below) and returns a pointer to the DNode containing the longest length name. If there is more than one name of longest length, it returns a pointer to the DNode with the first such name occuring in the linked list.
struct DNode
{
string name;
DNode * next; // points to the next node in the list
Dnode * prev; // points to the previous node in the list
};
If word is a variable of type string, word.length() returns the number of characters in word.
For example, MaxNameLength(faculty) where faculty points to the list below returns a pointer to the node containing the word "Biermann". There are two names of maximum length 8, "Biermann" is the first such occuring one. Note that next pointers point to the right and prev pointers point to the left.
Complete the function MaxNameLength below.
DNode * MaxNameLength(DNode * list)
// postcondition: returns a pointer to the first node with
// the maximum length name, returns NULL if the
// list is empty
{
Write the function RemoveDNode which has a pointer to a node of type DNode and removes this node from a doubly linked list.
For example, RemoveDNode(MaxNameLength(faculty)) where faculty is the list in problem A above, removes the name "Biermann" from the list, resulting in the list:
Calling RemoveDNode(faculty->next) on the list above removes "Chase", resulting in the list:
The node removed is deleted.
Complete the function RemoveDNode below.
void RemoveDNode(DNode * item)
// postcondition: If item is NULL, the list is unchanged.
// If item is not NULL, the node pointed to by item
// is removed from the list and this node is deleted.
{
Write the function ReverseList which reverses a singly linked list. Nodes in the list are of type Node which is defined below.
struct Node
{
string name;
Node * next; // points to the next node in the list
};
For example, consider the list below.
ReverseList(fruit) returns the list:
Hint: In working on this problem, consider using a loop and keeping track of two lists, the current fruit list you are building up and the rest of the original list. You may need additional temporary pointers.
Complete the function ReverseList below. Note list will change, so it must be passed by reference.
void ReverseList(Node * & list)
// postcondition: If list is NULL or contains one node, the list is
// unchanged. Otherwise, the list is reversed.
{
Write the function ReverseStack which reverses a stack of integers. Use the Stack class handed out in class (StackAr.h) and also on the CPS 100 web page. Note that you are NOT modifying the stack class, just using it.
For example, if S is the stack on the left below and ReverseStack(S) is called, then S is modified by reversing its elements, with the modifed S on the right below.
Complete the function ReverseStack below.
void ReverseStack(Stack <int> & S)
// postcondition: If S is empty, S remains unchanged. Otherwise,
// the elements in stack S are reversed.
{
Write the function ReverseStack Member function for the class Stack which reverses a stack of integers. The Stack class was handed out in class (StackAr.h) and is also on the CPS 100 web page. In this problem, you are modifying the class.
This is the same as problem 4, except now you are modifying the class to provide a member function to reverse a stack.
Given this function, the stack S in the example above could be reversed by S.ReverseStack()
Complete the function ReverseStack below.
template <class Etype>
void Stack<Etype>::ReverseStack()
// postcondition: If stack is empty, the stack remains unchanged.
// Otherwise, the elements in the stack are reversed.
{
Create a "README" file (please use all capital letters). Include your name, section number, the date, and an estimate of how long you worked on the assignment in the "README" file. You must also include a list of names of all those people (students, prof, tas, tutor) with whom you consulted on the assignment. See the rules for collaboration in the CPS 100 syllabus.
The name of your typed solutions should be assignment3. To submit your solutions electronically type:
submit100 assign3 README assignment3
You should receive a message telling you that the program was submitted correctly. If it doesn't work try typing ~rodger/bin/submit100 in place of submit100 above.