Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Linked List by Chapter 5 Linked List by
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures Using C++1 Chapter 5 Linked Lists.
© 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.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Doubly and Circular Lists Lecture 07: 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.
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:
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
  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.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
Chapter 16: Linked Lists.
Linked List ADT used to store information in a list
Unit – I Lists.
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Linked lists.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Lists Chapter 4.
Problem Understanding
Linked lists.
BY PROF. IRSHAD AHMAD LONE.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures & Algorithms CHAPTER 5 Linked List Ms. Manal Al-Asmari

Motivation Suppose I have an array: 1,4,10,19,6 I want to insert a 7 between the 4 and the 10 What do I need to do? What about deleting an element from the array ? Ms. Manal Al-Asmari

Need for Linked list We need an alternative structure that stores elements in a sequence but allows for more efficient insertion and deletion of elements at random positions in the list. In a linked list, elements contain links that reference the successor element in the list. Inserting and deleting an element is a local operation and requires updating only the links adjacent to the element. The other elements in the list are not affected. An Array must shift all elements on the tail whenever a new element enters or exits the list. Ms. Manal Al-Asmari

Arrays vs. Linked lists Arrays have a pre-determined fixed size easy access to any element a[i] in constant time no space overhead S = n x sizeof(element) Linked lists no fixed size; grow one element at a time space overhead each element must store an additional reference S = n x sizeof (element) + n x sizeof(reference) no easy access to i-th element need to hop through all previous elements Ms. Manal Al-Asmari

Linked Lists “Reference-Based Lists” List of items, called nodes The order of the nodes is determined by the address, called the link, stored in each node. Every node (except the last node) contains the address of the next node Components of a node Data: stores the relevant information Link: stores the address of the next node Ms. Manal Al-Asmari

Structure of Linked Lists Figure1: Structure of a node Figure2: Linked list Head or first holds the address of the first node in the list The info part of the node can be either a value of a primitive type or a reference to an object Ms. Manal Al-Asmari

Linked Lists Class Node Represents nodes on a list It has two instance variables * info (of type int, but it can be any other type) * link (of type Node) public class Node { public int info; public Node link; } Ms. Manal Al-Asmari

Linked List: Some properties Ms. Manal Al-Asmari

Linked List: Some properties Ms. Manal Al-Asmari

Linked List: Some properties Ms. Manal Al-Asmari

Linked List: Some properties Ms. Manal Al-Asmari

Traversing a Linked List Basic operations of a linked list that require the link to be traversed: * Search the list for an item * Insert an item in the list * Delete an item from the list You cannot use head to traverse the list * You would lose the nodes of the list *Use another reference variable of the same type as head: current Ms. Manal Al-Asmari

Traversing a Linked List The following code traverses the list current = head; while (current != null) { //Process current current = current.link; } Ms. Manal Al-Asmari

Insertion Consider the following linked list You want to create a new node with info 50 and insert it after p Ms. Manal Al-Asmari

Insertion Ms. Manal Al-Asmari

Insertion The following statements insert the node in the linked list at the required place newNode.link = p.link; p.link = newNode; The sequence of statements to insert the node is very important Ms. Manal Al-Asmari

Insertion Ms. Manal Al-Asmari

Deletion Ms. Manal Al-Asmari

Building a Linked List You can build a list in two ways: forward or backward: Forward manner A new node is always inserted at the end of the linked list Backward manner A new node is always inserted at the beginning of the linked list Ms. Manal Al-Asmari

Types of Linked Lists: Doubly Linked Lists Circular Linked Lists Ms. Manal Al-Asmari

1\ Doubly Linked Lists Linked list in which every node has a next link and a back link. A doubly linked list can be traversed in either direction Ms. Manal Al-Asmari

2\ Circular Linked Lists A linked list in which the last node refers to the first node. It is convenient to make first point to the last node. Ms. Manal Al-Asmari

THE END Ms. Manal Al-Asmari