LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the 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.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
slides adapted from Marty Stepp and Hélène Martin
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
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 © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 16: Linked Lists.
Programming Circular Linked List.
Definition and Application of Binary Trees
Doubly Linked List Review - We are writing this code
Linked lists.
Programming Abstractions
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 20: Binary Trees.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Recursion.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Programming Abstractions
Recursive Linked List Operations
Lecture 14 Linked Lists CSE /26/2018.
Linked Lists.
Lists.
Linked lists.
Linked Lists.
Presentation transcript:

LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1

Basic LL Node struct Node {int Num; Node * Next; } Node *Head, *Temp; Num Next 2

Traverse a Linked List (print) // Print each value of the list Temp = Head; while (Temp != NULL) { cout Num; Temp = Temp -> Next; } 3

Insert Node to Front of List if (Head == Null) // empty list { Head = new Node; Head -> Num = VAL; Head -> Next = NULL; } else // not empty list {Temp = new Node; Temp -> Num = VAL; Temp -> Next = Head; Head = Temp; } 4

Insert Node to Front of List - Revisited Could we eliminate the first case (Head == NULL)? That is, does the second case work with an empty or non-empty list? 5

Insert Node to Front of List if (Head == Null) // Can we eliminate this case? { Head = new Node; Head -> Num = VAL; Head -> Next = NULL; } else // Test this for empty list: Head == Null {Temp = new Node; Temp -> Num = VAL; Temp -> Next = Head; Head = Temp; } 6

Remove Front Node From List if (Head != NULL) { Temp = Head; Head = Head -> Next; delete Temp; Temp = NULL; } 7

Insert Node to End of List Node *P, *C; // for previous & current if (Head == NULL) { } //Use insert to front code; else {P = Head; C=Head -> Next; While (C != NULL) // find end of list { P = C; C = C -> Next; } P -> Next = new Node; P -> Next -> Num = VAL; P -> Next -> Next = Null; } 8

Remove Last Node in List if (Head != NULL) // empty list else if (Head -> Next == NULL) //one Node in list { delete Head; Nead = Null; } else // find end of list having at least 2 Nodes { P = Head; C = Head -> Next; While (C -> Next != NULL) // C will point to last Node { P = C; C = C -> Next; } delete C; C = Null; P -> Next = NULL; } 9

Insert Node (Val) to Ordered List (ascending) Temp = new Node; Temp -> Num = Val; //Case 1: Empty list – use code to insert to front if (Head == NULL) { } //Case 2: Val is smaller than first Node – insert to front else if (Val Num) { } //Case 3: Find correct location within the list else { // need to write this code } //Considerations: Search list comparing Val to Nodes already in list AND watching for end of list (NULL) 10

Insert Node in Ordered List (p.2) //Case 3: Find correct location within the list { P = NULL; C = Head; while (C -> Next != Null && C-> Num < Val) //move down list { P = C; C = C -> Next;} // found location – don’t know which condition stopped the loop if (C -> Num >= Val) // Insert between P & C { Temp -> Next = C; P -> Next = Temp; } else // Insert after C { C -> Next = Temp; Temp->Next = NULL; } 11

Remove Node (Val) from Linked List if (Head == Null) { return false; } // empty list else if (Head ->Num == Val) // delete first node { Temp = Head; Head = Head -> Next; delete Temp; Temp = Null; return true; } 12

Remove Node (Val) from Linked List else // must find val in list { P=Null; C = Head; while (C -> Next != Null && C-> Num < Val) { P = C; C = C -> Next;} if ( C -> Num == Val) // remove node { P -> Next = C -> Next; delete C; C = Null; return true; } else { return false; } // val not in list } 13

Linked List Class struct Node {int Num; Node * Next; } class LinkList { Node *Head; public: // put functions here } 14

Constructor LinkList ( ) { Head = NULL;} 15

Insert to Front Function void InsertFront (int Val) { Node * Temp; if (Head == Null) // empty list { Head = new Node; Head -> Num = VAL; Head -> Next = NULL; } else // not empty list {Temp = new Node; Temp -> Num = VAL; Temp -> Next = Head; Head = Temp; } } 16

Remove From Front of List bool RemoveFront (int &Val) { if (Head == Null) // Empty list return false; else if (Head != NULL) { Val = Head -> Num; Temp = Head; Head = Head -> Next; delete Temp; Temp = NULL; return true; } 17

Calls to Insert & Remove int main ( ) { LinkList MyList ( ); int X = 5, Y = 10, Z; bool B; MyList.InsertFront(X); MyList.InsertFront(Y); B = MyList.RemoveFront (Z); if (B) cout << Z; // only print if actually removed MyList.PrintList ( ); } 18

NOW, YOU DEVELOP YOUR OWN LINKED LIST CLASS USING WHAT WE HAVE DONE IN CLASS!!! Test with your own data. Test all functions thoroughly!!! 19

Linked List Application Consider a linked list consisting of a person’s last & first names & age, ordered by age. Modify the struct Node What function do you call to insert a new person to the list? How does the code in other member functions change? 20

New struct struct Node {string Lname; string Fname; int Age; // Num Node * Next; } Does the order of the attributes matter? If we use Num (instead of Age) will have less code changes. 21

Code Changes??? Every place that Num in a node is assigned, also need to assign last name & first name. 22

Constructor – any changes needed? LinkList ( ) { Head = NULL;} 23

Traverse to Print //Print each value of the list Temp = Head; while (Temp != NULL) { cout Lname Fname Num << endl; Temp = Temp -> Next; } 24

Insert Ordered – (Val, LN, FN) Temp = new Node; Temp -> Num = Val; Temp -> Lname = LN; Temp -> Fname = FN; if (Head == NULL) { // insert to front code here } else if (Val Num) { // insert to front code here } else { P = NULL; C = Head; while (C -> Next != Null && C-> Num < Val) //move down list { P = C; C = C -> Next;} if (C -> Num >= Val) //insert between P & C { Temp -> Next = C; P -> Next = Temp; } else // Insert after C { C -> Next = Temp; Temp->Next = NULL } } 25

Question? What happens if there is a “Tie” in the ages? That is, if 2 people have the same age which one is first in the list? 26