Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.

Slides:



Advertisements
Similar presentations
JAVA & Linked List Implementation
Advertisements

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.
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
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.
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.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Data Structures Using C++ 2E
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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
Defining New Types Lecture 21 Hartmut Kaiser
Linked List Containers. Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n *
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
Linked List Containers. Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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.
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 Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
Memory Management.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
COMP 53 – Week Eight Linked Lists.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming Abstractions
Linked Lists.
Lists.
Data Structures & Algorithms
Data Structures & Algorithms
Lists CMSC 202, Version 4/02.
Data Structures & Programming
Presentation transcript:

Linked List Containers

Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists of Data Data Pointer to another node Pointer to another node Traditional view of data: Traditional view of data: Data

Linked Lists Insertion at a specific point: Get a new node (allocate from memory) Set data pointer Set link of new node to previous node link Set link of previous node link to new node FATMATRAT HAT

Linked Lists Deletion at a specific point: Set the link of the previous node to the link of the node to delete. FATMATRAT FATRAT

Linked List Representations Can locate data in any place in memory, not required to be sequential Can locate data in any place in memory, not required to be sequential No requirements on computing size of list beforehand No requirements on computing size of list beforehand No more space than need No more space than need No bounds on space No bounds on space No resizing No resizing Still fairly trivial to manage and understand Still fairly trivial to manage and understand

Linked Lists in C++ Need to define a ListNode Class that represents: Need to define a ListNode Class that represents: Data Data Pointer to another ListNode Pointer to another ListNode Need to define a LinkedList Class that provides container operations (Add, Delete, etc), using ListNodes to implement the storage for the container Need to define a LinkedList Class that provides container operations (Add, Delete, etc), using ListNodes to implement the storage for the container

Linked Lists in C++ Node definition: Node definition: class nodeName class ThreeLetterListNode {{ private: private: type dataName; char data[3]; nodeName *link; ThreeLetterListNode * link; }}

Linked Lists in C++ Requirements for LinkedList construct: Requirements for LinkedList construct: Want to preserve encapsulation of nodes and ensure that updating the data and link pointers are only accomplished by the ListNode itself or the LinkedList Want to preserve encapsulation of nodes and ensure that updating the data and link pointers are only accomplished by the ListNode itself or the LinkedList Arbitrary and unlimited amount of memory Arbitrary and unlimited amount of memory Characterize the LinkedList as: A LinkedList consists of zero or more objects of type ListNode. Characterize the LinkedList as: A LinkedList consists of zero or more objects of type ListNode.

Linked Lists Linked List definition suggests that the List object actually physically contains lots of ListNodes. Linked List definition suggests that the List object actually physically contains lots of ListNodes. Contains a pointer, called first, to one ListNode, from which the rest of the ListNodes can be found by following links. Contains a pointer, called first, to one ListNode, from which the rest of the ListNodes can be found by following links. All ListNode objects are not physically contained within the LinkedList object All ListNode objects are not physically contained within the LinkedList object To access private data members of ListNode class, without making the data members public or having public functions to set data and links, To access private data members of ListNode class, without making the data members public or having public functions to set data and links, make the LinkedList class a friend of the ListNode class: make the LinkedList class a friend of the ListNode class:

Linked Lists class LinkedList; // forward declaration template template class ListNode { friend class LinkedList ; private: Type data; Type data; ListNode *link; ListNode *link;}; template template class LinkedList { public: // manipulation operations public: // manipulation operations private: private: ListNode *first; } LinkedList.h ListNode.h

Forward Declarations Forward Declarations: Forward Declarations: Used when defining classes that rely on each other. Used when defining classes that rely on each other. Indicates to compiler that the class in the forward declaration is going to be defined later and is a valid class. Indicates to compiler that the class in the forward declaration is going to be defined later and is a valid class. Similar to use of function signatures to ensure compiler sees all available functions. Similar to use of function signatures to ensure compiler sees all available functions.

Linked Lists Linked List Node Constructor Linked List Node Constructor Set Data Value, Set Pointer Set Data Value, Set Pointer ListNode ::ListNode (Type inputData, ListNode * inputLink) { data = inputData; link = inputLink; }

List Operations What operations do we want or need for the LinkedList? What operations do we want or need for the LinkedList?ConstructorDestructorisEmpty() isFull()// doesn’t make sense in this context add() (element?, position?) delete() (element?, position?)

Linked Lists Linked List Constructor Linked List Constructor Creates an empty list Creates an empty list Essence of list is “first” pointer, so that should be set to zero if empty Essence of list is “first” pointer, so that should be set to zero if emptyLinkedList<Type>::LinkedList<Type>(){ first = 0; }

Linked List Insertion Function Interface: void add(Type & toAdd); Passed in a variable of type Type 1 st step: Generate a new ListNode 1 st step: Generate a new ListNode Should hold value toAdd of type Type Should hold value toAdd of type Type Should point to nothing Should point to nothing

