Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 22 Implementing lists: linked implementations.
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Lists CS 3358.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Linked Lists Linked Lists Representation Traversing a Linked List
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Linked Lists.
Linked Lists.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures: A Pseudocode Approach with C
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Data Structures & Algorithms
Linked Lists. 2 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)
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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)
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)
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Data Structures Using C++ 2E
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
Data Strcutures.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
Linked Lists. 2 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)
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.
Linked Lists based on the original work of Dr. Roger deBry Version 1.0
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Data Structure & Algorithms
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
1 Linked list. 1 A linked list, or one-way list, is a linear collection of data elements, called nodes Each node is divided into two parts: * first part.
C++ Programming:. Program Design Including
Linked Lists.
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.
Review Deleting an Element from a Linked List Deletion involves:
Linked lists.
Linked lists.
Presentation transcript:

Linked List

p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element, a second element……and a last element. p Frequently, we want to add items to or delete items from a list Introduction

A Shopping List Milk Eggs Butter Tomatoes Apples Oranges Bread Milk Eggs Butter Tomatoes Apples Oranges Bread Milk Eggs Butter Tomatoes Apples Oranges Bread Chicken Corn Lettuce Milk Eggs Butter Tomatoes Apples Oranges Bread Chicken Corn Lettuce

p A linked lists, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. p Each node is divided into 2 parts: 1. the first part contains the information of the element, and 2. the second part, called the link field or nextpointer field, contains the address of the next node in the list. Linked Lists

Linked lists p A linked list consists of a sequence of nodes connected by links, plus a header. p Each node (except the last) has a successor, and each node (except the first) has a predecessor. p Each node contains a single element (object or value), plus links to its successor and/or predecessor. antbatcat header null link node element link

Linked lists (2) p The length of a linked list is the number of nodes. p An empty linked list has no nodes. p In a linked list: p We can manipulate the individual elements. p We can manipulate the links, thus changing the linked lists very structure! (This is impossible in an array.)

Singly-linked lists (1) p A singly-linked list (SLL) consists of a sequence of nodes, connected by links in one direction only. p Each SLL node contains a single element, plus a link to the nodes successor (or a null link if the node has no successor). p An SLL header contains a link to the SLLs first node (or a null link if the SLL is empty). pigdogratcatdog

