1 Array vs. Linked List Array vs. Linked List Pointers & Nodes Pointers & Nodes Singly linked list Singly linked list Doubly linked list Doubly linked

Download 1 Array vs. Linked List Array vs. Linked List Pointers & Nodes Pointers & Nodes Singly linked list Singly linked list Doubly linked list Doubly linked

Post on 30-Mar-2015

221 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

1 Array vs. Linked List Array vs. Linked List Pointers & Nodes Pointers & Nodes Singly linked list Singly linked list Doubly linked list Doubly linked list Circular linked lists Circular linked lists Header (dummy) node Header (dummy) node Implementing ADTs Implementing ADTs Stack Stack Queue Queue CSE 30331 Lecture 12 Linked Lists Slide 2 2 Reading Linked Lists Ford: Ch 9 Slide 3 3 Array vs. Linked List Arrays (& STL vectors) Direct access by index O(1) Insertion & deletion requires shifting O(n) Dynamically resizable only at one end May require copying of all values Pop & push at end O(1) Unless resizing is involved Linked Lists ( & STL lists) Sequential access O(n) Insertion & deletion without shifting O(1) Dynamically resizable anywhere O(1) Pop & push at either end O(1) Slide 4 4 Abstraction of a Linked List frontback Singly Linked List Doubly Linked List Slide 5 5 Linked List Nodes Each Node is like a piece of a chain To insert a new link, break the chain at the desired location and simply reconnect at both ends of the new piece. Slide 6 6 Linked List Nodes Removal is like Insertion in reverse. Slide 7 7 Node Composition (singly linked list) An individual Node is composed of two parts a Data field containing the data stored by the node a Pointer field containing the address of the next Node in the list. Slide 8 8 Inserting at the Front of an empty Singly Linked List Slide 9 9 Inserting at the Front of a nonempty Singly Linked List Slide 10 10 Deleting From the Front of a Singly Linked List front // front = NULL Deleting front of a 1-node list Slide 11 11 Deleting From the Front of a Singly Linked List front Deleting front of a multi-node list // front = front->next Slide 12 12 Removing a Target Node front target prevcurr // next Slide 13 13 A stack as a NULL terminated singly linked list Slide 14 14 Node data structure // forward declaration, just to keep the compiler happy template class Stack; template class Node { friend class Stack ; // let Stack access data & next public: Node(T value = T()) : data(value), next(NULL) { } private: T data; Node *next; }; Slide 15 15 Stack as a Singly Linked List template class Stack { public: Stack(); ~Stack(); bool empty(); // true or false, status of stack T top(); // return copy of top nodes value void pop(); // remove top node void push(T value); // create new top node with value void clear(); // remove all nodes from stack private: Node *topNode; }; Slide 16 16 Stack member functions template Stack ::Stack() : topNode(NULL) { } template Stack ::~Stack() { clear(); // make sure all memory is freed } template bool Stack ::empty() { return (NULL == topNode); } Slide 17 17 Stack member functions template T Stack ::top(){ if (empty()) throw(in top() with Empty Stack); return topNode->data; } template void Stack ::pop() { if (! empty()) { Node *temp = topNode; // point to top node topNode = topNode->next; // point around to next node delete temp; // free node memory } Slide 18 18 Stack member functions template void Stack ::push(T value) { Node *temp = new Node (value); // create new Node temp->next = topNode; // new node points to top Node topNode = temp; // now new node IS top Node } template void Stack ::clear() { while(! empty()) pop(); } Slide 19 > name; // get first name while (name != "done") { if (name == "purge") S.clear(); else S.push(name); cin >> name; // get another name } cout data; } template void Queue ::pop"> 24 Queue member functions template T Queue ::front(){ if (empty()) throw(in front() with Empty Queue); return first->data; } template void Queue ::pop() { if (! empty()) { Node *temp = first; // point to first node first = first->next; // point around to next node delete temp; // free node memory } Slide 25 25 Queue member functions template void Queue ::push(T value) { Node *temp = new Node (value); // create new Node if (empty()) first = temp; // new last node is also first node else last->next = temp; // new last node follows old last = temp; // now new node IS last Node } template void Queue ::clear() { while(! empty()) pop(); } Slide 26 26 Doubly Linked Lists Singly linked list only allow easy traversal in one direction (forward) Doubly linked lists allow easy traversal both directions (forward and backward) The list can be linear Having NULL pointers at both ends The list can be circular Having each end point back to the other Usually this is implemented with a header node That contains no data That points to itself when list is empty Slide 27 27 Circular Doubly Linked Lists A Watch Band provides a good Real Life analogue for this Data Structure Slide 28 28 Circular Doubly Linked Lists Implemented on a Computer it might look something like this. Slide 29 29 Empty and Non Empty Doubly Linked List Slide 30 30 Implementing a Circular Doubly Linked List // forward declaration, just to keep the compiler happy template class LinkedList; template class Node { friend class LinkedList ; // let LinkedList access data & next public: Node(T value = T()) : data(value), next(NULL), prev(NULL) { } private: T data; Node *next, *prev; }; Slide 31 31 LinkedList (Circular and Doubly Linked) template class LinkedList { public: LinkedList(); ~LinkedList(); int size(); // number of nodes in list T get(int pos); // return copy of value at pos void erase(int pos); // remove first node void insert(T value, int pos); // create new node with value at pos void clear(); // remove all nodes from queue int find(T value); // return position of first node with value private: Node *last, // indicate beginning and end of list *current; // indicates current node int numNodes, // number of nodes in list currentPos; // indicates current position void moveTo(int pos); // moves current to desired Node }; Slide 32 32 LinkedList member functions template LinkedList ::LinkedList() : numNodes(0), currentPos(-1) { last = new Node ; last->next = last; last->prev = last; current = last; // point to the header } template LinkedList ::~LinkedList() { clear(); // make sure all Node memory is freed delete last; // free the header Node memory } Slide 33 33 LinkedList member functions template int LinkedList ::size() { return numNodes; } template void LinkedList ::clear() { while(size() > 0) erase(0); // remove all nodes } Slide 34 34 LinkedList member functions template void LinkedList ::moveTo(int pos) { if ((pos < 0)|| (size() next; currentPos++; } while (currentPos > pos) // move backward along list { current = current->prev; currentPos--; } Slide 35 35 LinkedList member functions template T LinkedList ::get(int pos) { try { moveTo(pos); // move to indicated position in list } catch (exception &e) { cerr 36 LinkedList member functions template int LinkedList ::find(T value) { current = last->next; // point to first Node currentPos = 0; while (current != last) // move forward along list { if (current->data == value) break; current = current->next; currentPos++; } if (current == last) currentPos = -1; return currentPos; } Slide 37 37 Deleting a Node at a Position // unlink the node (*curr) from the list curr->prev->next = curr->next; curr->next->prev = curr->prev; delete curr; Slide 38 38 LinkedList member functions template void LinkedList ::erase(int pos) { try { moveTo(pos); // move to indicated position in list } catch (exception &e) { cerr next = current->next; // point around Node forward current->next->prev = current->prev; // point around Node backward delete current; // free Node memory } Slide 39 39 Inserting a Node at a Position // insert newNode before curr newNode->prev = curr->prev; newNode->next = curr; curr->prev->next = newNode; curr->prev = newNode; Slide 40 40 LinkedList member functions template void LinkedList ::insert(T value, int pos) { try { moveTo(pos); // move to indicated position in list } catch (exception &e) { cerr prev; // point back at previous Node newNode->next = current; // have it point to new Node current->prev->next = newNode; // point forward to next Node current->prev = newNode; // have it point to new Node } Slide 41 41 So what is missing? Copy Constructor & Assignment Operator Need a deep copy Traverse source list Copy each node Insert in destination list Comparison Operator (==) Compare list lengths, and if equal Traverse both lists Compare corresponding nodes Slide 42 42 Copy Constructor template LinkedList ::LinkedList(LinkedList & theList) : numNodes(0), currentPos(-1) { last = new Node ; last->next = last; last->prev = last; current = last; // point to the header int pos(0); // start at dummy node while (pos < theList.size()) // while nodes in theList { this->insert(theList.get(pos),pos)); // copy node and insert pos++; // move forward } Slide 43 43 Assignment Operator (=) template LinkedList & LinkedList ::operator=(LinkedList & theList) { if (this == &theList) return; // same lists, nothing to do LinkedList *newList; int pos(0); // start at dummy node while (pos < theList.size()) // while nodes in theList { newList.insert(theList.get(pos),pos)); // copy node and insert pos++; // move forward } return newList; } Slide 44 44 Comparison (==) template bool LinkedList ::operator==(LinkedList & theList) { if (this == &theList) return true; // same lists, nothing to do if (this->size() != theList.size()) return false; // lists have different number of values bool same(true); // assume the same int pos(0); // start at first node while (same && (pos < size()) // while same and more nodes { if (this->get(pos) != theList.get(pos)) same = false; // nodes differ so lists do, too pos++; // move forward } return same; } Slide 45 45 Comparison (==) (more efficient, without function calls) template bool LinkedList ::operator==(LinkedList & theList) { i

Recommended

View more >