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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Stack & Queues COP 3502.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 206 Introduction to Computer Science II 02 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks and Queues Introduction to Computing Science and Programming I.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS 106 Introduction to Computer Science I 11 / 29 / 2006 Instructor: Michael Eckmann.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
Pointers and Linked Lists
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Lesson Objectives Aims
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Introduction to C++ Linear Linked Lists
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS 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.