Top Banner
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann
30

CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

CS 106Introduction to Computer Science I

12 / 06 / 2006

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 106 - Fall 2006

Today’s Topics• Comments and/or Questions?• more with Linked lists

Page 3: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• A linked list is a data structure where every node contains data

and reference(s) to other node(s.)

Page 4: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists • In a singly linked list every node contains data and one

reference to another node.

– e.g.

class Node

{

public AnyType data; // can have more data than one

public Node next;

}

• A Linked List is maintained by keeping a reference to the head of the list.

• From the head, we are able to get at all the other nodes in the list by following the next reference.

Page 5: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists class Node

{

public AnyType data; // can have more data than one

public Node next;

// when constructing a new node, we set its data and

// set the next reference to null

public Node(AnyType d)

{

data = d;

next = null;

}

}

Page 6: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists Let me draw a representation of a node on the board and the

connection of that node to other nodes.

Recall that a reference holds an address and when we want to visualize what is happening, we draw an arrow from the reference to the data at the address stored in the reference.

Page 7: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists Node head; // this will be a reference to the head of the LL

// make sure this is at a scope high enough to be

// accessible for the life of the linked list.

// elsewhere we can have:

Node n = new Node(somedata);

head = n; // sets n to be the head of the linked list

Node newnode = new Node(somedata2);

// to add a newnode to the beginning of the list:

newnode.next = head;

head = newnode;

Page 8: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists Node newnode = new Node(somedata2);

// to add a newnode to the end of the list (assuming the list is

// not empty, i.e. head != null):

Node currnode;

currnode = head;

while (currnode != null)

{

savenode = currnode;

currnode = currnode.next;

}

savenode.next = newnode;

Page 9: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists • Insert after a particular node

// insert newnode after findnode

currnode = head;

while (!((currnode.data).equals(findnode.data)))

{

currnode = currnode.next;

}

newnode.next = currnode.next;

currnode.next = newnode;

Page 10: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Delete a node

// delete findnode

Node prevnode;

Node currnode = head;

while (!((currnode.data).equals(findnode.data)))

{

prevnode = currnode;

currnode = currnode.next;

}

prevnode.next = currnode.next;

currnode = null;

Page 11: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Now let's reconsider these operations to take into account that the

list might be empty (i.e. that head == null).

• Also, let's reconsider the possibility that if we are looking for some particular node that it may not be in the linked list.

Page 12: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Task: to go through a linked list until the end or when we find the

data we're looking for in a node.

• When we wrote the condition last time for the while loop we didn't get to think long enough about it to make it correct. Let's rethink it.

• Inside the while loop we will simply do

currnode = currnode.next;

• We wish to stop the loop when either

– currnode == null

– OR

– the data in the node is what we're looking for

Page 13: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• We wish to stop the loop when either

– currnode == null

– OR

– the data in the node is what we're looking for

• This translates to:

(currnode == null) || ((currnode.data).equals(findnode.data))

• But we need to negate it for the condition of the while loop. Why?

Page 14: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• This translates to:

(currnode == null) || ((currnode.data).equals(findnode.data))

• But we need to negate it for the condition of the while loop. Why?

– because a while loop will continue to iterate until the condition is FALSE

• So, either have:

while ((currnode != null) && !((currnode.data).equals(findnode.data)))

or

while (!((currnode == null) || ((currnode.data).equals(findnode.data))))

because (!a && !b) is equivalent to ! (a || b)

Page 15: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Insert after a particular node

// insert newnode after findnode

if (head != null)

{

currnode = head;

while ((currnode != null) && !((currnode.data).equals(findnode.data)))

{

currnode = currnode.next;

}

if (currnode != null)

{

newnode.next = currnode.next;

currnode.next = newnode;

}

else

{ /* .. .print it wasn’t found */ }

}

else

{ /* .. .print it wasn’t found and the list is empty */ }

Page 16: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Other options

–Storing link to last node (tail)

–Doubly linked lists (and their operations.)

–An idea to have a "dummy" head and tail

• makes adding new nodes easier because we won't have a special case that when the linked list is empty, head doesn't need to be reset.

