Data Structures: Linked Lists

Slides:



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

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists Linear collections.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 20 – Data Structures Outline 20.1 Introduction 20.2 Self-Referential Classes 20.3 Dynamic Memory.
1 Lecture 25 Abstract Data Types –III Overview  Basic Node Design  Example: The Dynamic Phone List  Manipulating the Phone List  Inserting Nodes into.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 Pearson Education, Inc. All rights reserved Data Structures.
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
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.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Java How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Section 3.7 Linked-Based Implementations
Linked Data Structures
Stack: a Linked Implementation
The List ADT.
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Java Methods Lists and Iterators Object-Oriented Programming
17 Data Structures.
5.13 Recursion Recursive functions Functions that call themselves
Copy Constructor / Destructors Stacks and Queues
CS2006- Data Structures I Chapter 5 Linked Lists I.
Chapter 22 Custom Generic Data Structures
CISC181 Introduction to Computer Science Dr
Stacks.
Stack and Queue APURBO DATTA.
public class StrangeObject { String name; StrangeObject other; }
8-1.
Top Ten Words that Almost Rhyme with “Peas”
The Singly-Linked Structure
8-1.
Chapter 16-2 Linked Structures
Chapter 19 – Data Structures
Data Structures ADT List
Linked List (Part I) Data structure.
Recitation 5 CS0445 Data Structures
Linked Lists [AJ 15].
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
ADT list.
COSC 1030 Section 8 List.
Queues CSC212.
Lecture 14 Linked Lists CSE /26/2018.
Linked Lists Chapter 5 (continued)
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists Chapter 5 (continued)
21 Data Structures.
Presentation transcript:

Data Structures: Linked Lists Chapter 6 Data Structures: Linked Lists CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Ahmad Al-Rjoub

1. Objectives After you have read and studied this chapter, you should be able to: Understand the concept of a dynamic data structure. Be able to create and use dynamic data structures such as linked lists. Understand the stack and queue ADTs. Various important applications of linked data structures. Know how to use inheritance to define extensible data structures. Create reusable data structures with classes, inheritance and composition.

2. Self-Referential Classes: Definition Contains an instance variable that refers to another object of the same class type That instance variable is called a link A null reference indicates that the link does not refer to another object

2. Self-Referential Classes (cont) A link to another Node object. Node<T> T Node (in o: T) setData(in o: T) getData(): T setNext(in link: Node<T>) getNext(): Node<T> T data 1 next Node<T> Node (in o: T) setData(in o: T) getData(): T setNext(in link: Node<T>) getNext(): Node<T>

Basic Node: The Generic Node Class A node in a linked list contains data elements and link elements. Student name: String id: int grade: double + student() + set(…) + get()… Node<T> T Node (in o: T) setData(in o: T) getData(): T setNext(in link: Node<T>) getNext(): Node<T> Ali Jamel Kamal

