CS212D: Data Structures Week 5-6 Linked List.

Slides:



Advertisements
Similar presentations
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Linked Lists Chapter 4.
Queues and Linked Lists
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Data Structures: A Pseudocode Approach with C
Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
CSC 212 – Data Structures Lecture 13: Linked Lists.
Fall 2006CSC311: Data Structures1 Chapter 3 Arrays, Linked Lists, and Recursion Objectives –Using Arrays –Singly Linked Lists –Doubly Linked Lists –Circularly.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Chapter 5 Linked Lists II
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 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Recursion1 © 2013 Goodrich, Tamassia, Goldwasser.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked List, Stacks Queues
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
COMP9024: Data Structures and Algorithms
Lecture 6 of Computer Science II
Elementary Data Structures
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
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.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked List Stacks, Linked List Queues, Dequeues
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Data Structures Linked list.
CS212D: Data Structures Week 5-6 Linked List.
LINKED LISTS CSCD Linked Lists.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Recall Brute Force as a Problem Solving Technique
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Programming with Recursion
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Problem Understanding
Presentation transcript:

CS212D: Data Structures Week 5-6 Linked List

Outline Singly Linked Lists Doubly Linked Lists Recursions

Data Structures and Algorithms A data structure is a way of storing data in a computer so that it can be used efficiently. Algorithms An algorithm is a finite sequence of well-defined instructions for solving a problem and guaranteed to terminate in a finite time. An algorithm does not necessarily need to be executable in a computer, but can be converted into a program.

Algorithms versus Heuristics A heuristic is a finite sequence of well-defined instructions for partially solving a hard problem and guaranteed to terminate in a finite time. A heuristic is not always guaranteed to find a solution while an algorithm is. Descriptions of algorithms We will use Java-like pseudo code mixed with English to describe algorithms.

Abstract Data Types An Abstract Data Type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations. The definition can be mathematical, or it can be programmed as an interface. We need to implement an ADT by using a class to make it usable.

Linked Lists

Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem  A B C D

