Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Linked Lists.
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.
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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 Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
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.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Classes and Data Structures Jeffrey S. Childs
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
1 Writing a Good Program 8. Elementary Data Structure.
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.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List by Chapter 5 Linked List by
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Introduction to Data Structures and Algorithms
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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 © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Doubly Linked List Review - We are writing this code
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked Lists: Implementation of Queue & Deque
Chapter 18: Linked Lists.
Doubly Linked List Implementation
Chapter 16 Linked Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Doubly Linked List Implementation
Stacks and Linked Lists
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout 2014 Chapter 5-ish

Previously Standard Template Library Makefiles and Geany Now…Moving on to Linked Lists

Marker Slide Any General Questions ? Next up Singly Linked Lists Class Fun Definition and Description Implementation Examples

The Setup How to create a list using Pointers Nodes

Group Fun The ceiling is NULL Each of you will be a NODE We shall now make a LINKED LIST Who wants to be the HEAD OF THE CLASS (list) ? Excellent. Point at the ceiling.

Currently Have a list of ONE node The HEAD of the list

Add a Node Point at someone near you Now we have 2 nodes in our list

An Another Node Node added last time (2 nd person) Now you point at someone near you Keep going…

End Group Fun Back to Computer Science

Marker Slide Any Questions On: Singly Linked Lists Class Fun Next up Singly Linked Lists Definition and Description Implementation Examples Doubly Linked Lists Circularly Linked Lists

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node Leonard head tail

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHoward head tail

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHoward head tail

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj head tail

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj  head tail NULL

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj  head tail NULL

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj  head tail

Singly Linked List A singly linked list is a structure consisting of a sequence of nodes A singly linked list stores a pointer to the first node (head) and last (tail) Each node stores – element – link to the next node next elem node LeonardSheldonHowardRaj  head tail

Singly Linked List Node next elem node template class SLinkedListNode { public: Type elem; SLinkedListNode *next; }; LeonardSheldonHowardRaj  Example code behind the scenes

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes Typical Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  LeonardSheldonHowardRaj headtail

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  LeonardSheldonHowardRaj head  Penny tail

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  LeonardSheldonHowardRaj head Penny tail

Inserting at the Front 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  LeonardSheldonHowardRaj head Penny tail

Inserting at the Front: Special Case 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  headtail 

Inserting at the Front: Special Case 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  Raj headtail  

Inserting at the Front: Special Case 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  Raj headtail   Done trivially, already points to NULL

Inserting at the Front: Special Case 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node  Raj head  tail

Inserting at the Front: Special Case 1.Allocate a new node 2.Have new node point to old head 3.Update head to point to new node 4.If tail is NULL, update tail to point to the head node Raj headtail 

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes Typical Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  LeonardSheldonHowardRaj headtail

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  LeonardSheldonHowardRaj headtail

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  LeonardSheldonHowardRaj headtail

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  LeonardSheldonHowardRaj headtail

Removing at the Front 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  SheldonHowardRaj headtail

Removing at the Front: Special Case 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  Sheldon headtail

Removing at the Front: Special Case 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  Sheldon headtail 

Removing at the Front: Special Case 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  Sheldon headtail 

Removing at the Front: Special Case 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node  Sheldon headtail 

Removing at the Front: Special Case 1.Update head to point to next node in the list 2.Return elem of previous head and delete the node 3.If head is NULL, update tail to NULL  headtail 

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes Typical Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list

Inserting at the Back 1.Allocate a new node 2.If tail is NULL, update head and tail to point to the new node; otherwise 1.Have the old tail point to the new node 2.Update tail to point to new node  LeonardSheldonHoward headtail

Inserting at the Back 1.Allocate a new node 2.If tail is NULL, update head and tail to point to the new node; otherwise 1.Have the old tail point to the new node 2.Update tail to point to new node  LeonardSheldonHoward headtail  Raj

Inserting at the Back 1.Allocate a new node 2.If tail is NULL, update head and tail to point to the new node; otherwise 1.Have the old tail point to the new node 2.Update tail to point to new node LeonardSheldonHoward headtail  Raj

