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.

Slides:



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

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
Linked Lists Chapter 4.
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.
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Double-Linked Lists and Circular Lists
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
The ArrayList Class and the enum Keyword
The List ADT Textbook Sections
Data Structure Lecture-5
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
M180: Data Structures & Algorithms in Java
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Doubly Linked Lists Representation Space Analysis Creation and Insertion Traversal Deletion.
Doubly Linked Lists Doubly Linked Lists: Introduction. Doubly Linked Lists: Implementation Doubly Linked Lists: Analysis Doubly Linked Lists: Creation.
Queues What are queues? Queue Implementation Using Linked Lists. Applications of Queues.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Singly Linked Lists Singly Linked Lists: Introduction. Singly Linked Lists: Implementation. Singly Linked Lists: Analysis. Singly Linked Lists: Creation.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Chapter 7 Stacks II CS Data Structures I COSC 2006
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
LinkedList Many slides from Horstmann modified by Dr V.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
CS 367 Introduction to Data Structures Lecture 5.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Java linked list.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Linked Lists CS 367 – Introduction to Data Structures.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Class 2 – Recursion on Lists
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Singly Linked Lists.
Doubly Linked List Review - We are writing this code
Linked Lists Damian Gordon.
Activity Write the code fragment to reverse a singly linked list
Stack and Queue APURBO DATTA.
Java collections library
Priority Queue.
Doubly linked lists.
8-1.
8-1.
Foundational Data Structures
Initializing Objects.
Queues: Implemented using Arrays
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Computer Science and Engineering
Queues CSC212.
Singly Linked Lists Representation Space Analysis
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Activity Write the code fragment to reverse a singly linked list
Queues: Implemented using Linked Lists
Doubly Linked Lists Representation Space Analysis
Chapter 16-3 Linked Structures
Presentation transcript:

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

class LinkedList { protected Element head; protected Element tail; public LinkedList () // constructor for empty list { } public final class Element {Object datum; Element next; Element (Object datum, Element next) { this.datum = datum; this.next = next;} public Object getDatum () //to return element { return datum; } public Element getNext () //to get next element { return next; } Java Class

public void insertAfter (Object item) { // to insert after element next = new Element (item, next); if (tail == this) tail = next; } public void insertBefore (Object item) { //to insert before element Element tmp = new Element (item, this); if (this == head) head = tmp; else { Element prevPtr = head; while (prevPtr != null && prevPtr.next != this) prevPtr = prevPtr.next; prevPtr.next = tmp; } } } public void purge () { //to clear the list head = null; tail = null; }

public Element getHead () { // to return head return head; } public Element getTail () { // to return tail return tail; } public boolean isEmpty () { // to check the item in the list return head == null; } public Object getFirst () { // to return head or first element return head.datum; } public Object getLast () { // to return tail or last element return tail.datum; } public Element getPtr(Object item) { //return ptr for (Element ptr = head; ptr != null;ptr = ptr.next) if (ptr.datum == item) return ptr; return null; }

public void prepend (Object item) // to add element at the fist of list {Element tmp = new Element (item, head); if (head == null) tail = tmp; head = tmp; } public void append (Object item) // to add element at the end of list {Element tmp = new Element (item, null); if (head == null) head = tmp; else tail.next = tmp; tail = tmp; } public void assign (LinkedList list) //to assign element of list to another list {if (list != this) { purge (); for (Element ptr = list.head; ptr != null; ptr = ptr.next) append (ptr.datum); }}

public void extract (Object item) // to delet the element for list {Element ptr = head; Element prevPtr = null; while (ptr != null && ptr.datum != item){ prevPtr = ptr; ptr = ptr.next;} if (ptr == null) throw new IllegalArgumentException ("item not found"); if (ptr == head) head = ptr.next; else prevPtr.next = ptr.next; if (ptr == tail) tail = prevPtr; } public void print (LinkedList list) // used to print the element of linked list {if (isEmpty()) System.out.println("The list is empty"); else{ for (Element ptr = list.head; ptr != null; ptr = ptr.next) System.out.print(ptr.datum + " "); System.out.println(); }}}

class LinkedListTest { public static void main(String [] args) { // create a new empty list l System.out.println("The head of the list is: " + l.getHead()); // print the tail // check if the list empty or not l.print(l); l.prepend(30); // add 20 and 10 at the first of list // print the list // add 40 and 50 at the last of list l.print(l); // print the first element System.out.println(l.getLast() + "");

// delet 30 from the list and print the list // create a new empty list l1 // assign l to l1 // print l1 l1.getPtr(10).insertBefore(100); l1.print(l1); // insert 100 after 50 and print the list l1.getHead().insertBefore("xxx"); … // insert ("xxx") after the tail of the list l1.print(l1); }}