Linked Lists Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists CSC220 Winter
Linked Lists.
JAVA & Linked List Implementation
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Data Structures 3 Stewart Blakeway FML
Hashing CS 3358 Data Structures.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
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.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University.
IT 152 Data Structures and Algorithms Tonga Institute of Higher Education.
Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
CSC 211 Data Structures Lecture 13
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Linked List by Chapter 5 Linked List by
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
Introduction to Data Structures and Algorithms
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
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,
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
Tonga Institute of Higher Education
Data Structures & Algorithms
Presentation transcript:

Linked Lists Tonga Institute of Higher Education

Introduction to Linked Lists Arrays are very useful but there have disadvantages  Searching is slow for unordered arrays  Insertion is slow for ordered arrays  Deletion is slow for both arrays  The size of an array cannot be changed easily Linked Lists are another way of storing data that solves some of these problems  Probably the 2 nd most common data structure after arrays  In many cases, you can use a linked list instead of an array

Linked Lists and Arrays Work Differently Array  Each item occupies a particular position.  The position can be accessed with an index number Linked List  The only way to find an item is to follow the chain of items.  It’s like human relations: I ask Sione where Semisi is Sione asks Sela Sela asks Felipe Felipe knows the answer  You can’t access an item directly. You must use relationships between the items to find the item.

Linked Lists LinkedList (LinkList) object  Contains a reference to the first Link object The reference may be null Link object  Contains data (example: First Name, Last Name)  Contains a reference to the next Link object The reference may be null

Linked List Class Has many methods Reference to first link

Link Class Data Reference to next Link Has method to display data Constructor to set values

References LinkList and Link have variables that contain Link objects.  LinkList.firstLink  Link.nextLink Declare the variable – Tell the computer to reserve a space in memory for the variable. How does the computer know how much space to reserve in memory if the computer doesn’t know how big a Link object is? The computer doesn’t need to know how big a Link object is. The variable does not contain a Link object, but only a pointer / reference to another link. Pointer / Reference - A variable that contains the address of a location in memory.

Demonstration Linked List Applet

Linked List Methods – insertFirst() With No Links Create a new Link 1. Set the first variable of the Linked List to be the new Link 1 New Link

Linked List Methods – insertFirst() With At Least 1 Link Create a new Link 1. Set the next variable of the new Link to be the old first Link. 2. Set the first variable of the Linked List to be the new Link New Link

Code View LinkedList insertFirst() Method

Linked List Methods – deleteFirst() With 1 Link 1. Set the first variable of the Linked List variable to be null. Deleted Link 1

Linked List Methods – deleteFirst() With More Than 1 Link 1. Set the first variable of the Linked List variable to be the next link of the old first Link. (If there are no more Links, then the value will be null) Deleted Link 1

Code View LinkedList deleteFirst() Method

Linked List Methods - displayData 1. To display data about the Links, we must travel down every Link and call the displayData method for each Link.

Code View LinkedList displayData() Method

Code View LinkedList Overview

Linked List Methods – find(key) 1. To find a Link with a key, we must travel down every Link and check the key for each Link. Traverse – Traveling down the links 1

Code View LinkedList2 find(key) Method

Linked List Methods – delete(key) With 1 Link 1. To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link. 2. Then, we must set the first Link variable of the Linked List to be null. Deleted Link 1 2

Linked List Methods – delete(key) With More Than 1 Link 1. To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link. 2. If the deleted Link is the last Link, then set the next Link variable of the previous Link to be null. Otherwise, set the next Link variable of the previous Link to be the Link after the current Link. Deleted Link 1 2

Code View LinkedList2 delete(key) Method

Code View LinkedList2 Overview

Linked List Methods – Other There are many more methods we could use with a Linked List  insertAfter(key)  insertBefore(key)  insertLast()  deleteLast()

Double-Ended Linked Lists - 1 A Double-Ended Linked List is similar to a normal linked list However, it has 1 additional feature: A reference to the last Link in the Linked List

Double-Ended Linked Lists - 2 Has many methods Reference to last link

Code View DoubleEndedLinkedList LinkList Class

