Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists CSC220 Winter
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
CHP-5 LinkedList.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Overview of Data Structures and Algorithms
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures Michael J. Watts
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
Hashing. Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Stacks, Queues, and Deques
1.A file is organized logically as a sequence of records. 2. These records are mapped onto disk blocks. 3. Files are provided as a basic construct in operating.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
File Organization Techniques
Copyright © Wondershare Software Introduction to Data Structures Prepared by: Eng. Ahmed & Mohamed Taha.
Array.
Tree.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Hash Tables1   © 2010 Goodrich, Tamassia.
Data structures Abstract data types Java classes for Data structures and ADTs.
Linked Lists Tonga Institute of Higher Education.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Introduction to Data Structures and Algorithms
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
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.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Data Structures Michael J. Watts
CHP - 9 File Structures.
Ordered Array.
Array.
Stacks, Queues, and Deques
CS202 - Fundamental Structures of Computer Science II
Data Structures & Algorithms
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Stacks, Queues, and Deques
Presentation transcript:

Linked List

Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion is slow. ▫In both kinds of arrays, deletion is slow. ▫The size of an array can’t be changed after it’s created. a data storage structure that solves some of these problems: the linked list. Linked lists are probably the second most commonly used general purpose storage structures after arrays. The linked list is a versatile mechanism suitable for use in many kinds of general-purpose databases. It can also replace an array as the basis for other storage structures such as stacks and queues. In fact, you can use a linked list in many cases in which you use an array, unless you need frequent random access to individual items using an index.

Links In a linked list, each data item is embedded in a link. A link is an object of a class called something like Link. Because there are many similar links in a list, it makes sense to use a separate class for them, distinct from the linked list itself. Each Link object contains a reference (usually called next) to the next link in the list. A field in the list itself contains a reference to the first link.

class Link { public int iData; // data public double dData; // data public Link next; // reference to next link } Atau class Link { public inventoryItem iI; // object holding data public Link next; // reference to next link }

References and Basic Types In Java a Link object doesn’t really contain another Link object, although it may look like it does. The next field of type Link is only a reference to another link, not an object. A reference is a number that refers to an object. It’s the object’s address in the computer’s memory, but you don’t need to know its value; you just treat it as a magic number that tells you where the object is.

Relationship, Not Position Let’s examine one of the major ways in which linked lists differ from arrays. In an array each item occupies a particular position. This position can be directly accessed using an index number. It’s like a row of houses: You can find a particular house using its address. In a list the only way to find a particular element is to follow along the chain of elements. It’s more like human relations. Maybe you ask Harry where Bob is. Harry doesn’t know, but he thinks Jane might know, so you go and ask Jane. Jane saw Bob leave the office with Sally, so you call Sally’s cell phone. She dropped Bob off at Peter’s office, so…but you get the idea. You can’t access a data item directly; you must use relationships between the items to locate it. You start with the first item, go to the second, then the third, until you find what you’re looking for.

A Simple Linked List The only operations allowed in this version of a list are ▫Inserting an item at the beginning of the list ▫Deleting the item at the beginning of the list ▫Iterating through the list to display its contents

The LinkList Class The LinkList class contains only one data item: a reference to the first link on the list. This reference is called first. It’s the only permanent information the list maintains about the location of any of the links. It finds the other links by following the chain of references from first, using each link’s next field:

The insertFirst() Method

The deleteFirst() Method The deleteFirst() method is the reverse of insertFirst(). It disconnects the first link by rerouting first to point to the second link. This second link is found by looking at the next field in the first link:

The displayList() Method To display the list, you start at first and follow the chain of references from link to link. A variable current points to each link in turn. It starts off pointing to first, which holds a reference to the first link. The statement current = current.next; changes current to point to the next link because that’s what’s in the next field in each link. Here’s the entire displayList() method:

Finding and Deleting Specified Links The find() Method The find() method works much like the displayList() method in the linkList.java program. The reference current initially points to first and then steps its way along the links by setting itself repeatedly to current.next. At each link, find() checks whether that link’s key is the one it’s looking for. If the key is found, it returns with a reference to that link. If find() reaches the end of the list without finding the desired link, it returns null. The delete() Method The delete() method is similar to find() in the way it searches for the link to be deleted. However, it needs to maintain a reference not only to the current link (current), but to the link preceding the current link (previous). It does so because, if it deletes the current link, it must connect the preceding link to the following link. The only way to tell where the preceding link is located is to maintain a reference to it.

Double-Ended Lists A double-ended list is similar to an ordinary linked list, but it has one additional feature: a reference to the last link as well as to the first. The reference to the last link permits you to insert a new link directly at the end of the list as well as at the beginning. Of course, you can insert a new link at the end of an ordinary single-ended list by iterating through the entire list until you reach the end, but this approach is inefficient. Access to the end of the list as well as the beginning makes the double-ended list suitable for certain situations that a single-ended list can’t handle efficiently. One such situation is implementing a queue;

Linked-List Efficiency Insertion and deletion at the beginning of a linked list are very fast. They involve changing only one or two references Finding, deleting, or inserting next to a specific item requires searching through, on the average, half the items in the list. This requires N comparisons. An array is also N for these operations, but the linked list is nevertheless faster because nothing needs to be moved when an item is inserted or deleted. The increased efficiency can be significant, especially if a copy takes much longer than a comparison. Another important advantage of linked lists over arrays is that a linked list uses exactly as much memory as it needs and can expand to fill all of available memory. The size of an array is fixed when it’s created; this usually leads to inefficiency because the array is too large, or to running out of room because the array is too small. Vectors, which are expandable arrays, may solve this problem to some extent, but they usually expand in fixed-sized increments (such as doubling the size of the array whenever it’s about to overflow). This solution is still not as efficient a use of memory as a linked list.