33
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Embed Size (px)

Citation preview

Page 1: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ

Linked Lists

Page 2: 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

nodeHead/tail pointers

Indicates start and end of the list

Page 3: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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 classstruct Node { T data; Node * next; Node(T newData):data(newData),next(NULL){}};

Page 4: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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){}};

Page 5: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Implementation – the List class

template<class T>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{ ... }

};

Page 6: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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

Page 7: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Essential Linked List Methods

Now give it a try!

Page 8: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Find Method

Node * find(int k, Node * curr){

}

Page 9: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Insert Method

void insert(int k, T data){

 

 

}

Page 10: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Remove Method

void remove(int k){

    

}

Page 11: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

getItem Method

T getItem(int k){  }

Page 12: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

InterleaveWith Function

For example, if interleaveWith is called on the list of integers 

List<int> list1 = < 10 11 12 13 > with parameter list 

List<int> list2 = < 20 21 22 23 24 25 26 > then the call to list1.interleaveWith(list2) should result in 

list1 == < 10 20 11 21 12 22 13 23 24 25 26 >

list2 == < >

Page 13: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

MixSplit Function

For example, if we have a list, original, of integers 

List<int> original = < 1 2 3 4 5 6 7 > and call List<int> returned = original.mixSplit(); The resulting list should be 

original == < 1 3 5 7 > and the returned list should be 

returned == < 2 4 6 >

Page 14: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Implementation – the sort() Function

In MP3, the sort() function was a member of the List class This means we don’t need to pass a List as a parameter, but

instead say myList.sort(), where myList is a List we createdOne 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 connectedNote: 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.

Page 15: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::sort() {

}

Page 16: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::sort() {

// Base case: we have a list of length 1 or 0if(length==1 || length==0) return;

// the rest of our code, yet to be written

}

Page 17: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::sort() {

// (we won’t show code from the previous slides)

List<T> left; // to hold the “left” halfList<T> right; // to hold the “right” half

}

Page 18: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort() – Dividing the list in two parts

We have to actually make these lists have meaning.

First, set the left list’s head to point to the current list’s 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 list’s head and tail pointers at the appropriate nodes

Finally, we will break the link between these two lists, making them independent

Page 19: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::sort() {

// (we won’t be showing the code from previous slides)

}

Page 20: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::sort() {

// (we won’t be showing the code from previous slides)

int mid = length/2;ListNode * curr = head;int pos = 1; // this is our position in the list

}

Page 21: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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 don’t 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

Don’t forget to modify the length variable

Page 22: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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

}

Page 23: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort() – Dividing the list in two parts

// loop until we reach the end of the listwhile(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++;

}

Page 24: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort() – Dividing the list in two parts

So what did we just do here? Lets draw it out

Page 25: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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

Page 26: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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

Page 27: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort – Merge

Lets assume we’ve 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

Page 28: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort – Merge

We aren’t 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<T>::merge(List<T> & left, List<T> & right) {

// our nodes still exist on the heap; we aren’t// going to delete them, but instead we will

remove // all ties this list has with the nodes

}

Page 29: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort – Merge

We aren’t 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<T>::merge(List<T> & left, List<T> & right) {

// our nodes still exist on the heap; we aren’t// going to delete them, but instead we will

remove // all ties this list has with the nodeshead=NULL;tail=NULL;length=0;

}

Page 30: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

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<T>::merge(List<T> & left, List<T> & right) { ListNode * currLeft = left.head;listNode * currRight = right.head

}

Page 31: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Sort – Merge

We won’t 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 sortedAgain, don’t forget details like adjusting the length,

and setting head, tail, next, and prev (if the list is doubly linked) pointers

Page 32: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Bonus Questions!

Page 33: PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists

Summing two Reversed Linked Lists