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.

Slides:



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

Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
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.
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
M180: Data Structures & Algorithms in Java
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Stacks, Queues, and Deques
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
UNIT-II Topics to be covered Singly linked list Circular linked list
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
CSCI-255 LinkedList.
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)
Linked lists.
Programming Abstractions
Linked Lists.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Programming Abstractions
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked lists.
Presentation transcript:

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

Self-referential Classes Contains a reference member (a link of some type) Reference member refers to an object of the same class type Necessary to create linked list (amongst others) Linked list consists of nodes Nodes consist of –Data –Link to next node A

Self-referential Classes Need “pointer” –Start pointer (so that we know where to start) –End pointer? (so that we know where to stop) –Null reference = end of “chain” The new operator –Allocates appropriate amount of memory –OutOfMemoryException

class Node { private int data; private Node next; public Node (int d) { //constructor body } public int Data { get { //get body } set { //set body } public Node Next { get { //get body } set { //set body }

The header & fields class Node { private int data; private Node next; Note the field type Node (this is what is meant by self-referential). It is a reference to another object of the SAME class 3 Data Node 4

Property Data public int Data { get { //get body } set { //set body } This does not need to be an int, it could be another class A Set (puts data INTO the node) Get (retrieves data FROM the node)

Property Next public Node Next { get { //get body } set { //set body } This has to be of type Node, otherwise it would not be self-referential. You can set this value in the constructor, but you must have a get and a set because you will probably need to change the value of Node (adding or deleting nodes from a list, queue, etc.) A Get (retrieves the next node or node position) Set (allows the next node to be positioned or inserted)

Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list

Chain of Nodes Data component “Pointer” or Reference Component Last reference “points” or refers to a NULL

The List Class public class LinkedListClass { private StudentNode firstStudent; private StudentNode lastStudent; private string ListName; //a name for the list Linked list has a FIRST node and a LAST node (or start and stop) The name for the list is not necessary (just a bit of extra)

The Constructors public LinkedListClass(string n) { ListName = n; firstStudent = lastStudent = null; } //construct an empty list with "list" as its name public LinkedListClass(): this("list") { } Overloaded constructors – one with a list name entered by the user and one with list name “list” by default. Both create empty lists.

The Constructors A public LinkedListClass(string n) { ListName = n; firstStudent = lastStudent = null; } First nodeLast node

Property: First Node public StudentNode FirstStudent { get { return firstStudent; } Allows you to retrieve the position or the address of the first node This property only has a “get” because we don’t want anything to change this property – except via the operations of inserting and deleting

Property: Last Node public StudentNode LastStudent { get { return lastStudent; } Allows the retrieval of the last node What could happen if there was a “set” option with the first and last nodes?

Method: Insert at Front public void InsertAtFront(Student insertItem) { lock (this) { if (IsEmpty()) { firstStudent = new StudentNode(insertItem); lastStudent = new StudentNode(insertItem); } else firstStudent = new StudentNode(insertItem, firstStudent); } Inserting a node to the front of the list

Method: Insert at Front Inserting into an empty list A First nodeLast node A if (IsEmpty()) { firstStudent = new StudentNode(insertItem); lastStudent = new StudentNode(insertItem); }

Method: Insert at Front Inserting into a list that already has nodes B First node Last node ACA firstStudent = new StudentNode(insertItem, firstStudent);

Using Linked Lists Step 1: What data should the list store? Step 2: Any special requirements? –Ordered list? Step 3: Where to use the list? –Global or local? Step 4: Instantiate Step 5: Special methods (not inside the list class) –Find average –Find a particular node (specific data) –Find minimum/maximum –Insert a node into the list at the correct place –Remove a specific node (from anywhere in the list) –Sorting a list (perhaps inside list class?)

Special Methods Need a current or position node –Keep track of where you are “at the moment” Do not touch the first or last node pointers in the class unless inserting or deleting those nodes Hands Off!!