Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doubly Linked List Review - We are writing this code

Similar presentations


Presentation on theme: "Doubly Linked List Review - We are writing this code"— Presentation transcript:

1 Doubly Linked List Review - We are writing this code
for practice –as you need just a singly linked list. Suppose we want to use a doubly linked list as a queue. Recall a queue is like a line for a performance. We insert at the tail. We remove from the front.

2 class DoublyLinkedList {
int size; // Number of items in linked list Node* head; // First node in list Node* tail; // Last node in list public: DoublyLinkedList(void) { head=NULL; size=0;tail=NULL;} void add(int); int remove(); void clear(); bool isEmpty() {return head==NULL;} string toString(); }; // Node in a doubly linked list of ints class Node { public: int value; // data Node * next; // next Node Node * prev; // previous Node Node(int v, Node * anext=NULL, Node *aprev=NULL); }; wHAT What is this? What does an empty list look like?

3 Write the code to insert 10 random numbers between 0 and 100 into the list. Pseudo code is fine for now, but come as close as possible to real code. int main() { }

4 int main() { DoublyLinkedList list; for (int i = 0; i < 10; i++) list.add(rand()%100); cout << list.toString(); }

5 Write the code to create a string which
Contains the contents of the linked list.

6 Stringstream can be very useful
string DoublyLinkedList::toString(){ stringstream ss; ss << "DoublyLinkedList " << endl; return ss.str(); }

7 string DoublyLinkedList::toString(){ stringstream ss; ss << "DoublyLinkedList " << endl; for (Node * t = head; t != NULL; t = t->next) ss << t->value << " "; ss << endl; return ss.str(); }

8 Write the code to add to a linked list
// Add to the tail of the list void DoublyLinkedList::add(int w) { } 5 5

9 Write the code to add to a linked list
void DoublyLinkedList::add(int w) { Node* n = new Node(w,NULL,tail); //Can set previous here size++; if (DEBUG) cout << “add: Just added to list " << w << endl; if (head == NULL) { head = n; tail = n; return; } tail->next = n; Letting every routine talk to you is a great way of testing.

10 Write the code to remove the first element from a doubly linked list (return the value removed)
Draw pictures of what you are trying to do. It will help YOU and help others you communicate with. What are all the special cases?

11 Write the code to remove the first element from a doubly linked list
// Return the first thing in the queue and return it. int DoublyLinkedList::remove() { if (head == NULL) return 0; size--; int v = head->value; Node* temp = head; head = head->next; delete (temp); if (head == NULL) tail = NULL; else head->prev = NULL; return v; }

12 Write the code to remove from a doubly linked list
// Return the first thing in the queue and return it. int DoublyLinkedList::remove() { if (head == NULL) return 0; // Check for inability to remove anything;(A) size--; // Update class variables (B) int v = head->value; // Take care of return value (C) Node* temp = head; // clean up memory (D) head = head->next; // Set new head. (E) delete (temp); // clean up memory (D) if (head == NULL) // Set tail if queue is empty (F) tail = NULL; else head->prev = NULL; // Remove previous of current head. (G) return v; // Take care of return value (C) } Score your code. At one point each, what is your score?


Download ppt "Doubly Linked List Review - We are writing this code"

Similar presentations


Ads by Google