cs32s10dis2

cs32s10dis2 - CS32: Introduction to Computer Science II...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 1B Notes (Week 3, April 16) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Set. Let us look at it from the user’s point of view. All the user needs to know is that he/she can insert items into the Set, remove them, count the item, and iterate through the items in the Set -- it does not matter how the items are stored within the Set. There are many different ways of implementing the Set with the same interface, your solution to Homework 1 being one of them. Project 2 will let you implement the Set class in a different way, without altering the interface. Such division of interface and implementation is called abstraction . The user is said to be abstracted from the implementation. We’ll do this for many of the data structures we’ll see in this class. Doubly Linked Lists We studied linked lists last week. Each node in a linked list keeps a pointer to the next node. This time, let us create a linked list whose nodes have two links each -- one to the next node ( next ) and another to the previous one ( prev ). It’s called a doubly linked list , and it looks like this: Insertion Suppose I want to add a new node. Where should it go? Well, you’re free to add it anywhere you want in the list, but since we have the head pointer that points to the first node, it might be easier to put it in front. For a linked list, we simply created a new node, made the new node point to the first one, and then updated the head pointer. However, a node in the doubly linked list has an extra pointer, which complicates our problem. Before: After: Let us first figure out how to set next and prev for the new node. Because it is going to be the first one in the list, there will be no “previous” node. Thus we set prev to NULL . next should point to the node that is in front before the insertion, which is pointed by head (and head may point to NULL , no problem.) So for the new node, 1) Set new node’s prev to NULL . 2) Set new node’s next to head . CS32: Introduction to Computer Science II Spring 2010 Copyright 2010 Brian Choi Week 3, Page 1/7 head doubly-linked list with 4 nodes head head tail tail tail
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There are two more things we need to update at this stage. head is pointing to a wrong one, and the original head node’s prev should be updated to point to the newly added node. However, there may be no node in the list, in which case there is no such prev to set. Therefore, we set prev only if the list is nonempty before the insertion. 3) If the list was empty before the insertion, just make
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

cs32s10dis2 - CS32: Introduction to Computer Science II...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online