Inserting at the Back 1.Allocate a new node 2.If tail is NULL, update head and tail to point to the new node; otherwise 1.Have the old tail point to the new node 2.Update tail to point to new node LeonardSheldonHoward headtail  Raj

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes Typical Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj headtail

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj headtail curPtr prevPtr 

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj headtail curPtr prevPtr

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj headtail curPtr prevPtr

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj headtail curPtr prevPtr

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj head tail curPtr prevPtr

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj head tail curPtr prevPtr 

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHowardRaj head tail curPtr prevPtr 

Removing at the Back No efficient way of doing so (O(n)) – Must walk a curPtr to end of list along with a prevPtr Typically would not use a singly linked-list if this operation is commonly used  LeonardSheldonHoward head tail

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes Typical Operations – insertFront(e): inserts an element on the front of the list – removeFront(): returns and removes the element at the front of the list – insertBack(e): inserts an element on the back of the list – removeBack(): returns and removes the element at the end of the list

Marker Slide Any Questions On: Singly Linked Lists Class Fun Definition and Description Next up Singly Linked Lists Implementation Examples Check Time May Stop Here Implementation Examples is reviewed in next presentation

Linked List – Definition A linked list is a data structure which is built from nodes and pointers. A list forms a “chain of nodes” With pointers representing the links of the chain and holding the entire list together.

Linked List – Example This linked list has four nodes in it Each with a link to the next node in the series. The last node has a link to the value NULL There is also another special pointer, called Start which points to the first link in the chain so that we can keep track of it.

Linked List – Implementation Key part of a linked list is the node structure Which holds the data for each node name, age, height, pointer to next node class Node { public: string m_name; int m_age; // age in years double m_height; // height in meters Node* mp_next; // pointer to next node }; Node* startPtr = NULL; // global variable to keep track // of beginning of the list Others may call startPtr start, head, headPtr root, rootPtr

Adding a Node To the End of the List First Create a new node Ask user for information to fill in the node’s data Node *tempPtr = new Node; cout "; cin >> tempPtr->m_name; cout “; cin >> tempPtr->m_age; cout << "Enter the height of the person  "; cin >> tempPtr->height; tempPtr->mp_next = NULL; tempPtr ??? Bob 22 Bob A class constructor would likely do this last line for us NULL

Initialize the Start Pointer Assuming that was the first node in the list How would we initialize the global variable startPtr ? Node *startPtr = NULL; ?????startPtr = tempPtr; tempPtr NULL startPtr NULL

Moving Through a List It is common to use a currentPtr To keep track of what node is “currently” being examined It too, usually begins at the beginning startPtr = tempPtr; ?????Node* currentPtr = startPtr; startPtr NULL currentPtr NULL

Moving Example Assume we have a list with more than 1 node Node* currentPtr = startPtr; NULL startPtr currentPtr while (currentPtr->next != NULL ) { currentPtr = currentPtr->mp_next } This will move the currentPtr to point to the last node in the list currentPtr Useful for outputting a list Useful for appending to a list

Removing the Head How to remove the first element NULL startPtr oldHeadPtr removeFront() { Node* oldHeadPtr = startPtr; startPtr = oldHeadPtr->mp_next; delete oldHeadPtr; } startPtr Calling this repeatedly until startPtr == NULL will delete the entire list. Useful for de-constructors

Example: Linked List Class class MyLinkedList { public: MyLinkedList(); // constructor ~MyLinkedList(); // destructor bool isEmpty() const; // returns true if list is empty Node* findNode(string findName); // returns null or node w/ match void addNode(const Node& newNode); // add node to list void removeFront(); // remove first node of list private: Node* mp_startPtr; // pointer to head of list }; class Node { public: string m_name; int m_age; // age in years Node* mp_next; // pointer to next node };

Summary Review Linked Lists are similar to arrays When compared to arrays Linked Lists have The bad: You cannot access the i-th element unless you walk to it through the i-1 elements that come before it The good: You can INSERT an element into a list WITHOUT moving/shifting all the other elements

Free Play – Things to Work On Homework 4 Homework 5

The End Or is it?