antbatcat first Example: SLL traversal Instance method (in class SLL ) to traverse an SLL: Instance method (in class SLL ) to traverse an SLL: public void printFirstToLast () { // Print all elements in this SLL, in first-to-last order. for (SLLNode curr = this.first; curr != null; curr = curr.succ) System.out.println(curr.element); } p Animation: antbatcat first curr antbatcat first curr antbatcat first curr antbatcat first curr

Bed Number Patient 1Kirk 2 3Dean 4Maxwell 5Adams 6 7Lane 8Green 9Samuels 10 11Fields 12NelsonsNext A hospital ward contains 12 beds, of which 9 are occupied. Suppose we want an alphabetical listing of the patients. Questions5START

Bed Number Patient 1Kirk 2 3Dean 4Maxwell 5Adams 6 7Lane 8Green 9Samuels 10 11Fields 12NelsonsNext Answer! PatientBed Adam5 Dean3 Fields11 Green8 Kirk1 Lane7 Maxwell4 Nelson12 Samuels9

INFO 1 2 3O 4T 5 6SPACE 7X 8 9N 10I 11E 12LINK What is the actual word based on the list? Questions9START

Answer! START = 9, so INFO[9]=N LINK[9]=3, so INFO[3]=O LINK[3]=6, so INFO[6]=SPACE LINK[6]=11, so INFO[11]=E LINK[11]=7, so INFO[7]=X LINK[7]=10, so INFO[10]=I LINK[10]=4, so INFO[4]=T LINK[4]=0 – a NULL Value INFO 1 2 3O 4T 5 6SPACE 7X 8 9N 10I 11E 12LINK START

TEST LINK What is the score of both SCIENCE & ART? Questions3SCIENCE8ART

TEST LINK SCIENCE 84, 62, 74, 100, ART 88, 74, 93, 82 Answer:3SCIENCE8ART

Customer 1Vito 2Hunter 3Katz 4Evans 5Rogers 6Tellers 7Jones 8Grant 9McBride 10Weston 11Scott 12AdamsLINK Suppose a brokerage firm has 4 broker and each broker QuestionsBrokerPointer1Bond12 2Kelly3 3Hall0 4Nelson9

Customer 1Vito 2Hunter 3Katz 4Evans 5Rogers 6Tellers 7Jones 8Grant 9McBride 10Weston 11Scott 12AdamsLINK Bonds list of Customer would be:Grant, Scott, Vito, Katz 2. Kellys list of customer would be: Hunter, McBridge, Evans 3. Nelsons list of customer would be: Tellers, Jones, Adams, Rogers, Weston AnswerBrokerPointer1Bond8 2Kelly2 3Hall0 4Nelson6

NameSSNSEXSALARY 1DAVIS Female22,800 2KELLY Male19,000 3GREEN Male27,200 4BROWN Female14,700 5LEWIS Female16,400 6COHEN Male19,000 7RUBIN Female15,500 8EVAN Male34,200 9HARRIS Female22, LINK Question4Start

p p Each node in the linked list is a class, as shown here. data link 10 data link 15 data link 7 null Declarations for Linked Lists

data link 7 p p The data portion of each node is an int. link null public class IntNode { private int data;... } data link 15 Declarations for Linked Lists data 10

p p Each IntNode also contains a link which refers to another IntNode. data 15 data 7 public class IntNode { private int data; private IntNode link;... } Declarations for Linked Lists data 10 link null link

Declarations for Linked Lists p p A program can keep track of the front node by using a variable such as head in this example. p p Notice that head is not an IntNode -- it is a reference to an IntNode. data link 10 data link 15 data link 7 null head

Declarations for Linked Lists p p A program can keep track of the front node by using an IntNode reference variable such as head. p p Notice that head is not an IntNode -- it is a reference to an IntNode. We represent the empty list by storing null in the head reference. head null

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

Inserting an IntNode at the Front ¶ ¶Create a new node null head

Inserting an IntNode at the Front ¶ ¶Create a new node... · ·Place the data in the new node's data field null head 13

Inserting an IntNode at the Front null head 13 ¶ ¶Create a new node... · ·Place the data in the new node's data field.... Ž ŽConnect the new node to the front of the list.

Inserting an IntNode at the Front null head 13 ¶Create a new node... ·Place the data in the new node's data field.... Ž Connect the new node to the front of the list. ¹Make the head refer to the new head of the linked list.

Inserting an IntNode at the Front null head 13 ¶Create a new node... ·Place the data in the new node's data field.... Ž Connect the new node to the front of the list. ¹Make the head refer to the new head of the linked list.

Pseudocode for Inserting IntNodes p p IntNodes are often inserted at places other than the front of a linked list. p p There is a general pseudocode that you can follow for any insertion function...

Pseudocode for Inserting IntNodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: head = new IntNode(newEntry, head);

Pseudocode for Inserting IntNodes ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position.

Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. In this example, the new node will be the second node In this example, the new node will be the second node previous

Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position What is the name of this link? Look at the link which is in the node previous Look at the link which is in the node previous

Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position This link is called previous.link This link is called previous.link What is the name of this link ? previous

Pseudocode for Inserting IntNodes null head ËOtherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position previous.link refers to the head of a small linked list, with 10 and 7 previous.link refers to the head of a small linked list, with 10 and 7 previous

Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. 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 Write one Java statement which will do the insertion. previous

Pseudocode for Inserting IntNodes null head ·Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the node which is just before the new node's position. 13 Write one Java statement which will do the insertion. previous previous.link = new IntNode(newEntry, previous.link);

Pseudocode for Removing IntNodes p p IntNodes often need to be removed from a linked list. p p 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. p p Well look at the technique for removing a node from the front of a linked list.

Removing the Head IntNode null head 13 Draw the change that this statement will make to the linked list.

Removing the Head IntNode null head 13

Removing the Head IntNode null head 13

Removing the Head IntNode Heres what the linked list looks like after the removal finishes null head

p p It is easy to insert or remove a node at the front of a list. p p You also need a technique for inserting or removing a node elsewhere Summary