The Node Class for List Nodes /** Node of a singly linked list of strings. */ public class Node { private String element; // we assume elements are character strings private Node next; /** Creates a node with the given element and next node. */ public Node(String s, Node n) { element = s; next = n; } /** Returns the element of this node. */ public String getElement() { return element; } /** Returns the next node of this node. */ public Node getNext() { return next; } // Modifier methods: /** Sets the element of this node. */ public void setElement(String newElem) { element = newElem; } /** Sets the next node of this node. */ public void setNext(Node newNext) { next = newNext; } }

The Class for a Singly Linked List /** Singly linked list .*/ public class SLinkedList { protected Node head; // head node of the list protected long size; // number of nodes in the list /** Default constructor that creates an empty list */ public SLinkedList() { head = null; size = 0; } // ... update and search methods would go here ...

Inserting at the Head (1/2) Allocate a new node Insert the new element Have the new node point to old head Update head to point to the new node

Inserting at the Head (2/2) Algorithm addFirst(v) { v.setNext(head); // make v point to the old head node head = v; // make head point to the new node size =size+1; // increment the node count }

Removing at the Head (1/2) Update head to point to next node in the list Allow garbage collector to reclaim the former first node

Removing at the Head (2/2) Algorithm removeFirst() { if ( head = null ) Indicate an error: the list is empty; t = head ; /* make head point to the next node (or null) */ head = head.getNext(); t.setNext(null); //null out the next pointer of the removed node size = size - 1; //decrement the node count }

Inserting at the Tail (1/2) Allocate a new node Insert the new element Have the new node point to null Have the old last node point to new node Update tail to point to new node

Inserting at the Tail (2/2) Algorithm addLast(v) { v.setNext(null); //make the new node v point to null object tail.setNext(v); //make the old tail node point to the new node tail = v ; //make tail point to the new node size = size +1; //increment the node count }

Removing at the Tail (1/2) Removing the tail node of a singly linked list cannot be done in constant time. We need to traverse the whole linked list to remove the tail node, which takes O(n) time, where n is the number of nodes in the linked list.

Removing at the Tail (2/2) Algorithm removeLast() { if ( size = 0 ) Indicate an error: the list is empty; else { if ( size = 1 ) // only one node head = null; else // at least two nodes { t = head; /* make t point to the head */ while ( t.getNext != null ) /* find the last node */ { s = t; t = t.getNext(); } s.setNext(null); //null out the next pointer of the last node } size = size - 1; //decrement the node count

Doubly Linked List A doubly linked list is a concrete data structure consisting of a sequence of nodes. Each node stores: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements

The Node Class of Doubly Linked List /** Node of a doubly linked list of strings */ public class DNode { protected String element; // String element stored by a node protected DNode next, prev; // Pointers to next and previous nodes /** Constructor that creates a node with given fields */ public DNode(String e, DNode p, DNode n) { element = e; prev = p; next = n; } /** Returns the element of this node */ public String getElement() { return element; } /** Returns the previous node of this node */ public DNode getPrev() { return prev; } /** Returns the next node of this node */ public DNode getNext() { return next; } /** Sets the element of this node */ public void setElement(String newElem) { element = newElem; } /** Sets the previous node of this node */ public void setPrev(DNode newPrev) { prev = newPrev; } /** Sets the next node of this node */ public void setNext(DNode newNext) { next = newNext; } }

The double linked list Class public class DLinkList { Protected DNode header; // reference to the head of the link list Protected DNode trailer; // reference to the end of the link list Protected long size; // number of the nodes in the link list // No arguments Constructor public DLinkList() { header = new DNode(" ", null, null); // create a dummy node to point to the beginning of the list trailer = new DNode(" ", null, header); // create a dummy node to point to the end of the list header.setNext(trailer); trailer.setPrev(header); size = 0; } . . . all the other methods } // end of the Class 5-Jul-18 Computer Science Department

Inserting at the Head (1/3) header trailer Rome Seattle New York (a) Before the insertion header trailer Sydney Rome Seattle New York (b) After the insertion

Inserting at the Head (2/3) Algorithm addFirst(v) { w = header.getNext(); // make w point to the first node v.setNext(w); // make v point to the old first node v.setPrev(header); // make v point to the header w.setPrev(v); // make the old first node point to v header.setNext(v); // make header point to v size = size+1; // increment the node count }

Inserting at the Head (3/3) // Java method to insert Node at the head public void addFirst (DNode v) { DNode w; w = header.getNext(); // make w point to the first node v.setNext(w); // make v point to the old first node v.setPrev(header); // make v point to the header header.setNext(v); // make header point to v w.setPrev(v); // make the old first node point to v size = size + 1; // increment the node count } 5-Jul-18 Computer Science Department

Inserting in the Middle (1/3) header trailer Rome Seattle New York (a) Before the insertion header trailer Rome Seattle Sydney New York (b) After the insertion

Inserting in the Middle (2/2) Algorithm addAfter(v,z) { w=v.getNext(); // make w point to the node after v z.setPrev(v); // link z to its predecessor, v z.setNext(w); // link z to its successor, w w.setPrev(z); // link w to its new predecessor, z v.setNext(z); // link v to its new successor, z size = size+1; // increment the node count }

Inserting in the Middle (3/3) //Java method to insert a node in the middle public void addAfter(DNode afterNode, DNode newNode) { DNode w; w = afterNode.getNext(); newNode.setPrev(afterNode); newNode.setNext(w); w.setPrev(newNode); afterNode.setNext(newNode); size = size + 1; } 5-Jul-18 Computer Science Department

Removing at the Tail (1/3) header trailer Rome Seattle Sydney New York (a) Before the deletion header trailer Rome Seattle Sydney (b) After the deletion

Removing at the Tail (2/3) Algorithm removeLast(): { if ( size = 0 ) Indicate an error: this list is empty; v = trailer.getPrev() // make v point to the last node u = v.getPrev(); // u points to the node before the last node trailer.setPrev(u); u.setNext(trailer); v.setPrev(null); v.setNext(null); size = size -1; }

Removing at the Tail (3/3) public void removeLast() { if (size == 0) { System.out.print("no nodes to delete"); } else { DNode lastNode; DNode beforLastNode; lastNode = trailer.getPrev(); beforLastNode = lastNode.getPrev(); trailer.setPrev(beforLastNode); beforLastNode.setNext(trailer); lastNode.setNext(null); lastNode.setPrev(null); size = size - 1; } // end of the method 5-Jul-18 Computer Science Department

Removing in the Middle (1/3) header trailer Rome Seattle Sydney New York (a) Before the deletion header trailer Rome Seattle New York (b) After the deletion

Removing in the Middle (2/3) Algorithm remove(v): { u = v.getPrev() // make u point to the node before v w = v.getNext(); // w points to the node after v w.setPrev(u); // link out v u.setNext(w); v.setPrev(null); // null out the pointers of v v.setNext(null); size = size -1; // decrement the node count }

Removing in the Middle (3/3) public void remove(DNode v) { DNode u, w; u = v.getPrev(); // make u point to the node before v w = v.getNext(); // w points to the node after v w.setPrev(u); // link out v u.setNext(w); v.setPrev(null); // null out the pointers of v v.setNext(null); size = size - 1; // decrement the node count } 5-Jul-18 Computer Science Department

The double linked list Class (completed) public class DLinkList { Protected DNode header; // reference to the head of the link list Protected DNode trailer; // reference to the end of the link list Protected long size; // number of the nodes in the link list // No arguments Constructor public DLinkList() { header = new DNode(" ", null, null); // create a dummy node to point to the beginning of the list trailer = new DNode(" ", null, header); // create a dummy node to point to the end of the list header.setNext(trailer); trailer.setPrev(header); size = 0; } 5-Jul-18 Computer Science Department

} //end of the class // insert Node at the head public void addFirst(DNode v) { DNode w; w = header.getNext(); // make w point to the first node v.setNext(w); // make v point to the old first node v.setPrev(header); // make v point to the header header.setNext(v); // make header point to v w.setPrev(v); // make the old first node point to v size = size + 1; // increment the node count } //insert a nodein the middle public void addAfter(DNode afterNode, DNode newNode) { w = afterNode.getNext(); newNode.setPrev(afterNode); newNode.setNext(w); w.setPrev(newNode); afterNode.setNext(newNode); size = size + 1; // print the elemtns in Double linked list public void printElement() { DNode current = header; while (current != null) { System.out.println(" " + current.getElement()); current = current.getNext(); }// end of method // remove from the tail public void removeLast() { if (size == 0) { System.out.print("no nodes to delete"); } else { DNode lastNode; DNode beforLastNode; lastNode = trailer.getPrev(); beforLastNode = lastNode.getPrev(); trailer.setPrev(beforLastNode); beforLastNode.setNext(trailer); lastNode.setNext(null); lastNode.setPrev(null); size = size - 1; } //Removing in the Middle public void remove(DNode v) { DNode u, w; u = v.getPrev(); // make u point to the node before v w = v.getNext(); // w points to the node after v w.setPrev(u); // link out v u.setNext(w); v.setPrev(null); // null out the pointers of v v.setNext(null); size = size - 1; // decrement the node count } //end of the class 5-Jul-18 Computer Science Department

The DoubleLinkList Class (the main) package doublelinklist; public class DoubleLinkList { public static void main(String[] args) { // Create Dnodes objects. DNode dNode1 = new DNode("Riyadh", null, null); DNode dNode2 = new DNode("Dammam", null, null); DNode dNode3 = new DNode("Jeddah", null, null); DNode dNode4 = new DNode("Makah", null, null); //Create a double linked list DLinkList theDList = new DLinkList(); System.out.println("The size before adding Nodes: " + theDList.size); System.out.println("-------------------------------------"); // call the method addFirst to add each node at the beginning of the list theDList.addFirst(dNode1); theDList.addFirst(dNode2); theDList.addFirst(dNode3); theDList.addFirst(dNode4); // this node should be the first node in the list // call the method to print the elements in the node // it should print Makah first since it was added at the beginng of the list 5-Jul-18 Computer Science Department

theDList.printElement(); System.out.println("After adding the elements using addFirst method"); theDList.printElement(); System.out.println("The Size is : " + theDList.size); System.out.println("------------------"); theDList.removeLast(); // call the method that removes the last node System.out.println("Prints the elements after deleting the last node"); // print the elements after deleting the last node System.out.println("the Size is : " + theDList.size); DNode dNode5 = new DNode("Tabuk", null, null); theDList.addAfter(dNode3, dNode5); // add the dNode5 after DNode3 (add node in the middle) System.out.print(" Tabuk has been added after Jeddah"); // delete a node at the middel theDList.remove(dNode5); System.out.print(" Print the element after deleting Tabuk "); System.out.println("------------------"); } } // end of the class 5-Jul-18 Computer Science Department

Computer Science Department The size before adding Nodes: 0 ------------------------------------ After adding the elements using addFirst method Riyadh Dammam Jeddah Makah The Size is :4 ------------------ Prints the elements after deleting the last node the Size is :3 Tabuk has been added after Jeddah Tabuk Print the element after deleting Tabuk 5-Jul-18 Computer Science Department

Algorithm for Multiple Recursion Algorithm PuzzleSolve(k,S,U) { Input: An integer k, sequence S, and set U (the universe of elements to test) Output: An enumeration of all k-length extensions to S using elements in U without repetitions for all e in U do Remove e from U; // e is now being used Add e to the end of S; if (k = 1) Test whether S is a configuration that solves the puzzle; if ( S solves the puzzle ) return “Solution found: ” S; else PuzzleSolve(k - 1, S,U); Add e back to U; // e is now unused Remove e from the end of S; }

Visualizing PuzzleSolve ( 3 , () ,{ a b c } ) Initial call 2 1 ab ac cb ca bc ba abc acb bac bca cab cba

References Chapter 3, Data Structures and Algorithms by Goodrich and Tamassia. Garbage Collection by Bill Venners (http://www.artima.com/insidejvm/ed2/gc.html).