Linked Lists and Generics Written by J.J. Shepherd.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Linked Lists CENG 213 Data Structures.
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.
M180: Data Structures & Algorithms in Java
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.
Linked Lists
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Data Structures & Algorithms
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: 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.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
Data Structures Using Java1 Chapter 4 Linked Lists.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
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.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
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 List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
Data Structures Using C++1 Chapter 5 Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked Lists and Generics Part 2 Written by J.J. Shepherd.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
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,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
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 Lists.
CSCI 3333 Data Structures Linked Lists.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Data Structures & Algorithms
Linked List Functions.
Linked Lists.
Linked Lists.
Presentation transcript:

Linked Lists and Generics Written by J.J. Shepherd

Data Structures Data structures are used to organize and store data. All the data structures are used to group together like data with some defined rules.

Arrays Great, and so far only, way to group together like data Random Access for data – Example: If we had an array named numbers and we wanted the access the 5 th element it would look like this number[5] Not perfect – Not resizable – If the amount of data isn’t known or fixed one can waste memory or have to create larger arrays

IS THERE ANYTHING WE CAN DO!?

Linked List Has nodes that contain – Data – Links to other nodes Resizable But no random access Head Current Tail This means null Previous

Linked List Generally all linked lists have these pointers which keeps track of certain nodes – Head: Points to the first element in the list – Tail: Points to the last element in the list – Current: Movable and points to a node of interest – Previous: Always stays one behind current (Optional) Head Current Tail Previous

Linked List They also generally have the following methods – Add: Adds a new node – Remove: Removes a certain node – Peek: Returns value of a certain node – Iterate: Moves the current pointer forward – Print: Prints the data for each node Head Current Tail Previous

Setting up a Linked Structure Linked structures should behave like a TYPE, thus it needs its own class Much link an array all of the information and the actions need to be internal We must create a class that represents the entire structure, but there’s some problems The structure is made of nodes which should be a class as well

Internal Classes You may define classes inside of classes public class LinkedList { private class Node { int data; Node link; } Node head; Node previous; Node current; }

Adding to a Linked List Adding to the linked list requires changing links to point to the new data Head Current Tail Previous

Adding to a Linked List Original List, soon we are adding a node after Current Head Current Tail Previous

Adding to a Linked List A new node is created Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }

Adding to a Linked List A new node is created Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }

Adding to a Linked List Always check if current is not null Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }

Adding to a Linked List New Node Link set to current link Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }

Adding to a Linked List Finally Current link is set to the new node Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }

Moving Through a Linked List This is called “Iterating” Current and previous keep track of iterations Head Current Tail Previous

Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;

Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;

Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;

Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;

Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;

Removing from a Linked List Removing an element requires changing links to skip over Head Current Tail Previous

Removing from a Linked List Original List, soon we are removing the node at Current Head Current Tail Previous

Removing from a Linked List There are a few cases we must consider when removing a node. The first one is removing a node from the middle of the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }

Removing from a Linked List If both current and previous are not null we know that the node we are removing is in the middle of the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }

Removing from a Linked List The previous’ link is now set to the current link to skip over that node Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }

Removing from a Linked List Finally the current point is set to the next one in the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }

Removing from a Linked List The node was removed as it has been skipped over Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }

Removing from a Linked List Another case is when the current is pointing to the head Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }

Removing from a Linked List We can tell by seeing that the current is not null but previous is Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }

Removing from a Linked List Move the head to current’s link Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }

Removing from a Linked List Finally move the current to the head Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }

Removing from a Linked List The previous head node is deleted as the new head node has been moved forward Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }