Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists.

Similar presentations


Presentation on theme: "Lists."— Presentation transcript:

1 Lists

2 ADT (brief intro): Abstract Data Type A DESCRIPTION of a data type
The data type can be anything: lists, sets, trees, stacks, etc. What we want to do at the ADT level is describe what it is and what it should do We don’t worry about HOW it does it There’s no definite rule for what operations must be supported for each type Use what makes sense.

3 Lists: Things we know about lists: The items have an order
One comes after another - this doesn’t mean they’re “ordered” in any purposeful way, but there’s a built in order to the elements in a list The list has a size (n elements in the list) Data in a list can be duplicated All elements in the list are of the same data type

4 List operations we might want:
push(x)-add to end of list insert(x,k) adds item x to list at kth position remove (int x) – removes a node with data from the list removekth(int kth) – removes the node at the kth position from the list pop() – removes the last element from the list size() - gives you number of elements in list isEmpty() – returns true iff the list is empty find(x) – return the position of x in the list (usually -1 if not in list) findkth(k) – return the item at the kth position in the list printList() – you figure it out … I’m sure there are other things you’d want to be able to do with a list.

5 Arrays: Implementation of the ADT List All one type Sequential
Has a size Pros: Very easy to find the nth value in an array! Relatively easy to traverse Cons: Resizing!!! Adding elements Joining lists Removing elements (esp. from the middle) Finding if element x is in the list (does the number 42 occur anywhere in our list of random numbers?)

