130 likes | 143 Views
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
E N D
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 • Last node has no linked node • Referenced by NULL • Structure of node declaration c++ 3
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
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
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
// 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
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;
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
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
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
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
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