Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Stacks, Queues, and Linked Lists
Linked Lists.
JAVA & Linked List Implementation
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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.
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.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Linked Structures, LinkedStack
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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Data Structure & Algorithms
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
UNIT – I Linked Lists.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
The Bag and Sequence Classes with Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Linked List Intro CSCE 121 J. Michael Moore.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Programming Abstractions
Linked Lists.
Linked List Functions.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Linked Lists.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Linked Lists Objects->Connected->by->Pointers

What is a Linked List? List: a collection Linked: any individual item points to another item to connect them Linked List: ◦ A collection of objects (data + functions) ◦ Data includes items of the same data type(s) ◦ A way of implementing a container class

List Operations Create itself (initialize) Insert an item Delete an item Print itself Know the number of items it contains

Linked List Container Class Implementation of the container class is based on the concept of a node Node: holds two important components ◦ The item that the user wants (private data and public functions) ◦ A pointer to the next node in the list (next)

Anatomy of a Node Pointer to the head of the list Tux Penquin data Pointer to next node Node Null pointer At the end of the list

How to Code a Node class node { public: typedef double value_type; … private: value_type penquin; node *next; };

What Else Does a Node Need? Data is private Accessor Method Mutator Method

From a Node to a List Create a pointer to a node object ◦ Remember the pointer to a dynamic array? Create an add function to add a new node and link it to a list (and other useful functions) ◦ Textbook creates these functions as non-member functions in the linked list toolkit ◦ You can also create another class to hold all of these useful functions  Bonus: node is more easily accessible if it is a member of the same class as the functions that manipulate it!

From a Node to a List Non-Member functions in a Linked List Toolkit Begin with a “head” pointer to a node This pointer must be declared in main main { node *head; }

From a Node to a List Member Functions in a Linked List Class Begin with a pointer to a node This pointer must be declared in the private section of your linked list class: class myList { … private: node *head; };

Class Functions versus Non- Member Functions main { node *head; } class myList { … private: node *head; };

From a Node to a List: How To Initialize a Node to Add to a List So far, we have a pointer for a node object, but where does it point? Remember how to make a dynamic array grow? Create a function to add nodes to the list ◦ 1. Declare a node object ◦ 2. Use the new operator to initialize it in the heap ◦ 3. Initialize the data to a valid state (put some data in it) ◦ 4. Get the original “head” pointer to point to it Which end of the list do we add a new node?

From a Node to a List: Where To Add a Node The first node should be at the head of the list Where should the second node go? ◦ Head of the list ◦ Tail of the list How do we know where the head of the list is? Do we know where the tail is?

From a Node to a List: The Add Function ◦ 1. Declare a node object ◦ 2. Use the new operator to initialize it in the heap Node *newOne = new Node;

From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_type myData) { Node *newOne = new Node; … } void AddNode(node*& head, value_type myData) { Node *newOne = new Node; … }

From a Node to a List: 1. Declare a node object 2. Use the new operator to initialize it in the heap 3. Initialize the data to a valid state (put some data in it) 4. Get the original “head” pointer to point to it newOne->set_data(“Joe”);

From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_type myData) { Node newOne = new Node; newOne->set_data(“Joe”); return newOne; } void AddNode(node*& head, value_type myData) { Node newOne = new Node; newOne->set_data(“Joe”); head = newOne; } 3. Get the original “head” pointer to point to it

From a Node to a List: What about Joe’s Next pointer? Joe newOne head_ptr

From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne

From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne

From a Node to a List: One MORE Thing… ◦ 1. Declare a node object ◦ 2. Use the new operator to initialize it in the heap ◦ 3. Initialize the data to a valid state (put some data in it) ◦ 3b. Set the node’s next pointer to point to the head of the list ◦ 4. Get the original “head” pointer to point to it

From a Node to a List: 1. Declare a node object 2. Use the new operator to initialize it in the heap 3. Initialize the data to a valid state (put some data in it) 3b. Set the node’s next pointer to point to the head of the list 4. Get the original “head” pointer to point to it newOne->set_next(head);

From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_type myData) { Node newOne = new Node; newOne->set_data(“Joe”); newOne->set_next(head); return newOne; } void AddNode(node*& head, value_type myData) { Node newOne = new Node; newOne->set_data(“Joe”); newOne->set_next(head); head = newOne; } 3a. Set the node’s next pointer to point to the head of the list

From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne

From a Node to a List: Does it Work for an Empty List? What will the new node’s next pointer point to if the list is empty? newOne->set_next(head); Joe newOne head_ptr ??? What if you initialize head_ptr as NULL?

Removing a Node From a List Nodes often need to be removed from a linked list From front of the list From another location in the list ◦ Two operations are needed  Search for node to remove  Remove node

Removing the node at the head of a list Null pointer At the end of the list head_ptr Is this OK? delete head_ptr;

Removing the node at the head of a list Null pointer At the end of the list head_ptr doomed What if I create a new pointer and point it to the head of the list …

Removing the node at the head of a list Null pointer At the end of the list head_ptr doomed …and then point the head pointer to the next node…

Removing the node at the head of a list Null pointer At the end of the list head_ptr Can I delete doomed now? doomed

Member Function Code Node *doomed = list; list = list->get_next(); delete doomed;

Does This Work For Removing ANY Node? This method works because we know where the head node is What of the node is in the middle or the end? Do we know where it is? ◦ Our find function can locate the node ◦ But what about all of the nodes in front of that node?

Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr doomed Does this work? And what about the penguin in the green hat? ???

Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr previous What if I remember where the previous node is…and then I redirect that nodes’ next pointer instead?

Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr previous …but what if I need to delete the last node?

Does This Work For Removing ANY Node? Redirect Which Pointer?Redirect Where? Deleting 1 st NodeHead pointer to point toHead pointer’s->next Deleting Middle NodePrevious pointer to point toPrevious pointer’s -> next Deleting Last NodePrevious pointer to point toNULL