CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 367 – Introduction to Data Structures
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 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.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 102 Computers In Context (Multimedia)‏ 04 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
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 11 / 10 / 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.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 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 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 18 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 29 / 2006 Instructor: Michael Eckmann.
Data Structures Using Java1 Chapter 4 Linked Lists.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Data Structures Using C++1 Chapter 5 Linked Lists.
CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSCI387 Data Structure Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structure & Algorithms
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Linked List Stacks, Linked List Queues, Dequeues
Arrays and Linked Lists
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked List Insert After
CS148 Introduction to Programming II
Computer Science
More on Linked List Yumei Huo Department of Computer Science
Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? Linked lists

A linked list is a data structure where every node contains data and reference(s) to other node(s.)‏ Let's look at drawings on the board. Let's figure out how to – Add to beginning of linked list – Add to end of linked list Need to find last node – Insert after a particular node – Delete a particular node Other options – Storing link to last node – Doubly linked lists (and their operations.)‏ – Circular linked lists (where the “last” node refers to the “head”).

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 The value of the next node for the last element of a linked list is null. The value of the previous node for the first element of a linked list is also null.

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 // All the code on the rest of the slides is outside of the Node class. Node n = new Node(somedata); Node head; 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: 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 // head currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { currnode = currnode.next; } newnode.next = currnode.next; currnode.next = newnode;

Linked lists // delete findnode // head Node prevnode; Node currnode = head; while (!((currnode.data).equals(findnode.data)))‏ { prevnode = currnode; currnode = currnode.next; } prevnode.next = currnode.next; currnode = null; // the data originally refered-to to be garbage collected // if we didn't have currnode = null; here, it would get garbage // collected once the currnode went out of scope.

Linked lists What's deficient in singly linked lists?

Linked lists What's deficient in singly linked lists? –can't add to the end of the list efficiently (add tail)‏ what gets easier? How to do it; what needs to change? –can't “back up” if necessary, so we can't say, traverse the linked list from the tail to the head because we only have a next (one way)‏ create a doubly linked list = add links that go both ways (next and prev)‏

Linked lists Let's consider operations for a doubly linked list on the board. A doubly linked list is a data structure where every node has – data – a reference to previous node (prev)‏ – a reference to next node (next)‏ We maintain a head node and a tail node. The value of the next node for the last (tail) element of a linked list is null. The value of the previous node for the first (head) element of a linked list is also null. When you construct a new node, set the data and next and prev default to null