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

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

  • View
    222

  • Download
    2

Embed Size (px)

Citation preview

CS 106Introduction 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

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

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

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.

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;

}

}

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.

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;

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;

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;

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;

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.

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

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?

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)

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 */ }

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.

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?

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.

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

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.

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.

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.

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?

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

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

Implementing a Stack• Properties of a Stack

– linear data structure

– LIFO processing

• What operations should be available to Stacks?

– push

– pop

– peek

– empty?

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?

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 ?

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?

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.