PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Chapter 17 Linked Lists.
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
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.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
Introduction to Computer Science Recursive Link Operations MergeSort Unit 18.
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.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
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.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 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.
Linked List by Chapter 5 Linked List by
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Java linked list.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
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.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Data Structure Dr. Mohamed Khafagy.
Linked lists.
ENEE150 Discussion 13 Section 0101 Adam Wang.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Chapter 4 Linked Lists
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
Linked Lists.
Chapter 4 Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Presentation transcript:

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

The Linked List Class Nodes Value – the data that the node contains Pointers – point to the next and/or previous node Head/tail pointers Indicates start and end of the list

Implementation - Nodes Node struct contained within a Linked List class Constructor – sets data to newData, next to NULL T is the data type according to the template Typically an int in the context of this course, but can be any data type or class struct Node { T data; Node * next; Node(T newData):data(newData),next(NULL){} };

What is a struct? Think of it as a public class An object that contains member variables and functions data and next are variables, the constructor is a function A node is just one piece of the entire list Just contains a value (our data) and a pointer to the next node Node(3) constructs the node to the right struct Node { T data; Node * next; Node(T newData):data(newData),next(NULL){} };

Implementation – the List class template class List{ public: List():size(0), head(NULL) {} ~List(); int size(); void insert(int loc, T data); void remove(int loc); T const & getItem(int loc) const; Private: Node* head; Node * find(int k, Node * curr); struct Node{... } };

Essential Linked List Methods Insert– create a node and put it somewhere in the list (public) Remove – get rid of a node, link the neighboring nodes to keep list intact (public) Get Item – return the value of a node at some arbitrary position in the list (public) Find – return a pointer to a node at some arbitrary position in the list (private) helper function for the other three methods

Essential Linked List Methods Now give it a try!

Find Method Node * find(int k, Node * curr) { }

Insert Method void insert(int k, T data) { }

Remove Method void remove(int k) { }

getItem Method T getItem(int k) { }

InterleaveWith Function For example, if interleaveWith is called on the list of integers List list1 = with parameter list List list2 = then the call to list1.interleaveWith(list2) should result in list1 == list2 ==

MixSplit Function For example, if we have a list, original, of integers List original = and call List returned = original.mixSplit(); The resulting list should be original == and the returned list should be returned ==

Implementation – the sort() Function In MP3, the sort() function was a member of the List class This means we dont need to pass a List as a parameter, but instead say myList.sort(), where myList is a List we created One of the requirements for sort() was that we could NOT allocate new listNodes We cannot create new nodes, but we can modify the way these nodes are connected Note: Before you start writing your own code, know that there are multiple ways to implement the same thing. We will be showing our implementation, but you can take a different path as long as it works and makes sense to you.

Sort() – base case For our base case, we must check if the list has length 0 or 1. We had a length member variable, so this is straightforward. void List ::sort() { }

Sort() – base case For our base case, we must check if the list has length 0 or 1. We had a length member variable, so this is straightforward. void List ::sort() { // Base case: we have a list of length 1 or 0 if(length==1 || length==0) return; // the rest of our code, yet to be written }

Sort() – Dividing the list in two parts Now we have to create two new lists. We will put half of the nodes in the first, and the other half in the second. void List ::sort() { // (we wont show code from the previous slides) List left; // to hold the left half List right; // to hold the right half }

Sort() – Dividing the list in two parts We have to actually make these lists have meaning. First, set the left lists head to point to the current lists head Our strategy was to create a pointer curr, originally pointing at the head of the current list curr will traverse the list, setting the left and right lists head and tail pointers at the appropriate nodes Finally, we will break the link between these two lists, making them independent

Sort() – Dividing the list in two parts How do we implement this in code? Lets set up a few things: Something to hold the midpoint The curr pointer used to traverse a list Something to keep track of our position in the list void List ::sort() { // (we wont be showing the code from previous slides) }

Sort() – Dividing the list in two parts How do we implement this in code? Lets set up a few things: Something to hold the midpoint The curr pointer used to traverse a list Something to keep track of our position in the list void List ::sort() { // (we wont be showing the code from previous slides) int mid = length/2; ListNode * curr = head; int pos = 1; // this is our position in the list }

Sort() – Dividing the list in two parts Now lets use curr to move down the list, and set up the left and right lists The key thing to remember is setting the head and tail pointers for the lists We dont need to change the next pointers very much, because the nodes are already linked together We have know the midpoint, and we keep track of the position in the list, so its easy to know where these heads/tails should be Dont forget to modify the length variable

Sort() – Dividing the list in two parts Write a loop to set up the left and right lists There are multiple ways to do this, but this was the setup we chose (feel free to do your own thing if it works) // loop until we reach the end of the list while(){ if(pos <= mid){ // do something } else { // do something else } curr = curr->next; pos++; }

Sort() – Dividing the list in two parts // loop until we reach the end of the list while(curr != NULL){ if(pos <= mid){ if(pos == 1) left.head = curr; if(pos == mid) left.tail = curr; left.length++; } else { if(pos == mid+1) right.head = curr; if(pos == length) right.tail = curr; right.length++; } curr = curr->next; pos++; }

Sort() – Dividing the list in two parts So what did we just do here? Lets draw it out

Sort() – Dividing the list in two parts No we just need to break the link between the two lists, figuratively speaking left.tail->next = NULL; right.head->prev = NULL; // good, now our left and right lists are separated

Sort – the Recursive Part We have successfully divided our list into two parts. Now we should sort them. left.sort(); right.sort(); // well that was easy

Sort – Merge Lets assume weve written a helper function called merge(list1, list2) which will merge two lists back together in the proper order. Our two lists are sorted, so now we call merge(list1, list2) on these lists merge(left, right); // cool

Sort – Merge We arent going to go into the details of merge(), but just the general ideas when it comes to merging on a linked list. We have the information about the left and right lists saved already, so lets just start with a blank slate. void List ::merge(List & left, List & right) { // our nodes still exist on the heap; we arent // going to delete them, but instead we will remove // all ties this list has with the nodes }

Sort – Merge We arent going to go into the details of merge(), but just the general ideas when it comes to merging on a linked list. We have the information about the left and right lists saved already, so lets just start with a blank slate. void List ::merge(List & left, List & right) { // our nodes still exist on the heap; we arent // going to delete them, but instead we will remove // all ties this list has with the nodes head=NULL; tail=NULL; length=0; }

Sort – Merge Now we should make some pointers to point to the heads of both the left and right lists. Lets call them currLeft, and currRight; these will be used to traverse the list and make comparisons between nodes void List ::merge(List & left, List & right) { ListNode * currLeft = left.head; listNode * currRight = right.head }

Sort – Merge We wont go into the rest of the merge implementation in too much detail You should compare the values of the nodes pointed at by currLeft and currRight. The node with the lesser value should become the new tail of the original list Whenever currLeft or currRight becomes the tail of the new list, you should have that pointer move down a node By doing this, the original list will become sorted Again, dont forget details like adjusting the length, and setting head, tail, next, and prev (if the list is doubly linked) pointers

Bonus Questions!

Summing two Reversed Linked Lists