1 / 13

Exercise 11 – Data Structures

Exercise 11 – Data Structures. Agenda. Linked Lists Queues Stacks Exercise. struct nodeType { int data; nodeType *next; }; nodeType *head;. Linked Lists. head. Collection of nodes Two components for each node to store the data to store the “next” linked node

napolitano
Download Presentation

Exercise 11 – Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise 11 –Data Structures

  2. Agenda • Linked Lists • Queues • Stacks • Exercise

  3. struct nodeType { int data; nodeType *next; }; nodeType *head; Linked Lists head • Collection of nodes • Two components for each node • to store the data • to store the “next” linked node • Last node has no linked node • Referenced by NULL • Structure of node declaration c++ 3

  4. nodeType *current; current = head; while (current != NULL) { cout << current->data << " "; current = current->next; } nodeType *current; current = head; int element = 12; while (current != NULL) { if(current->data == element) break; current = current->next; } if(current == NULL) cout <<"element not found“ << endl; Linked Lists Operations • Traversing a list • Use other pointer similar to *head not to lose reference to beginning of list • Finding an element in the list • Traverse the list until you find the element (here 12) • Do something if not found 4

  5. nodeType *newNode; newNode = new nodeType; newNode->data = 37; newNode->next = current->next; current->next = newNode; Linked Lists Operations • Inserting a new node • Create a new node • Determine insertion position and assign it to current • Make the new node point to the same node that current is pointing to • Make current now point to the new node 5

  6. nodeType *deleteNode; deleteNode = current->next; current->next = deleteNode->next; delete deleteNode; Linked Lists Operations • Deleting a node • Determine the node to be removed (deleteNode) and point current to the node prior to that node • Create a pointer to point to the node you want to delete (i.e *deleteNode) • Make current’s link now point to deleteNode’s link • delete the node from memory Assuming current is at correct position 6

  7. // Init the list nodeType *head; nodeType *newNode; int num; head = NULL; // Build the list using the backward approach for (int i=0; i<3; i++) { cout << "Enter number :"; cin >> num; newNode = new nodeType; // Create the new node newNode->data = num; // and assign its data value newNode->next = head; // make its link point to the // front of the list head = newNode; // make the head now be the // newNode } Building Linked Lists • Backward approach 7

  8. Doubly Linked Lists • Similar to single linked lists but • Each node has a connection to the previous one • List can be traversed also backwards • Can reach a3 from a1 and vice versa structnodeType { int data; nodeType *next; nodeType *prev; }; nodeType *head;

  9. a1 a2 an Queues • Like linked lists • Add new elements at the end • Remove elements from the head struct element { int value; // The value of the element struct element *next; // Pointer to the next element }; typedef struct element Node; Node *head = NULL; //Pointer to first element Node *tail = NULL; //Pointer to last element a3 null head tail 9

  10. v a2 a2 a1 Queues a1 a3 • add element • remove element null tail->next = v; tail = v; v -> next = NULL; head tail a3 v null Node *v = head; head = head->next; delete v; head tail 10

  11. Stacks • “One-sided queues” • Add to and remove from the “top” by pushing/popping • The only element that is reachable is the top element! top a4 a3 a2 a1 a0 11

  12. v a2 a2 a1 Stacks • add element • remove element a1 a3 v->next = head; head = v; null head a3 v Node *v = head; head = head->next; delete v; null head 12

  13. Stacks vs Queues • Queues: • Work in FIFO (first-in-first-out) fashion • Store two pointers • Add/removal using different pointers • Operating systems: Jobs wait in a queue for CPU time • Printers: Incoming jobs end up in a queue • Stacks: • Work in LIFO (last-in-first-out) fashion • Store only one pointer • Add/removal from top only • Evaluating Arithmetic Operations • Function calls: They are put in a stack by the compiler 13

More Related