Generic Node Class: Implementation public class Node<T> { private T data; // Stores any kind of data private Node<T> next; public Node<T>(T obj) { data = obj; next = null; } public void setNext( Node<T> nextPtr ) { next = nextPtr; public Node<T> getNext() { return next; public void setData(T obj) { public T getData() { return data; public String toString() { return data.toString();

Connecting two nodes S2 S1 The statements Student S1 = new Student(“Ali”, 42700000, 3.5) Student S2 = new Student(“Ahmed”, 4271000, 3.9) Node <Student> p = new Node <Student>(S1); Node <Student> q = new Node(S2); allocate storage for two objects of type Node referenced by p and q. The node referenced by p stores the object S1, and the node referenced by q stores the object S2. The next fields of both nodes are null. Nodes referenced by p and q S1 S2

Connecting two nodes (Cont) The statement p.setNext (q); stores the address of node q in the link field of node p, thereby connecting node p to node q, and forming a linked list with 2 nodes. The diagonal line in the next field of the second list node indicates the value null. Linked list with two nodes S1 S2

Linked Lists: Definition Linear collection of nodes A linked list is based on the concept of a self-referential object -- an object that refers to an object of the same class. A program typically accesses a linked list via a reference to the first node in the list A program accesses each subsequent node via the link reference stored in the previous node Are dynamic The length of a list can increase or decrease as necessary Become full only when the system has insufficient memory to satisfy dynamic storage allocation requests

Generic Linked Lists List<T> name: String List () List(n: String) insertAtFront(o: T) insertAtBack(o: T) removeFromFront():T removeFromBack():T isEmpty(): Boolean size(): int insertAfter(int, T) insertBefore(int, T) remove(int):T T data 1 next Node<T> Node (in o: T) setData(in o: T) getData(): T setNext(in link: Node<T>) getNext(): Node<T> 1 first

Implementation of the Generic Linked Lists public class List<T> { private Node<T> first; private String name; public List() { name=“ “; first=null; } public List(String listName ) { name = listName; first = null; ……

Linked List: insertAtFront Method insertAtFront’s steps Call isEmpty to determine whether the list is empty If the list is empty, assign firstNode and lastNode to the new ListNode that was initialized with insertItem The ListNode constructor call sets data to refer to the insertItem passed as an argument and sets reference nextNode to null If the list is not empty, set firstNode to a new ListNode object and initialize that object with insertItem and firstNode The ListNode constructor call sets data to refer to the insertItem passed as an argument and sets reference nextNode to the ListNode passed as argument, which previously was the first node

Linked List: insertAtFront (Cont) Graphical representation of operation insertAtFront

Linked List: insertAtFront (Cont) Code of insertAtFront public void insertAtFront(T obj) { Node N = new Node(obj); N.setNext(first); first = N; } // insertAtFront()

Linked List: insertAtBack Method insertAtBack’s steps Call isEmpty to determine whether the list is empty If the list is empty, assign firstNode and lastNode to the new ListNode that was initialized with insertItem The ListNode constructor call sets data to refer to the insertItem passed as an argument and sets reference nextNode to null If the list is not empty, assign to lastNode and lastNode.nextNode the reference to the new ListNode that was initialized with insertItem The ListNode constructor sets data to refer to the insertItem passed as an argument and sets reference nextNode to null

Linked List: insertAtBack (Cont) Graphical representation of operation insertAtBack.

Linked List: insertAtBack (Cont) public void insertAtBack(T obj) { if (isEmpty()) first = new Node(obj); else { Node current = first; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(new Node(obj)); // Insert the newObj } } // insertAtRear Solution 1 public void insertAtBack(T obj) { Node N =new Node(obj); if (isEmpty()) first = N; else { Node current = first; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(N)); // Insert the newObj } } // insertAtRear Solution 2

Linked List: removeFromFront Method removeFromFront’s steps Throw an EmptyListException if the list is empty Assign firstNode.data to reference removedItem If firstNode and lastNode refer to the same object, set firstNode and lastNode to null If the list has more than one node, assign the value of firstNode.nextNode to firstNode Return the removedItem reference

Linked List: removeFromFront Graphical representation of operation removeFromFront.

Linked List: removeFromFront Code of removeFromFront public T removeFromFrontt() { if (isEmpty()) return null; Node N = first; // T d = first.getdata(); first = first.getNext(); return N.getData(); // return d; }

Linked List: removeFromBack Method removeFromBack’s steps Throws an EmptyListException if the list is empty Assign lastNode.data to removedItem If the firstNode and lastNode refer to the same object, set firstNode and lastNode to null If the list has more than one node, create the ListNode reference current and assign it firstNode “Walk the list” with current until it references the node before the last node The while loop assigns current.nextNode to current as long as current.nextNode is not lastNode

Linked List: removeFromBack Graphical representation of operation removeFromBack.

Linked List: removeFromBack Code of removeFromBack public T removeFromBack() { if (isEmpty()) // Empty list return null; Node current = first; if (current.getNext() == null) { // Singleton list first = null; return current.getData(); } Node previous = null; // All other cases while (current.getNext() != null) { previous = current; current = current.getNext(); previous.setNext(null); } // removeLast()

Generic Linked Lists and Interface data 1 next Node<T> Node (in o: T) setData(in o: T) getData(): T setNext(in link: Node<T>) getNext(): Node<T> List<T> name: String List () List(n: String) displayAll() countSimilar(T): int search(T): int remove(int): T remove(T):T Intersection(List<T>): List<T> 1 first

Generic Linked Lists and Interface Code of displayAll() public void displayAll() { if (!(isEmpty())) { Node current = first; while (current!= null) current.getData().display(); current=current.getNext(); } <Interface> AAAA display() This method must be implemented by each substitute class of the generic type. We add an Interface to our package containing this method “display” and each substitution’s class of the generic type must implement the Interface.

Generic Linked Lists and Interface Code of countSimilar(T):int public int count(T t) { Node current = first; int x=0; while (current!= null) { if (current.getData().compare(t)) x++; current=current.getNext(); } return x; <Interface> AAAA display() compare(Object): Boolean This method must be implemented by each substitute class of the generic type. We add an Interface to our package containing this method “compare” and each substitution’s class of the generic type must implement the Interface.

Generic Linked Lists and Interface Code of search(T):int public int search(T t) { Node current = first; int x=0; while (current!= null) { x++; if (current.getData().compare(t)) return x; current=current.getNext(); } return -1; <Interface> AAAA display() compare(Object): Boolean This method must be implemented by each substitute class of the generic type. We add an Interface to our package containing this method “compare” and each substitution’s class of the generic type must implement the Interface.

Generic Linked Lists and Interface Code of remove(int):T public T remove (int x) { if ((isEmpty()) || (x<0) || (x>size()) return null; if (x==1) return removeFromFront(); if (x==size()) return removeFromBack(); Node current = first; for(int i=1, i<x-1 ; i++) current = current.getNext(); T d=current.getNext().getdata(); current.setNext(current.getNext().getNext()); return d; }

Generic Linked Lists and Interface Code of remove(T):T public int remove(T t) { int x = search(t); return remove(x); }