Linked List Insertion Node Creation: Node *node = new Node (toAdd, 0); Node Creation: Node *node = new Node (toAdd, 0); Then need to insert in “right” place in list. What is right place? Then need to insert in “right” place in list. What is right place? Depends on problem of interest Depends on problem of interest Sorted list? First in, first out list? Sorted list? First in, first out list? Let’s look at four cases: Let’s look at four cases: Empty list Empty list Non-empty, Front of list Non-empty, Front of list Non-empty, Back of list Non-empty, Back of list Non-empty, Arbitrary position in middle of list Non-empty, Arbitrary position in middle of list

Insertion into Empty List Insertion into Empty List Empty List Empty List FirstFirst 0 value0 void LinkedList: :Add(Type & toAdd) { ListNode *node = new ListNode (toAdd, 0); if (first == 0) first = node; }

Insertion Non-empty list, insert at front Non-empty list, insert at front value1value20 first value30 node value1value20 first value3

Linked List Insertions Insert at front method: Insert at front method: ListNode *node = new ListNode (toAdd, first); first = node;

Insertion Non-empty list, insert at back Non-empty list, insert at back value1value20 first value30 node value1value2 first value30

Linked List Insertions Insert at back method: Insert at back method: // get to last node ListNode * current = first; while (current-> link != 0) { current = current->link; } // add ListNode * node = new ListNode (toAdd, 0); current->link = node;

Insertion Non-empty list, insert in middle Non-empty list, insert in middle value1value20 first value30 node value1value20 first value3 current

Linked List Insertions Insert at arbitrary place: Insert at arbitrary place: ListNode * current = first; while (someExpression holds)// current->value value < 5 {// for example current = current-> link; } ListNode * node = new ListNode (value, current->link); current->link = node;

Linked List Insertions Insertion at front, insertion at end usually encapsulated into two linked list methods: Insertion at front, insertion at end usually encapsulated into two linked list methods: Insert (at front), Append (onto back) Insert (at front), Append (onto back) Insertion in middle could be encapsulated as insertAtNth() Insertion in middle could be encapsulated as insertAtNth() Usually seen instead as part of more complicated methods (insertSorted for example) Usually seen instead as part of more complicated methods (insertSorted for example)

Linked List: Deletion How about deleting nodes? How about deleting nodes? Very similar to addition – 3 cases Very similar to addition – 3 cases Case 1: Delete from front Case 1: Delete from front value1value20 first value3 value20value3 first

Linked Lists: Deletion Deletion from front: Deletion from front: ListNode * node = first; first = first->link; delete node;

Linked Lists: Deletion Case 2: Delete from back Case 2: Delete from back value1value20 first value3 0value1 first current previous

Linked Lists: Deletion Deletion from back: Deletion from back: // get to just before last node ListNode * previous =0; ListNode * current = first; while (current-> link != 0) { previous = current; current = current->link; } previous->link = 0; delete current;

Linked Lists: Deletion Case 3: Delete from arbitrary location Case 3: Delete from arbitrary location value1value20 first value3 value20value1 first current previous

Linked Lists: Deletions ListNode * previous =0; ListNode * current = first; while (someExpression holds) //current->value != { // value3 for example previous = current; current = current->link; } previous->link = current->link; delete current;

Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Three cases: Three cases: ListA is empty – Set head for listA to head of listB ListA is empty – Set head for listA to head of listB ListB is empty – No change, nothing to do ListB is empty – No change, nothing to do Both have data – Set pointer on last node of listA to head for listB Both have data – Set pointer on last node of listA to head for listB This version of concatenate is: This version of concatenate is: Easy to implement Easy to implement Destructive towards listsA and listB – as it potentially changes how listA and listB work from here on out (listA delete will now cause listB nodes to go away as well) Destructive towards listsA and listB – as it potentially changes how listA and listB work from here on out (listA delete will now cause listB nodes to go away as well)

Concatenate Example void LinkedList ::concatenate(const LinkedList & listB) { // empty list A if (!first) { first = listB.first; return; } if (!first) { first = listB.first; return; } else if (listB.first) // if b is empty do nothing { // get to end of my list (listA) // get to end of my list (listA) ListNode * current = first; ListNode * current = first; while (current->link != 0) { current = current->link; } while (current->link != 0) { current = current->link; } current->link = listB.first; current->link = listB.first;}}

Safer Concatenate Example LinkedList& LinkedList ::concatenate(const LinkedList & listB) { // make a copy of list A using copy constructor LinkedList returnList = *(new LinkedList(*this)); if (listB.first) // if b is empty do nothing, else add to end { ListNode * current =listB.first; ListNode * current =listB.first; while (current->link != 0) while (current->link != 0) {listA.add(current->value); current = current->link; } return returnList; } } This version returns a new LinkedList which is a copy of listA with copies of listB nodes added to the end. Changes to the new list don’t affect listA or listB.