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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

COMP171 Fall 2005 Lists.
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.
Queues and Linked Lists
Linear Lists – Linked List Representation
Data Structures ADT List
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Linked Lists Linear collections.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
ADTs unsorted List and Sorted List
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
CSE Lecture 12 – Linked Lists …
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
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 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
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.
Linking Objects Together References are particularly important in computer science because they make it possible to represent the relationship among objects.
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 ساختمان داده ها و الگوريتم ها.
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.
April 24, 2017 Chapter 4 Data Abstraction The Walls
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
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 – Part 2 Arab Open University 1.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
An Array-Based Implementation of the ADT List
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
CS2006- Data Structures I Chapter 5 Linked Lists I.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Data Abstraction: The Walls
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Chapter 4 Linked Lists.
List Implementations Chapter 9.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Ch. 5 Linked List When a list is implemented by an array Linked list
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Programming II (CS300) Chapter 07: Linked Lists
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

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 newNode.setNext(curr); prev.setNext(newNode); Figure 5-13 Inserting a new node into a linked list

Inserting a Node into a Specified Position of a Linked List To insert a node at the beginning of a linked list newNode.setNext(head); head = newNode; Figure 5-14 Inserting at the beginning of a linked list

Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if curr is null newNode.setNext(curr); prev.setNext(newNode); Figure 5-15 Inserting at the end of a linked list

Inserting a Node into a Specified Position of a Linked List Three steps to insert a new node into a linked list Determine the point of insertion Create a new node and store the new data in it Connect the new node to the linked list by changing references

Determining curr and prev Determining the point of insertion or deletion for a sorted linked list of objects assume we have newValue which should inserted (deleted) for ( prev = null, curr = head; (curr != null) && (newValue.compareTo(curr.getItem()) > 0); prev = curr, curr = curr.getNext() ) { } // end for

A Reference-Based Implementation of the ADT List A reference-based implementation of the ADT list Does not shift items during insertions and deletions Does not impose a fixed maximum length on the list Figure 5-18 A reference-based implementation of the ADT list

A Reference-Based Implementation of the ADT List Default constructor Initializes the data fields numItems and head List operations Public methods isEmpty size add remove get removeAll Private method find

Reference-based Implementation of ADT List public boolean isEmpty() { return numItems == 0; } // end isEmpty public int size() { return numItems; } // end size

Reference-based Implementation of ADT List private Node find(int index) { // // Locates a specified node in a linked list. // Precondition: index is the number of the desired // node. Assumes that 1 <= index <= numItems+1 // Postcondition: Returns a reference to the desired // node. // Node curr = head; for (int skip = 1; skip < index; skip++) { curr = curr.getNext(); } // end for return curr; } // end find

Reference-based Implementation of ADT List public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { // get reference to node, then data in node Node curr = find(index); Object dataItem = curr.getItem(); return dataItem; } else { throw new ListIndexOutOfBoundsException( "List index out of bounds on get"); } // end if } // end get

Reference-based Implementation of ADT List public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems+1) { if (index == 1) { // insert the new node containing item at // beginning of list Node newNode = new Node(item, head); head = newNode; } else { Node prev = find(index-1); // insert the new node containing item after // the node that prev references Node newNode = new Node(item, prev.getNext()); prev.setNext(newNode); } // end if numItems++; } else { throw new ListIndexOutOfBoundsException( "List index out of bounds on add"); } // end if } // end add

Reference-based Implementation of ADT List public void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { if (index == 1) { // delete the first node from the list head = head.getNext(); } else { Node prev = find(index-1); // delete the node after the node that prev // references, save reference to node Node curr = prev.getNext(); prev.setNext(curr.getNext()); } // end if numItems--; } // end if else { throw new ListIndexOutOfBoundsException( "List index out of bounds on remove"); } // end if } // end remove

Comparing Array-Based and Referenced-Based Implementations Size (number of elements stored) Array-based Fixed size Can you predict the maximum number of items in the ADT? Will an array waste storage? Resizable array Increasing the size of a resizable array can waste storage and time Reference-based Do not have a fixed size Do not need to predict the maximum size of the list Will not waste storage (for data)

Comparing Array-Based and Referenced-Based Implementations Storage requirements Array-based Requires less memory than a reference-based implementation There is no need to store explicitly information about where to find the next data item Reference-based Requires more storage An item explicitly references the next item in the list

Comparing Array-Based and Referenced-Based Implementations Access time Array-based Constant access time Reference-based The time to access the i th node depends on I But when processing all elements: both implementations require constant time on average

Comparing Array-Based and Referenced-Based Implementations Insertion and deletions Array-based Require you to shift the data Reference-based Do not require you to shift the data Require a list traversal