Unit 3 Linked Lists King Fahd University of Petroleum & Minerals

Slides:



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

Chapter 3 Lists Dr Zeinab Eid.
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
Linear Lists – Linked List Representation
Data Structures ADT List
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
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.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
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.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
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)
Stacks, Queues & Deques CSC212.
List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
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.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
Algorithms and Data Structures
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
Data Structure & Algorithms
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Advanced Programming 7/10/20161 Ananda Gunawardena.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
IT Department – Bunda Mulia University
Introduction What Is Linked List? (2002 , 2007,2011)
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.
Linked Lists Chapter 5 (continued)
Big-O notation Linked lists
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Mid Term Review Advanced Programming Ananda Gunawardena
Algorithm for deleting a node from a singly linked list
Java collections library
Priority Queue.
LINKED LISTS CSCD Linked Lists.
Sparse Tables A sparse table refers to a table that is populated sparsely by data and most of its cells are empty With a sparse table, the table can be.
Queues.
Data Structures ADT List
Data Structures ADT List
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.
CS212D: Data Structures Week 5-6 Linked List.
ADT list.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
LINKED LISTS.
Data Structures ADT List
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists & Iterators
LAB#4 Linked List.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
More on Linked List Yumei Huo Department of Computer Science
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Unit 3 Linked Lists King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department Unit 3 Linked Lists

Reading Assignment “Data Structures and Algorithms in Java”, 3rd Edition, Adam Drozdek, Thomson Learning, ISBN 0-534-49252-5. Chapter 3 (Sections 1 – 3) Sections 3.4-3.9 are not included.

Objectives Discuss the following topics: Singly Linked Lists Doubly Linked Lists Circular Lists

Singly Linked Lists A linked structure is a collection of nodes storing data and links to other nodes A linked list is a data structure composed of nodes, each node holding some information and a reference to another node in the list A singly linked list is a node that has a link only to its successor in this sequence

Singly Linked Lists (continued) Figure 3-1 A singly linked list

Singly Linked Lists (continued) Figure 3-1 A singly linked list (continued)

A Node in a Singly Linked List //*********** SLLNode.java ***************** // node in a generic singly linked list class public class SLLNode<T> { public T info; public SLLNode<T> next; public SLLNode() { this(null,null); } public SLLNode(T el) { this(el, null); public SLLNode(T el, SLLNode<T> ptr) { info = el; next = ptr;

A List in a Singly Linked List //**************** SLL.java ******************* // a generic singly linked list class public class SLL<T> { protected SLLNode<T> head, tail; public SLL() { head = tail = null; } public boolean isEmpty() { return head == null;

Singly Linked List Visualization Figure 3-3 A singly linked list of integers

Insertion into Singly Linked Lists In the worst case, this operation is in O( ). Figure 3-4 Inserting a new node at the beginning of a singly linked list

Insertion into Singly Linked Lists (cont.) In the worst case, this operation is in O( ). Figure 3-5 Inserting a new node at the end of a singly linked list

Insertion into Singly Linked Lists (cont.) public void addToHead(T el) { head = new SLLNode<T>(el,head); if (tail == null) tail = head; } public void addToTail(T el) { if (!isEmpty()) { tail.next = new SLLNode<T>(el); tail = tail.next; else head = tail = new SLLNode<T>(el);

Deletion from Singly Linked Lists In the worst case, this operation is in O( ). Figure 3-6 Deleting a node from the beginning of a singly linked list

Deletion from Singly Linked Lists (cont.) In the worst case, this operation is in O( ). Figure 3-7 Deleting a node from the end of a singly linked list

Deletion from Singly Linked Lists (cont.) public T deleteFromHead() { // delete head and return its // info; if (isEmpty()) return null; T el = head.info; if (head == tail) // if only one node on the list; head = tail = null; else head = head.next; return el; }

Deletion from Singly Linked Lists (cont.) public T deleteFromTail() { // delete tail, return its info; if (isEmpty()) return null; T el = tail.info; if (head == tail) // if only one node in list; head = tail = null; else { // if more than one node in list, SLLNode<T> tmp; // find predecessor of tail; for (tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; // predecessor of tail becomes tail; tail.next = null; } return el;

Deletion from Singly Linked Lists (cont.) public void delete(T el) { // delete the node with element el; if (!isEmpty()) if (head == tail && el.equals(head.info)) // if only one head = tail = null; // node on the list; else if (el.equals(head.info)) // if > one node on list; head = head.next; // and el is in the head node; else { // if more than one node in list SLLNode<T> pred, tmp;// and el is in a nonhead node; for (pred = head, tmp = head.next; tmp != null && !tmp.info.equals(el); pred = pred.next, tmp = tmp.next); if (tmp != null) { // if el was found; pred.next = tmp.next; if (tmp == tail) // if el is in the last node; tail = pred; }

Printing a Singly Linked List and Checking an Element in a List public void printAll() { for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next) System.out.print(tmp.info + " "); } public boolean isInList(T el) { SLLNode<T> tmp; for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next); return tmp != null;

Doubly Linked Lists A doubly linked list is when each node in a linked list has two reference fields, one to the successor and one to the predecessor Figure 3-9 A doubly linked list

A Doubly Linked List Node //****************** DLLNode.java ********************* // node of generic doubly linked list class public class DLLNode<T> { public T info; public DLLNode<T> next, prev; public DLLNode() { next = null; prev = null; } public DLLNode(T el) { info = el; next = null; prev = null; public DLLNode(T el, DLLNode<T> n, DLLNode<T> p) { info = el; next = n; prev = p;

A Doubly Linked List //****************** DLL.java ********************* // generic doubly linked list class public class DLL<T> { private DLLNode<T> head, tail; public DLL() { head = tail = null; } public boolean isEmpty() { return head == null; public void setToNull() {

Insertion into Doubly Linked Lists Figure 3-11 Adding a new node at the end of a doubly linked list

Insertion into Doubly Linked Lists (cont.) In the worst case, this operation is in O( ) Figure 3-11 Adding a new node at the end of a doubly linked list (continued)

Insertion into Doubly Linked Lists (cont.) public void addToTail(T el) { if (tail != null) { tail = new DLLNode<T>(el,null,tail); tail.prev.next = tail; } else head = tail = new DLLNode<T>(el);

Deletion from Doubly Linked Lists Figure 3-12 Deleting a node from the end of a doubly linked list

Deletion from Doubly Linked Lists (cont.) public T deleteFromTail() { if (isEmpty()) return null; T el = tail.info; if (head == tail) // if only one node on the list; head = tail = null; else { // if more than one node in list; tail = tail.prev; tail.next = null; } return el;

Circular Lists A circular list is when nodes form a ring: The list is finite and each node has a successor Figure 3-13 A circular singly linked list

Circular Lists (continued) Figure 3-14 Inserting nodes at the front of a circular singly linked list (a) and at its end (b)

Circular Lists (continued) Figure 3-15 A circular doubly linked list

Summary A linked structure is a collection of nodes storing data and links to other nodes. A linked list is a data structure composed of nodes, each node holding some information and a reference to another node in the list. A node in a singly linked list has a link only to its successor in this sequence. A node in a doubly linked list has links to its successor and predecessor in this sequence. A circular list is when nodes form a ring: The list is finite and each node has a successor. The advantage of arrays over linked lists is that they allow random accessing.