Test 1 Statistics - Data Structures | CSCI 1200, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-6ru
koofers-user-6ru 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Lecture 10
Linked Lists Part 1
Test 1 Statistics
Distribution:
Range Number
90-99 27
80-89 66
70-79 42
60-69 28
50-59 19
<50 25
Average: 72.6
Slight linear curve: curved scores are written on the test.
Solutions are posted on line.
Regrades:
If you believe your test was incorrectly graded, please write an explanation on
your test, mark it clearly, and resubmit.
Be forwarned that for any issue other than a numerical tabulation error, we will
regrade your entire exam.
Today’s Class: Linked Lists Part 1
Introductory example on linked lists.
Basic linked list operations:
Stepping through a list
Push back
Insert
Remove
Common mistakes
Our study will eventually lead to our own simplified implementation of the standard
list class, as well as toward more sophisticated link-based data structures.
Objects with Pointers / Linking Objects
The two fundamental mechanisms of linked lists are
creating objects with pointers as one of the member variables, and
making these pointers point to other objects of the same type.
pf3
pf4
pf5

Partial preview of the text

Download Test 1 Statistics - Data Structures | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Lecture 10

Linked Lists — Part 1

Test 1 Statistics

  • Distribution: Range Number 90-99 27 80-89 66 70-79 42 60-69 28 50-59 19 < 50 25
  • Average: 72.
  • Slight linear curve: curved scores are written on the test.
  • Solutions are posted on line.
  • Regrades:
    • If you believe your test was incorrectly graded, please write an explanation on your test, mark it clearly, and resubmit.
    • Be forwarned that for any issue other than a numerical tabulation error, we will regrade your entire exam.

Today’s Class: Linked Lists Part 1

  • Introductory example on linked lists.
  • Basic linked list operations:
    • Stepping through a list
    • Push back
    • Insert
    • Remove
  • Common mistakes
  • Our study will eventually lead to our own simplified implementation of the standard list class, as well as toward more sophisticated link-based data structures.

Objects with Pointers / Linking Objects

  • The two fundamental mechanisms of linked lists are
    • creating objects with pointers as one of the member variables, and
    • making these pointers point to other objects of the same type.
  • These mechanisms are illustrated in the follow short program.

#include using namespace std;

template class Node { public: T value; Node* ptr; };

void main() { Node* ll; // ll is a pointer to a (non-existent) Node. ll = new Node; // Create a Node and assign its memory address to ll ll->value = 6; // This is the same as (*ll).value = 6; ll->ptr = 0; // The value 0 indicates a "null" pointer.

Node* q = new Node; q->value = 8; q->ptr = 0;

ll->ptr = q; // ll’s ptr member variable now has the same value // as the pointer variable q

cout << "1st value: " << ll->value << "\n" << "2nd value: " << ll->ptr->value << endl; }

  • We will make the Node class templated throughout our discussion. The functions that maniplate the nodes will therefore need to be templated as well.
  • The following picture illustrates the structure of memory at the end of the program.

ll

value ptr

value ptr

q 6

Exercise

Based on the foregoing discussion, write the templated function is_there

Basic Mechanisms: Pushing on the Back

  • Goal: place a new node at the end of the list.
  • We must step to the end of the linked list, remembering the pointer to the last node.
    • This is an O(n) operation and is a major drawback to the ordinary linked-list data structure we are discussing now. We will correct this drawback by creating a slightly more complicated linking structure in our next lecure.
  • We must create a new node and attach it to the end.
  • We must remember to update the head pointer variable’s value if the linked list is initially empty. - Hence, in writing the function, we must pass the pointer variable by reference.
  • The function prototype is

template void push_back( Node* & head, T const& value )

Exercise

Write push_back.

Basic Mechanisms: Inserting a Node

  • There are two parts to this: finding the location where the insert must take place, and doing the insert operation.
  • We will ignore the find for now. We will also write only a code segment to understand the mechanism rather than writing a complete function.
  • The insert operation itself requires that we have a pointer to the location before the insert location — in other words, the new value is placed after the value the pointer refers to. - Note that this differs from the use of std::list::insert, where the value to be inserted is placed before the node referred to by the iterator.
  • If p is a pointer to this node, and x holds the value to be inserted, then the following code will do the insertion:

Node * q = new Node; // create a new node q -> value = x; // store x in this node q -> next = p -> next; // make its successor be the current // successor of p p -> next = q; // make p’s successor be this new node

  • Can you draw a picture to illustrate what is happening here?
  • This code will not work if you want to insert the value stored in x in a new node at the front of the linked list.

Basic Mechanisms: Removing a Node

  • There are two parts to this: finding the node to be removed and doing the remove operation.
  • The remove operation itself requires a pointer to the node before the node to be removed.
  • Removing the first node is an important special case.

Exercise

Suppose p points to node that should be removed from a linked list, q points to the node before p, and head points to the first node in the linked list. Write code to remove p, making sure that if p points to the first node that head points to what was the second node and now is the first after p is removed.