Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Chapter 17 Linked Lists.
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
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
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
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. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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 Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List by Chapter 5 Linked List by
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
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 List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
Lectures linked lists Chapter 6 of textbook
Programming Abstractions
Chapter 4 Linked Lists
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Chapter 18: Linked Lists.
Programming Abstractions
Lists CSE 373 Data Structures.
Review and Prepare for Test 2
Chapter 16 Linked Structures
Data Structures & Algorithms
Lists CSE 373 Data Structures.
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Previously This is a continuation Part 2 of class 14 Previous discussion was on Reading Data from a text file Sorting Data Searching Data Moving on to Linked Lists

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

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 

Singly Linked List: Operations A singly linked list is a structure consisting of a sequence of nodes 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 

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 

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

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

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

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->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

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

Doubly Linked Lists You can also create linked lists that have pointers in both directions Pointer to NEXT Pointer to PREVIOUS Doubly Linked lists are sometimes more useful than singly linked The cost is maintaining 2 pointers (and using more memory to do so)

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

You can also create lists that go in circles They have nodes that can be referred to as front node (start) back node (end) And a cursor (current) pointer So… Ummm… Always keep track of your pointers and What they *should* be pointing to Circularly Linked Lists

So Ends the Listing of Lists Three Linked List Data Types Singly Linked Doubly Linked Circularly Linked Each with its own features and uses Each can be SORTED and SEARCHED Just as arrays can Lists have advantages and disadvantages compared to arrays See previous summary of lists slide

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

Form into groups Create a linked list class Based mostly on the above Nodes are people with first name and age Class be able to add nodes (to front) remove nodes by name sort list alphabetically by name print the entire list (readable) allocate and de-allocate memory correctly using constructors and destructors Suggest do sort() last and discuss as a group – draw pictures Put ALL group member names at beginning of EVERY source file. When done, or out of time Submit to correct D2L dropbox Suggest distribute work 1 write header (.h) 1 write main() for testing 1 write constructor/destructor/isEmpty 1 write addNode/Insert 1 write findName 1 write displayList and so on, or something like that Find way to combine at end Only 1 submission needed per team Look on D2L for starter code GCA202 Check Next Slide too

struct LIST { struct LIST * pNext; int iValue; }; Insertion Sort – Code for Linked List struct LIST * SortList1(struct LIST * pList) { // zero or one element in list if(!pList || !pList->pNext) return pList; // head is the first element of resulting sorted list LIST * head = 0; while(pList != 0) { LIST * current = pList; pList = pList->pNext; if(!head || current->iValue iValue) { // insert into the head of the sorted list // or as the first element into an empty // sorted list current->pNext = head; head = current; } // the else-codeblock on the right goes here } return head; } else { // insert current element into proper position in non- empty sorted list LIST * p = head; while(p) { // p->pNext == NULL if last element of sorted list // current->iValue pNext->iValue // means in middle if ( !p->pNext || current->iValue pNext->iValue) { // insert into middle of sorted list or as last element current->pNext = p->pNext; p->pNext = current; break; // done, exit while loop } p = p->pNext; }

The End Of this part There may be more