6 Linked List (versus Array):
Every element in a linked list consists of at a minimum 2 parts: The data A pointer to (aka the address of ) the element coming next in the list No fixed size (no limit on the number of elements in the list) No “wasted space” No empty spaces in between elements in the list (in an array you could have an “empty space” at an index, where you either didn’t initialize or you removed something Sequential access (as opposed to random access, like an array) This means that you have to start at the first node, and follow it to the second node, and follow that to the third node, etc., to find anything in a linked list, As opposed to an array, where you can jump to any index in an array (to get to arr[42], I don’t have to look at arr[41] first). Nodes in a linked list 2 NULL

7 Node implementation : class SNode {
friend class SLL; // allows another class to access private members of the Node class int data; SNode *next; public: SNode(int x); ~SNode(); }; //Node SNode::SNode(int x){ data = x; next = NULL; } //Constructor SNode::~SNode() { if (next != NULL) { cout << “deleting may cause memory leak.” << endl; } //if } //destuctor This is just the node’s class declaration and definition, not the linked list’s declaration and definition. This could just as easily have been a struct instead of a class (like in the video)

8 A list class (A list of nodes):
#include "SNode.hpp" class SLL { SNode *first; SNode *last; int size; public: SLL(); ~SLL(); void printSLL(); void addFirst(int x); void addAtFront(int x); void push(int x); void addAtK(int x, int k); void join(SLL *list2); int pop(); SNode *findKth(int k); int findK(int k); int remFirst(); int remKth(int k); }; This is a class for a linked list. The list consists of nodes. The list itself keeps track of the first node in the list To get to the second node, you go to the first node and follow the pointer in the first node’s next field. The class also has a last pointer That points to the last node in the linked list. And the class has size field that keeps track of the total number of nodes in the list at any time.

9 Linked List constructor:
Start with a Null list First pointer must point to NULL Last pointer must point to NULL Size must be set to 0 SLL::SLL(){ first = NULL; last = NULL; size = 0; } How would you add a first node? When we first instantiate a linked lists, there are no nodes. Since there are no nodes, we want to initialize the first and last pointer to NULL and the size to 0

10 Add first node to list? Create a brand new node (this is just creating a node): Snode *n = new Snode (x) //where x is the data part of the node (assume x is 3) Set the first pointer to point to the new node Set the last pointer to point to the new node Increase the size of the list by one How would you add to the end of the list? Code: void SLL::addFirst(int x) { size++; // the linked list’s size goes up by one first = new SNode (x); // makes a new node, //then sets first to point to the new node last = first; // there’s only one node – last and //first point to the same node //cout << " added first "; } 3 NULL first last

11 Add to the end of the list? push(int x)
Create a new node: Snode *n = new Snode (x) //where x is the data part of the node (assume x is 7) Set the last node in the list’s next pointer to the new node last->next = n Set the last pointer to point to the new node last = n; Increase the size of the list by one size += 1 How would you print the list? 3 NULL 7 NULL last Code: void SLL::push(int x) { Snode *n = new SNode (x); last->next = n; // makes the old //last’s next point to //the new node n last = n; // makes last point to the //new last (n) size += 1; // list size grew by one } first . 3 7 NULL last first . 3 7 NULL first last

12 printSLL() Remember: what connects each node to the next node is the *next pointer! Make a temp address – this holds the address of a node Start the temp address pointing to the first node in the list Print out the data in that node. Set the temp pointer to the next node in the list (by setting temp to hold the address of the next node in the list) Also known as making the temp pointer point to the next node in the list Continue making the temp pointer point to the next node in the list until you hit the end of the list Count could start at 0 and continue incrementing until it hits the size of the list Or (as seen) continue until the temp pointer is actually NULL void SLL::printSLL() { SNode *tmp = first; while (tmp != NULL) { cout << tmp->data << “, ”; tmp = tmp->next; } cout << endl; First 2 NULL Last

13 void addAtK(x,k); Adds value x at location k
Inserting node with data, x= 9 at location k = 2; first last NULL SNode *n = new SNode(9); n->next = node4->next; node4->next = n; Would this work? (hint: no!!! – garbage is created here!!! As well as a strange loop) SNode *n = new SNode(9); node4->next = n; n->next = node4->next; How do we get to the kth position in the list? Loop! Do we loop to the kth position or the k-1th position? Why?

14 addAtK(x,k) void SLL::addAtK(int x, int k){ SNode *tmp = first; if (k==0) { addAtFront(x); size++; } if (k < size && k >= 0) { for (int i = 0; i < k-1; i++) { tmp = tmp->next; // what type is the next field? ALWAYS keep this in mind… SNode *tmp2 = tmp->next; tmp->next = new SNode(x); tmp->next->next = tmp2; What if we did this with an array?

15 Assume we have the following linked list:
NULL First Last void SLL::showstuff() { SNode *tmp = first; int ct = 0; while (tmp != NULL) { if (ct%2 !=0) { cout << tmp->data; } ct++; tmp = tmp->next; cout << endl;

16 Assume we have the following linked list (data type is a character instead of an int):
s a n r o r d a e y NULL First Last void SLL::showstuff() { SNode *tmp = first->next; while (tmp != NULL ) { cout << tmp->data; if (tmp->next !=NULL) { tmp = tmp->next->next; } else { tmp = NULL; cout << endl;

17 Assume we have the following linked list:
s p l u o p t p h y NULL First Last void SLL::showstuff() { SNode *tmp = first; while (tmp != NULL && tmp->next != NULL) { tmp->next = tmp->next->next; tmp = tmp->next; } if (tmp != NULL) { tmp->next = NULL; last = tmp; tmp = first; while (tmp != NULL){ cout << tmp->data; cout << endl;

18 Pop: Removing the last value from the list
Does the last pointer help us? Steps? Loop through the list to find the value BEFORE the last value in the list using a tmp pointer Have a variable hold the data in the last node (int x = last->data) Delete the last node (delete last) Set the last pointer to point to the value before the last node (last = tmp) Make the last node’s next pointer point to NULL (last->next = NULL) Decrease the size Return the value temp First Last 2 NULL Last First 2 NULL temp First temp Last First NULL temp Last

19 Pop() int SLL::pop() { if (size > 0) { SNode *tmp = first;
for (int i = 0; i < size-1;i++) { tmp = tmp->next; } int x = last->data; delete last; last = tmp; last->next = NULL; size --; return x; else { return -1; To reset the last pointer, we always have to loop through the entire linked list to find the node before the current last node Because this node will become the new last node when the last node is deleted. Currently we have no way of going backwards in the list.

20 RemoveKth? Remove the third node (8):
tmp tmp2 7 3 8 1 6 To remove the node with 8 in it: Loop to the node before it (the one with 3 in it) – tmp now points to 3 node Set a second temp pointer to node with 8 (tmp2 now points to 8 node) Make 3’s next pointer point to 8’s next pointer (or 3’s next’s next pointer) (tmp->next = tmp2->next) Delete tmp2 (the 8 node) Decrease the size

21 RemoveKth(int k) int SLL::remKth(int k) {
if (k < size && k >= 0) { SNode *tmp = first; for (int i = 0; i < k -1; i++) { tmp = tmp->next; } int x = tmp->next->data; SNode *tmp2= tmp->next; tmp->next = tmp->next->next; delete tmp2; return x; You must loop to the node BEFORE the node to be removed. If you loop to the kth node, there’s no way to connect the node before the kth node to the node after the kth node

22 How would you reverse a list?
UGH!


Download ppt "Lists."

Similar presentations


Ads by Google