1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Chapter 17 Linked Lists.
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Linklist21 Linked Lists II The rest of the story.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Copyright © 2002 Pearson Education, Inc. Slide 1.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Linked List by Chapter 5 Linked List by
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
Pointers and Linked Lists
CSC212 Data Structure - Section BC
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Exercise 1 From Lec80b-Ponter.ppt.
CSCI 3333 Data Structures Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Lists.
Comp 249 Programming Methodology
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

1 Linked Lists It’s a conspiracy!

2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of a data item and a reference to the next node -- the connecting reference is called a link –Links keep nodes in sequence –Last node’s link is assigned null Entire list is accessed via the first node in the list, usually called the head node

3 Node structure public class Node { private int data; private Node link; … } Data type is for data member declared as int as an example – could be any type (and soon will be) Node contains a reference to itself -- actual use is as reference to another Node

Portrait of a linked list List begins with head node –keeps track of list front –may or may not contain data; if it doesn’t, it’s a dummy node A list may also have a tail node to keep track of the end of the list – could also be a dummy 4

Portrait of a linked list 5 data link 10 data link 15 data link 7 head NULL tail Head and tail nodes illustrated here are dummies; they have link members that point to other nodes, contain no data

6 previousEvery node has a link through which it can be accessed – this is the link member of the previous node For example, in the list from the previous slide, head.link refers to the first node in the list –the data in the first node is head.link.data –head.link.link is a reference to the second node Accessing list members

Operations on Nodes Constructor: creates Node with specified data and link Accessors & mutators for data and link 7

Constructors 8 public Node(int initialData, Node initialLink) { data = initialData; link = initialLink; } public Node () { data = 0; link = null; }

Accessors & Mutators public int getData () { return data; } public Node getLink() { return link; } public void setData (int newData) { data = newData; } public void setLink (Node newLink) { link = newLink; } 9

10 Operations on Linked Lists Inserting a node –at the front –at any position other than the front Removing a node –from the front –from any other position Removing all nodes

11 Operations on Linked Lists Finding the length of a list Finding a target value in a list Finding a value at a specified position in a list Making a copy of a list Making a copy of part of a list

12 Inserting a Node at the Front We want to add a new entry, 13, to the front of the linked list shown here null head entry 13

Inserting a Node at the Front head = new Node(13, head); –first argument is data for new node –second argument is link for new node; points to node that used to be at front of list null head 13 When the function returns, the linked list has a new node at the front, containing 13.

14 Caution!Caution! Always make sure that your linked list methods work correctly with an empty list. EMPTY LIST

15 Pseudocode for Removing Nodes Nodes often need to be removed from a linked list. As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere. We’ll look at the pseudocode for removing a node from the front of a linked list.

16 Removing the Head Node null head 13 Removal of the first node is easily accomplished with the following instruction: head = head.getLink();

17 Removing the Head Node Here’s what the linked list looks like after the removal finishes. Automatic garbage collection takes care of removing the node that is no longer part of the list null head_ptr

Inserting nodes elsewhere in the list The head node is the only node to which we have direct access, unless we also maintain a tail node Even if this is the case, however, the task of adding a node anywhere besides the front is a bit complicated, since we need access to the node that precedes the spot where the new node is to be added 18

19 Pseudocode for Inserting Nodes null head We begin with the assumption that we already have a reference to the Node just before the one we want to add (labelled previous_ptr in the illustration) In this example, the new node will be the second node In this example, the new node will be the second node previous_ptr

20 Pseudocode for Inserting Nodes null head Look at the pointer which is in the node referenced by previous_ptr Look at the pointer which is in the node referenced by previous_ptr previous_ptr This pointer is called previous_ptr.link This pointer is called previous_ptr.link

21 Pseudocode for Inserting Nodes null head previous_ptr.link points to the head of a small linked list, with 10 and 7 previous_ptr.link points to the head of a small linked list, with 10 and 7 previous_ptr

22 previous_ptr.setLink (new Node(13, previous_ptr.getLink)); Pseudocode for Inserting Nodes null head The new node must be inserted at the front of this small linked list. The new node must be inserted at the front of this small linked list. 13 previous_ptr

Node insertion & the previous example The code on the previous slide isn’t part of the Node class described in the textbook, although the principle is discussed there The previous slide’s approach to adding a Node in the middle of the list is an example of recursive thinking about a linked list: –every linked list has a head node –every node is the head node of a linked list –the very last node in the list is the head node of an empty list 23

Same approach, different code 24 Here is the code provided in the textbook’s Node class; it’s the same approach, wrapped in a method: public void addNodeAfter(int item) { link = new Node(item, link); } Here, the calling object (of type Node) takes the place of previous_ptr.link in the first example

25 Removing a Node from elsewhere in the list If we want to remove a node from somewhere besides the front of the list, the process is a bit more complicated As with list insertion, the key idea is to make sure that we never lose our grip on the rest of the list while we’re making changes to a particular node Again, we will assume for the moment that we already have a reference to the node previous to the one we want to remove

Removing a node that is not at the head As with insertion, we could accomplish this task with an instruction like the following: previous_ptr.setLink (previous_ptr.getLink().getLink()); or by using the method in the Node class: public void removeNodeAfter( ) { link = link.link; } 26

Methods that operate on an entire list This set of methods includes finding the length of list, searching for a value in the list, and copying all or part of a list These methods are declared static because they operate on lists, not on Nodes Static methods can operate on any linked list, including an empty list 27

linklist228 Code for listLength public static int listLength(Node head) { Node cursor; int answer; answer = 0; for (cursor = head; cursor != null; cursor = cursor.link) answer++; return answer; }

linklist229 Finding a target value in a list Traverse list, using same strategy as in listLength function If target is found, return pointer to its node If target is not found and cursor reaches end of list, return null

linklist230 Code for list_search public static Node listSearch(Node head, int target) { Node cursor; for (cursor = head; cursor != null; cursor = cursor.link) if (target == cursor.data) return cursor; return null; }

linklist231 Finding value at specific location Traverse list to current position, or until cursor is null Return value of cursor once list has been traversed

linklist232 Code for list_locate public static Node listPosition(Node head, int position) { Node cursor; int i; if (position <= 0) throw new IllegalArgumentException ("position is not positive"); cursor = head; for (i = 1; (i < position) && (cursor != null); i++) cursor = cursor.link; return cursor; }

33 Linked Lists It’s a conspiracy!