CS 367 – Introduction to Data Structures

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structures ADT List
Data Structure HKOI training /4/2010 So Pak Yeung.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Data Structure Lecture-5
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about 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.
Data Structures: A Pseudocode Approach with C
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
Lists: array implementation list_size = 5 lst Obj 1Obj 2Obj 3Obj 4Obj 5.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Binary Trees – Part I CS 367 – Introduction to Data Structures.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Linked List by Chapter 5 Linked List by
Subject Name : Data Structure Using C Title : Linked Lists
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.
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
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked Lists CS 367 – Introduction to Data Structures.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Programming Circular Linked List.
Linked Lists & Hash Tables
Doubly Linked List Review - We are writing this code
Algorithm for deleting a node from a singly linked list
EEL 4854 IT Data Structures Linked Lists
Java collections library
Linked list insertion Head. Linked list insertion Head.
LINKED LISTS CSCD Linked Lists.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Data Structures ADT List
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
LINKED LISTS.
Data Structures ADT List
CS148 Introduction to Programming II
Programming II (CS300) Chapter 07: Linked Lists
More on Linked List Yumei Huo Department of Computer Science
Linked Lists.
Presentation transcript:

CS 367 – Introduction to Data Structures Linked Lists – Part II CS 367 – Introduction to Data Structures

Adding a Tail Reference Up to now, we have only considered our lists to have a head reference having a tail reference makes adding to the end of the list much more efficient constant time: O(1) however, a tail reference makes things more complex must make sure that head and tail refer to null when the list is empty must make sure head and tail point to the same node when only one node in the list

Add/Delete Tail public void addTail(Object data) { Node tmp = new Node(data, null); // empty list? if(tail == null) { head = tail = tmp; } else { tail.next = tmp; tail = tmp; } } public Object deleteTail() { if(tail == null) { return null; } // empty list. // get the tail and a reference to the previous node Node cur, prev = null; for(Node cur = head; cur != tail; prev = cur, cur = cur.next); tail = prev; if(tail == null) { head = null; } // list is now empty else { tail.next = null; } return cur;

Problem Adding to tail: O(1) Deleteing from tail: O(n) this is because the list can only be searched in one direction from head to tail have to search entire list to find the node prior to the tail so that its next reference can be adjusted

Doubly Linked List Solution is to include a previous and a next pointer in a node previous: refers to node immediately prior next: refers to node immediately after this is called a doubly linked list Now it is possible to search in both directions also makes removing the tail O(1)

Doubly Linked List Head Tail John Smith Jane Doe Pat Thomas 87.45 next next next previous previous previous

Doubly Linked List Node A node now looks slightly different class Node { public Object data; public Node previous; public Node next; public Node(Object data, Node previous, Node next) { this.data = data; this.previous = previous; this.next = next; } The linked list we will consider will have a head and a tail pointer

Inserting at the tail Inserting at the tail is almost identical it is slightly more complicated one more reference to deal with public void insertTail(Object data) { tail = new Node(data, tail, null); // is this the only node in the list? if(tail.previous == null) { head = tail; } else { tail.previous.next = tail; } }

Removing Tail of List Removing a node at the tail is now quick public Object deleteTail() { if(tail == null) { return null; } // empty list Node tmp = tail; tail = tail.previous; if(tail != null) { tail.next = null; } else { head = null; } return tmp.data; } Notice that this operation is O(1) now

Adding a Node public void add(Object data) { Node tmp = new Node(data, null, null); if(head == null) { head = tail = tmp; } // list was empty // find where the node goes else { Node cur; while((cur != null) && (((Comparable)cur.getData()).compareTo(data) <= 0)) cur = cur.next; tmp.setNext(cur); if(cur != null) { tmp.setPrev(cur.getPrev()); cur.setPrev(tmp); if(tmp.getPrev() != null) { tmp.getPrev().setNext(tmp); } else { head = tmp; } } else { tmp.setPrev(tail); tail.setNext(tmp); tail = tmp; }

Adding a Node Remember, must consider the following cases empty list adding to head adding to tail adding to middle of the list Obviously, adding to the middle of a doubly linked list is more sophisticated

Appropriateness When should you use a doubly linked list? depends on your application if you are routinely deleting from tail if you know where the desired node is may be faster to search from the rear Anything you can do with a singly linked list can be done with a doubly linked list reverse is not true

Circular Lists May want a list to form a ring (why?) every node has a successor if it is a doubly linked circular list, every node has a successor and a predecessor no head or tail only need to keep track of current node if only one node in the list, its successor is itself current.next = current if the list is empty, current refers to null

Circular List Empty list. One node in list. Multiple nodes in list. current Empty list. current data One node in list. next current Multiple nodes in list. data data data next next next

Inserting a Node While there is no head or tail, we do have a concept of the beginning and end of list beginning: what current is referencing end: node immediately prior to current When using a circular list, usually does not matter much about the ordering so usually insert immediately after or before the current node

Inserting a Node After Current public void insert(Object data) { Node tmp = new Node(data, null); // check if this is the only node in the list if(current == null) { tmp.setNext(tmp); current = tmp; } else { tmp.setNext(current.getNext()); current.setNext(tmp);

Inserting Node Before Current In a singly linked list, this is more difficult either need to keep track of the tail and do the insert or you need to search from current to the end of the list how do you know when a complete loop has been made around the list? what is the problem with this method?

Removing the Current Node When doing a remove, usually removing the current node why? This can cause some problems if a singly linked list is used to implement circular list have to find and update tail of list requires a search all the way through the list O(n) problem easily solved by using a doubly linked list