List Data Structure.

Slides:



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

Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures: A Pseudocode Approach with C
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Data Structures Using Java1 Chapter 4 Linked Lists.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked List by Chapter 5 Linked List by
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
The List ADT.
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Pointers and Linked Lists
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Review Deleting an Element from a Linked List Deletion involves:
CE 221 Data Structures and Algorithms
Building Java Programs
Chapter 20 Lists, Stacks, Queues, and Priority Queues
UNIT-3 LINKED LIST.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCI 3333 Data Structures Linked Lists.
Chapter 17 Object-Oriented Data Structures
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Linked Lists.
Arrays and Linked Lists
Linked Lists: Implementation of Queue & Deque
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Building Java Programs
Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
Introduction to C++ Linear Linked Lists
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists
Lecture 7: Linked List Basics reading: 16.2
Linked Lists Chapter 5 (continued)
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Linked Lists Chapter 5 (continued)
Problem Understanding
Presentation transcript:

List Data Structure

What is List? A list is a sequential data structure. It differs from the stack and queue data, structures in that additions and removals can be made at any position in the list.

List operations Add : adds a new node Set : update the contents of a node Remove : removes a node IsEmpty : reports whether the list is empty IsFull : reports whether the list is full Initialize : creates/initializes the list Destroy : deletes the contents of the list (may be implemented by re-initializing the list)

Illustration/example Initialize(L) Create a new empty List named L Add(1,X,L) adds the value X to list L at position 1 (the start of the list is position 0), shifting subsequent elements up Set(2,Z,L) updates the values at position 2 to be Z Remove(Z,L) removes the node with value Z Get(2,L) returns the value of the third node, i.e. C IndexOf(X,L) returns the index of the node with value X, i.e. 1

Illustration/example Operation List’s contents Return value 1. Initialiaze(L) <empty> - 2. Add(0,A,L) A - 3. Add(0,B,L) B A - 4. Add(1,C,L) B C A - 5. Set(1,N,L) B N A - 6. Add(1,D,L) B D N A - 7. Remove(A,L) B D N A 8. Set(3,I,L) B D N I - 9. Remove(D,L) B N I D 10. Remove(N,L) B I N

Exercise: List Operation What would the contents of a list be after the following operations? Initialise(L) Add(0,A,L) Add(0,F,L) Add(1,X,L) Add(1,G,L) Add(3,P,L) Add(2,V,L) Set(3,K,L) Set(0,H,L) Remove(V,L) Remove(P,L) H G K A What values would be returned by the following operations? IndexOf(A,L) IndexOf(H,L) Get(3,L)

Storing a list in a static data structure (Array List) This implementation stores the list in an array. The position of each element is given by an index from 0 to n-1, where n is the number of elements. Given any index, the element with that index can be accessed in constant time – i.e. the time to access does not depend on the size of the list. To add an element at the end of the list, the time taken does not depend on the size of the list. However, the time taken to add an element at any other point in the list does depend on the size of the list, as all subsequent elements must be shifted up. Additions near the start of the list take longer than additions near the middle or end. When an element is removed, subsequent elements must be shifted down, so removals near the start of the list take longer than removals near the middle or end.

Storing a list in a dynamic data structure (Linked List) The Link List is stored as a sequence of linked nodes. As in the case of the stack and the queue, each node in a linked list contains data AND a reference to the next node. The list can grow and shrink as needed The position of each element is given by an index from 0 to n-1, where n is the number of elements. Given any index, the time taken to access an element with that index depends on the index. This is because each element of the list must be traversed until the required index is found. The time taken to add an element at any point in the list does not depend on the size of the list, as no shifts are required. It does, however, depend on the index. Additions near the end of the list take longer than additions near the middle or start. The same applies to the time taken to remove an element. The first node is accessed using the name LinkedList.Head Its data is accessed using LinkedList.Head.DataItem The second node is accessed using LinkedList.Head.NextNode

Adding a node The new node is to be added at a specified index in the list A special case is that the list is empty. In this case, the first node is set to be the new node. Another special case is that the specified index is 0 (the node is to be added at the front of the list).

Adding a node (2) In the general case, an object reference Probe moves through the list until the node before the required index is reached. Here, the node is to be added at index 2. If the new node is to be added at the end of the list, then the NextNode of the last element is set to refer to the new node.

Adding a node (3) A possible algorithm (pseudocode) for the Add operation Add(index, Data, LinkedList) Declare NewNode and initialise NewNode.DataItem with data If(List.isEmpty) Set NewNode.NextNode to NULL Copy NewNode to LinkedList.Head Else If (index is 0) Copy LinkedList.Head to NewNode.NextNode Copy LinkedList.Head to Probe i = 0 While i < index-1 And end of list not reached Copy Probe.NextNode to Probe Increment i End While Copy Probe.NextNode to NewNode.NextNode Copy NewNode to Probe.NextNode End If Set NewNode to NULL Set Probe to NULL Additional operations can also be implemented to add to the beginning and end of the list. AddFirst(Data, LinkedList) calls Add(0, Data, LinkedList) AddLast(Data, LinkedList) calls Add(Size, LinkedList), where Size is itself a call to the Size operation of the list.

Removing a node A TargetNode object is created. There is a special case when TargetNode is equal to the first node. In this case, LinkedList is set to point to the second node. In the general case, an object reference Probe moves through the list until the required node is reached – at each node the current node (Probe) is compared with TargetNode. A Previous object reference is also required to keep track of the predecessor of the node to be removed. A special case is when both references point to the same node – this happens when the first node is to be deleted. To delete the current node we can set Previous.NextNode to be equal to Probe.NextNode.

Removing a node (2)

Accessing nodes (Get, Set, IndexOf, View) Get and Set work in a similar way to Add – Probe moves through the list until the required index is reached, and the DataItem is returned (Get) or updated (Set). IndexOf works in a similar way to Remove – Probe moves through the list until the target node is found, and returns the index. View – Probe moves through the list from beginning to end and the DataItem is returned at each node.

Variations on Linked Lists Circularly linked lists The tail of the list always points to the head of the list Doubly linked lists These permit scanning or searching of the list in both directions. (To go backwards in a simple list, it is necessary to go back to the start and scan forwards.) In this case, the node structure is altered to have two links: Sorted lists Lists can be designed to be maintained in a given order. In this case, the Add method will search for the correct place in the list to insert a new data item.

List Implementation

List Implementation in Java The Java Collections Framework in the most recent version of Java now includes list classes. As you did for the stack & queue, you will create your own List class in order to learn how a list is implemented. Your class will again be a bit simpler than the Collections Framework one but it will do essentially the same job In this case you will look at Java implementations of an ArrayList and a LinkedList. Both lists have the same operations, so we can define a class List which includes minimal implementations of the operations. The ArrayList and LinkedList classes will be subclasses of List. The List class will define the set of operations which a list class must have. This is sometimes known as the interface of a list. Both kinds of list will have the same interface.

List Implementation in Java Notice that the LinkedList class makes use of a Node class which is exactly the same as the one used for the dynamic queue.