Page 17: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists • A doubly linked list is a data structure where every node

contains

– data

– a reference to previous node

– a reference to next node

• What do you think the value of the next node is for the last element of a linked list?

• What do you think the value of the previous node is for the first element of a linked list?

• What's the point of having a doubly linked list?

Page 18: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Let's implement our deck of cards as a linked list of card nodes

instead of as an array of cards.

• Note well:

– A user of the Deck and Card class should not need to know how we decided to store the Deck of Cards.

• i.e. the implementation details should be hidden from the user of these classes.

• Deck was implemented as an array of Cards, now we're going to change the implementation to a linked list of Cards and last time we discussed the possibility of storing the Cards in an ArrayList.

Page 19: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Linked lists• Compare a linked list's behaviors to that of arrays and

ArrayLists.

–Which are dynamic in length?

–How about ease of operations in terms of:

• speed of execution and • programmer ease

–Consider these operations:• Add• Insert• Delete

Page 20: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• A queue is a data structure that has the following

characteristics

– It is linear

–Uses FIFO (first in, first out) processing

• Queue operations

–Enqueue – add an item to the rear of the queue

–Dequeue – remove an item from the front of the queue

–Empty – returns true if the queue is empty

• What's significant about a queue is that there are no insert in anywhere or remove from anywhere in the queue. The only place to add something to the queue is the rear, and the only place to remove something from the queue is the front.

Page 21: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• A stack is a data structure that has the following characteristics

– It is linear

– Uses LIFO (last in, first out) processing

• Stack operations

– Push – add an item to the top of the stack

– Pop – remove an item from the top of the stack

– Empty – returns true if the stack is empty

– Peek – retrieve information about the item on top of the stack without removing it

• The only allowable ways to put an item into and to get an item from the stack is via push and pop. There are no insert in anywhere or remove from anywhere in the stack.

• What if there was no peek? Is it redundant --- could a series of the existing operations achieve the same functionality.

Page 22: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• Can anyone think of real world examples that are

naturally modeled by queues?

• Can anyone think of a real world example that is naturally modeled by a stack?

• Let's see visual representations of a queue and a stack on the board.

Page 23: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• Can anyone think of real world examples that are

naturally modeled by queues?

–Line of people at grocery store checkout

–Line of airplanes waiting for takeoff

• Can anyone think of a real world example that is naturally modeled by a stack?

–Plates at a salad bar• A customer takes the top plate (pop)• When new plates come out, they are “pushed” to the

top of the stack.

–Why is this example not a queue?

Page 24: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• Recursive method calls

• A new call to the method causes it's local data to be popped onto the stack• The method calls finish in reverse order, so when a

method call ends, it's local data is popped off the stack

Page 25: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Queues and Stacks• Could we implement a Queue with

– a linked list

– an array

• Could we implement a Stack with

– a linked list

– an array

Page 26: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Implementing a Stack• Properties of a Stack

– linear data structure

– LIFO processing

• What operations should be available to Stacks?

– push

– pop

– peek

– empty?

Page 27: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Implementing a Stack• If we were implementing the data on the Stack as an array of

elements of some type, we would give a maximum length to our array.

• What else would we need to store to allow push, pop, peek and empty? to work?

Page 28: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Implementing a Stack• If we were implementing the data on the Stack as an array of

elements of some type, we would give a maximum length to our array.

• What else would we need to store to allow push, pop, peek and empty? to work?

– top_of_stack

• we have two choices of what this will hold– either the index of the next place to push– or the index of the place to pop

• So, for an empty stack what value would top_of_stack have ?

Page 29: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Implementing a Stack• Let's say we're representing a stack of Strings.

public class Stack

{

private String the_stack[];

private int top_of_stack;

public Stack(int max_size)

{

the_stack = new String[max_size];

}

}

// anything else need to go in the constructor?

Page 30: CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.

Implementing a Stack• Let's say we're representing a stack of Strings.

public class Stack

{

private String the_stack[];

private int top_of_stack;

public Stack(int max_size)

{

the_stack = new String[max_size];

top_of_stack = -1;

}

}

// we can continue this in eclipse now.