Double Ended Linked List Methods – insertLast() With No Links Create a new Link 1. Set the first variable of the Linked List to be the new Link 2. Set the last variable of the Linked List to be the new Link New Link 1 2

Double Ended Linked List Methods – insertLast() With At Least 1 Link Create a new Link 1. Set the next variable of the last Link to be the new Link 2. Set the last variable of the Linked List to be the new Link New Link

Code View DoubleEndedLinkedList insertFirst(key) Method

Code View DoubleEndedLinkedList Overview

Sorted Linked List Sometimes, we want to store data in order In a Sorted Linked List, the items are arranged in or by key value Advantages over sorted arrays  Faster insertion of new items  Linked List can grow in size Disadvantages under sorted arrays  More difficult to implement

Demonstration Linked List Applet (Sorted)

Sorted Linked List Methods – insert(key) With No Links Create a new Link 1. Set the first variable of the Linked List to be the new Link 1 New Link

Sorted Linked List Methods – insert(key) With More Than 1 Link Create a new Link 1. Determine the proper location to put the new Link. We do this by finding the Link that will have the new item inserted before it. Call this the current Link. 2. Set the next Link variable of the previous Link to be the new Link 3. If we’re inserting a Link at the end of the Linked List, set the next Link variable of the new Link to be null 4. Otherwise, set the next Link variable of the new Link be the current Link Current Link Previous Link Inserting a new Link in a location that is not the beginning of the list 1 New 2 3

Code View SortedLinkedList insert(key) Method

Code View SortedLinkedList Overview

Doubly Linked Lists The Linked Lists we have examined cannot traverse backwards A Doubly Linked List allows you to traverse forwards and backwards through the list Each Link has a reference to the next Link and the previous Link

Code View DoubleLinkedList Link Class

Doubly Linked List Methods – displayForward() and displayBackward() To display data about the Links, we must travel down every Link and call the displayData method for each Link We start at the first Link or the last Link, depending on whether we’re going forwards or backwards

Code View DoubleLinkedList displayForward() and displayBackward() Methods

Doubly Linked List Methods – insertFirst() With No Links Create a new Link 1. Set the first Link variable Linked List to be the new link 2. Set the last Link variable Linked List to be the new link New Link 1 2

Doubly Linked List Methods – insertFirst() With More Than 1 Link Create a new Link 1. Set the previous link variable of the old first link to be the new link 2. Set the next link variable of the new link to be the old first link 3. Set the first variable of the Linked List to be the new link

Code View DoubleLinkedList insertFirst() Methods

Doubly Linked List Methods – insertLast() Create a new Link 1. Set the next variable of the old last Link to be the new Link 2. Set the previous variable of the new Link to be the old last Link 3. Set the last variable of the Linked List to be the new Link New Link 1 2 3

Code View DoubleLinkedList insertLast() Method

Doubly Linked List Methods – insertAfter(key) Create a new Link Determine Location of where to put new Link 1. Set the next variable of the new Link to be the Link after the current Link 2. Set the previous variable of the Link after the current Link to be the new Link 3. Set the previous Link of the new Link to be the current Link 4. Set the next Link of the current Link to be the new Link

Code View DoubleLinkedList insertAfter(key) Method

Doubly Linked List Methods – deleteKey(key) To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link. 1. Set the next variable of the link before the current Link to be the Link after the Current Link. 2. Set the previous variable of the link after the current Link to be the Link before the Current Link.

Code View DoubleLinkedList deleteKey(key) Method

Doubly Linked List Methods - Other Methods deleteFirst() deleteLast()

Code View DoubleLinkedList Overview

Linked List Efficiency Insertion and Deletion at the beginning of a Linked List are very fast.  Only requires changing 1 or 2 references which takes O(1) time Finding, Deleting or Inserting Items next to a specific item is slower  Requires searching through, on average, half of the list (N/2) which is O(N) time  However, inserting and deleting is much faster than an array because nothing needs to be moved.

Sorted Linked List Efficiency Insertion and Deletion of items  Requires searching through, on average, half of the list (N/2) which is O(N) time If an application frequently accesses the minimum or maximum item and fast insertion is not important, then this is a good choice For example, a Linked List would be a good option for creating a priority queue if we peek a lot.