Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



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

Linked Lists Geletaw S..
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists.
Linked Lists Chapter 4.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
CHP-5 LinkedList.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: 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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
Chapter 17 Linked List.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Data Structures and Algorithms Lecture 7,8 and 9 (Linked List) Instructor: Quratulain Date: 25, 29 September and 2 October, 2009 Faculty of Computer Science,
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
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
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
CSCS-200 Data Structure and Algorithms Lecture
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Chapter 4 Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Chapter 4 Linked Lists.
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.
Chapter 4 Linked Lists.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College

Linked List (review) Each element contains two parts: –Value of this element –Pointer to the next element class string_node { Private: string val; string_node *next; }

Inserting into a Linked List (Review) /* Assume item is a reference to the node that contains “Barry”*/ Node newNode = new Node (“Carol”); newNode.next = aNode.next; aNode.next = newitem;

Printing a Linked List (Review) //print elements, starting at the node called head; // Uses a for loop for(Node current=head; current!=null; current=current.next){ System.out.println(current.data); }

Printing Using Iterator (Review) //prints the list, one item per line //returns iterator pointing before the list… Iterator itr = aList.iterator(); while(itr.hasNext()){ String name = itr.next(); System.out.println(name); }

LinkedList Implementation (Review) public class LinkedList { private Node head; //first element or null private int size; //list length … private static class Node { //inner class private E data; private node next; } //methods go here }

Implementing toString Writes elements of list into a string Use either traversal from before (iterator version shown) String toString(){ StringBuilder result = new StringBuilder(); Iterator itr = iterator(); while(itr.hasNext()){ result.append(itr.next()); result.append(‘\n’); }

Dummy Nodes simplify implementation An empty list has one node (the dummy node) There is no longer a special case for adding the first element of the node! What changes need to be made to… –Constructor? –Find / Retrieve ? –Insert / Delete? Sometimes a dummy node can have a useful value (e.g. smallest possible element for sorted list)

Changes to the implementation for a dummy node Constructor –Head is a new (dummy) node instead of NULL indexOf / get –indexOf is unchanged (assume value not otherwise in list) –get(k) will return value at node (k+1) Insert / Delete –Begin by locating the predecessor node –No special cases for first node (it has a predecessor)

Circular List Circular List: The tail of the list points back to the head There is no NULL pointer to “end” the list.

Issues with circular list How do you know when you’re done? –Make sure you save the head pointer. –When (cur.next == head) you’ve reached the end How are insertion and deletion handled? –No special cases! –Predecessor to head node is the last node in the list.

The Josephus Problem (One of many variations…) The founder of a startup is forced to lay off all but one employee. Not having any better way to decide, he arranges all employees in a circle and has them count off. The 10th employee in the circle is laid off, and the count begins again. The last person is not laid off. If there are N employees, where should you sit to avoid being laid off?

Solution Model the circle of employees as a circular linked list Implement the counting off process, and delete the 10th employee each time After N-1 people are deleted, there should be only one employee left. That employee’s original position number is the solution to the problem.

Doubly Linked List Each node has prev and next pointer List can be traversed forward or backward To insert a node after “cur” –Node tmp = new Node (newItem); –tmp.next = cur.next; –cur.next=tmp; –tmp.prev=cur; –tmp.next.prev = tmp; Reverse the process to delete!

Which list implementation? Array –Can jump into the middle easily (random access) –Inserting & deleting can require time-consuming shifting –Must allocate block of memory at once (can resize later with new) Linked –No random access –Insert & delete are fixed cost, once you decide where. –Nodes allocated one at a time.

Linked List Choices Plain vanilla –Simple, small Dummy header –Eliminates special cases (chance for error) Circular –No defined start position in sequence –Can find the node before, though it takes N steps Doubly linked –Easy to find the node before –Larger Node; twice as many special cases Circular and doubly linked

Linked Lists and Recursion Recursive definition of a list –Null (empty list) is a list(base case) –A list consists of one item (the head) followed by a list (next) Example: –A->B->C-| is a list –Head = A, next = B->C-|

Recursive Function on Lists Base case for the recursive function is the empty list (base case for the definition) Other cases - –“Take a step” = do something to the head –Recurse = call function for the rest of the list –May or may not build up a solution

Recursive List Search Node recFind (Node head, E x){ //x is not in an empty list! If (head == null) return null; //x is found at the beginning Else if (head.item == x) return head; //Recursive case: search the rest of the list Return recFind(head.next, x); }

Recursive functions are private! A recursive function uses Node references. Node references are private –Implementation detail –Not every implementation of List has Nodes Most recursive functions have a public “starter” and a private internal function

Public Caller for recFind boolean isIn(E x){ //call Find on the head of the list //if Find does not return null, the item is in // the list. return (recFind(head,x) != null); }

Recursive CountItem This function counts the number of occurrences of the item x in the list Int CountItem (Node * head, ItemType x){ }

Public Caller for CountItem int List::howMany(ListItemType x){ }