Simple Linked Lists Exercise
PEER GRADED
due Feb. 14
This lab will hopefully do two things: give you a quick refresher on pointers
and also give you a little hands-on experience with linked-lists. Remember
that the linked-list is a simple implementation of the ADT List type.
The Assignment
Create a file called "list.cpp". In that file, add the following class definition:
class Node
{
public:
int val;
Node* next;
};
- In your main function, create three objects of type Node, a, b,
c.
- Set the val member of a to be 3, b to 8 and c
to 10.
- Make the next member of a point to b.
- Set the next member of b to c to NULL
- Write a function (or a member function of the class if prefer) that
takes a Node* and prints out all the values in the linked list.
- Write a function (or a member function of the class if prefer) that
calculates the length of a linked list and returns it.
- Write a function (or a member function of the class if prefer) that
inserts a Node* into a linked list at a specified position. This should
NOT be creating a new Node, only adding the Node* in the correct place.
(Thus, the header should be something like
Node* insert(Node* head, int pos, Node* toInsert)
since it might need to insert before the old head (pos == 0), so the
return value is the new head of the list.
- Write a function (or a member function of the class if prefer) that
deletes all Nodes with a given value from a linked list and returns
the new head of the list.
Node* removeVal(Node* head, int valToDelete)
- Write a function (or a member function of the class if prefer) that
takes in a value and a linked list and creates a new Node with that
value at the end of the list, returning the new list.
Node* addToEnd(Node* head, int valToAdd)
Deliverables
- Show in class to peer grader
- Take snapshots of system working and printouts - put in a document called working.doc. Must include testing all the above as illustrated above.
- Upload code in a zipped file called code.zip to blackboard Exercise
Evaluation
- This exercise will be graded by a peer (fellow student) during class. You must be present on the due date of this exercise hence to get credit on it and to participate in the grading of another student's work. (Only Valid DOCUMENTED Excuse will be allowed for missed peer grading and must be approved by instructor--evaluation process to be determined by instructor)
- During the class session when peer grading is started, the professor will disscuss a "correct" solution with the class and you will use this and guidelines presented and discussed to grade the student's work. FOR THIS ASSIGNMENT you will demonstrate your work to your grader
- As part of this we will explore different solutions done by students and unexpected logic errors and syntax errors.
Evaluation Guidelines
NOTE: the points on each item are given as full points if solution completely correct and 0 points if not (even if code attempted).
ITEM |
Point Range |
Create class Node in a file list.cpp |
(+5 points) |
In your main function, create three objects of type Node, a, b,
c.
- Set the val member of a to be 3, b to 8 and c to 10.
- Make the next member of a point to b.
- Set the next member of b to c to NULL
|
(+5 points) |
Write a function (ora member function of the class if prefer) that
takes a Node* and prints out all the values in the linked list. |
(+10 points) |
Write a function (or a member function of the class if prefer) that
calculates the length of a linked list and returns it. |
(+5 points) |
Write a function (or a member function of the class if prefer) that
inserts a Node* into a linked list at a specified position. This should
NOT be creating a new Node, only adding the Node* in the correct place.
(Thus, the header should be something like
Node* insert(Node* head, int pos, Node* toInsert)
since it might need to insert before the old head (pos == 0), so the
return value is the new head of the list. |
(+10 points) |
Write a function (or a member function of the class if prefer) that
deletes all Nodes with a given value from a linked list and returns
the new head of the list.
Node* removeVal(Node* head, int valToDelete)
|
(+10 points) |
Write a function (or a member function of the class if prefer) that
takes in a value and a linked list and creates a new Node with that
value at the end of the list, returning the new list.
Node* addToEnd(Node* head, int valToAdd) |
(+5 points) |
|