cs32w11dis2 - CS32 Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 3, January 21) TA: Brian Choi ([email protected]) Section Webpage: Abstraction In Homework 1, you were asked to build a class called Bag. 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 Bag, remove them, count them, and iterate through the items in the Bag, without having to know how exactly the items are stored within the Bag. There is more than one way of implementing the Bag class with the same interface and behavior, your solution to Homework 1 being one of them. Project 2 will let you implement the Bag class in a different way, without altering the interface; that is, a program that uses the previous version of the Bag class should work with the new version without any modification. Such a 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 shall it be? You are 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. Last week, for a (singly) 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 prev , 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 currently 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 Winter 2011 Copyright 2011 Brian Choi Week 3, Page 1/7 head doubly-linked list with 4 nodes head tail head tail tail
Image of page 1

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

View Full Document Right 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 not empty before the insertion.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern