“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

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.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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,
Linked Lists and Generics Written by J.J. Shepherd.
slides adapted from Marty Stepp and Hélène Martin
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Single-Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists I.
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”
Programming Abstractions
slides created by Marty Stepp
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Linked node problem 3 What set of statements turns this picture:
Pointers and Linked Lists
A Bag Implementation that Links Data
Chapter 16-2 Linked Structures
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Chapter 18: Linked Lists.
slides created by Marty Stepp and Ethan Apter
Linked List (Part I) Data structure.
Building Java Programs
Building Java Programs
CSE 373: Data Structures and Algorithms
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Lecture 7: Linked List Basics reading: 16.2
Programming Abstractions
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
Chapter 17: Linked Lists.
CSE 373 Data Structures and Algorithms
Lecture 14 Linked Lists CSE /26/2018.
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
slides created by Marty Stepp
Recursive Objects Singly Linked Lists.
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
Presentation transcript:

“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell Thought for the Day

New Class: IntegerList Same public methods as IntegerVector –Clients will see almost no difference Very different “internal” implementation

IntegerList first, numElements add, get, set, position, remove, length Class Diagram

Internal Details public class IntegerList { private class ListNode { public int data; public ListNode next; } // class ListNode private ListNode first; // Pointer to the // first ListNode in an IntegerList private int numElements; // Number of // elements in an IntegerList... } // class IntegerList An Inner Class

The Inner Class ListNode ListNode is private, so can only be used inside IntegerList data and next are public, so can be accessed outside ListNode public class IntegerList { private class ListNode { public int data; public ListNode next; } // class ListNode... } // class IntegerList

Structure of a ListNode Object We can picture a single ListNode object as follows: ListNode data next -56 private class ListNode { public int data; public ListNode next; } // class ListNode

A Linked List Made up of many ListNode objects “linked” together by the next fields ListNode data next

Locating the First Element in the List We use the first member of the IntegerList class ListNode data next IntegerList first numElements 4

Implementation of the Class Constructor –Must create an “empty” list public IntegerList () // Constructor { first = null; numElements = 0; } // constructor IntegerList first numElements 0

Adding a New Element Simple Case: add as first element Need to –Create a new list node –Link it into the list public void add (int item) // Place the new item into a list { ListNode node = new ListNode(); node.data = item; node.next = first; first = node; numElements++; } // add

public void add (int item) { ListNode node = new ListNode(); node.data = item; node.next = first; first = node; numElements++; } // add ListNode data next 3-8 IntegerList first numElements 2 Adding 17 0 node 17 3

More Flexibility: Adding Nodes at Any Position Need to work through the linked list to find the correct position