linked lists part 1 cs 244 brent m. dingle, ph.d. game design and development program department of...
TRANSCRIPT
![Page 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](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/1.jpg)
Linked Listspart 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
![Page 2: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/2.jpg)
Previously
• Standard Template Library
• Makefiles and Geany
• Now…Moving on to Linked Lists
![Page 3: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/3.jpg)
Marker Slide
• Any General Questions ?
• Next up• Singly Linked Lists
• Class Fun• Definition and Description• Implementation Examples
![Page 4: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/4.jpg)
The Setup
• How to create a list using
• Pointers
• Nodes
![Page 5: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/5.jpg)
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.
![Page 6: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/6.jpg)
Currently
• Have a list of ONE node• The HEAD of the list
![Page 7: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/7.jpg)
Add a Node
• Point at someone near you• Now we have 2 nodes in our list
![Page 8: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/8.jpg)
An Another Node
• Node added last time (2nd person)• Now you point at someone near you• Keep going…
![Page 9: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/9.jpg)
End Group Fun
• Back to Computer Science
![Page 10: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/10.jpg)
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
![Page 11: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/11.jpg)
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
![Page 12: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/12.jpg)
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 Sheldon Howard
head tail
![Page 13: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/13.jpg)
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 Sheldon Howard
head tail
![Page 14: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/14.jpg)
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 Sheldon Howard Raj
head tail
![Page 15: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/15.jpg)
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 Sheldon Howard Raj
head tail
NULL
![Page 16: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/16.jpg)
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 Sheldon Howard Raj
head tail
NULL
![Page 17: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/17.jpg)
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 Sheldon Howard Raj
head tail
![Page 18: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/18.jpg)
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 Sheldon Howard Raj
head tail
![Page 19: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/19.jpg)
Singly Linked List Node
next
elem node
template <typename Type>class SLinkedListNode {public: Type elem;
SLinkedListNode<Type> *next;
};
Leonard Sheldon Howard Raj
Example code behind the scenes
![Page 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](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/20.jpg)
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
![Page 21: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/21.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 22: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/22.jpg)
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
Leonard Sheldon Howard Raj
head
Penny
tail
![Page 23: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/23.jpg)
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
Leonard Sheldon Howard Raj
head
Penny
tail
![Page 24: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/24.jpg)
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
Leonard Sheldon Howard Raj
head
Penny
tail
![Page 25: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/25.jpg)
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
head tail
![Page 26: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/26.jpg)
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
![Page 27: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/27.jpg)
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
Done trivially, already points to NULL
![Page 28: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/28.jpg)
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
![Page 29: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/29.jpg)
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
head tail
![Page 30: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/30.jpg)
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
![Page 31: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/31.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 32: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/32.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 33: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/33.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 34: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/34.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 35: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/35.jpg)
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
Sheldon Howard Raj
head tail
![Page 36: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/36.jpg)
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
head tail
![Page 37: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/37.jpg)
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
head tail
![Page 38: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/38.jpg)
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
head tail
![Page 39: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/39.jpg)
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
head tail
![Page 40: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/40.jpg)
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
head tail
![Page 41: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/41.jpg)
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
![Page 42: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/42.jpg)
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
Leonard Sheldon Howard
head tail
![Page 43: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/43.jpg)
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
Leonard Sheldon Howard
head tail
Raj
![Page 44: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/44.jpg)
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
Leonard Sheldon Howard
head tail
Raj
![Page 45: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/45.jpg)
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
Leonard Sheldon Howard
head tail
Raj
![Page 46: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/46.jpg)
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
![Page 47: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/47.jpg)
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
Leonard Sheldon Howard Raj
head tail
![Page 48: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/48.jpg)
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
Leonard Sheldon Howard Raj
head tailcurPtrprevPtr
![Page 49: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/49.jpg)
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
Leonard Sheldon Howard Raj
head tailcurPtr
prevPtr
![Page 50: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/50.jpg)
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
Leonard Sheldon Howard Raj
head tailcurPtr
prevPtr
![Page 51: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/51.jpg)
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
Leonard Sheldon Howard Raj
head tailcurPtr
prevPtr
![Page 52: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/52.jpg)
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
Leonard Sheldon Howard Raj
headtail
curPtrprevPtr
![Page 53: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/53.jpg)
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
Leonard Sheldon Howard Raj
headtail
curPtrprevPtr
![Page 54: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/54.jpg)
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
Leonard Sheldon Howard Raj
headtail
curPtrprevPtr
![Page 55: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/55.jpg)
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
Leonard Sheldon Howard
headtail
![Page 56: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/56.jpg)
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
![Page 57: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/57.jpg)
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
![Page 58: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/58.jpg)
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.
![Page 59: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/59.jpg)
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.
![Page 60: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/60.jpg)
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 startPtrstart,head, headPtrroot, rootPtr
![Page 61: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/61.jpg)
Adding a NodeTo 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 << "Please enter the name of the person -> ";cin >> tempPtr->m_name;cout << “Enter the age of the person -> “;cin >> tempPtr->m_age;cout << "Enter the height of the person ";cin >> tempPtr->height;tempPtr->mp_next = NULL;
tempPtr
???
BobBob22Bob221.8
A class constructor would likely do this last line for us
NULL
![Page 62: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/62.jpg)
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
startPtrNULL
![Page 63: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/63.jpg)
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
![Page 64: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/64.jpg)
Moving Example
• Assume we have a list with more than 1 nodeNode* 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 currentPtr currentPtr
Useful for outputting a list
Useful for appending to a list
![Page 65: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/65.jpg)
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 == NULLwill delete the entire list.
Useful for de-constructors
![Page 66: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/66.jpg)
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};
![Page 67: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/67.jpg)
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
![Page 68: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/68.jpg)
Free Play – Things to Work On
• Homework 4• Homework 5
![Page 69: Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University](https://reader036.vdocument.in/reader036/viewer/2022062500/5697bf7a1a28abf838c83258/html5/thumbnails/69.jpg)
The End
• Or is it?