Download presentation
Presentation is loading. Please wait.
Published byVernon Anthony Modified over 9 years ago
1
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures
2
© M. Gross, ETH Zürich, 2014 Agenda Linked Lists Queues Stacks Exercise
3
© M. Gross, ETH Zürich, 2014 3 Linked Lists 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++ struct nodeType { int data; nodeType *next; }; nodeType *head; head
4
© M. Gross, ETH Zürich, 2014 4 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 nodeType *current; current = head; while (current != NULL) { cout data 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;
5
© M. Gross, ETH Zürich, 2014 5 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 nodeType *newNode; newNode = new nodeType; newNode->data = 37; newNode->next = current->next; current- >next = newNode;
6
© M. Gross, ETH Zürich, 2014 6 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 nodeType *deleteNode; deleteNode = current->next; current->next = deleteNode->next; delete deleteNode;
7
© M. Gross, ETH Zürich, 2014 7 Building Linked Lists Backward approach // 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 }
8
© M. Gross, ETH Zürich, 2014 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 8 struct nodeType { int data; nodeType *next; nodeType *prev; }; nodeType *head;
9
© M. Gross, ETH Zürich, 2014 9 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 a1a1 a2a2 a3a3 anan null headtail
10
© M. Gross, ETH Zürich, 2014 10 Queues add element remove element v a1a1 a2a2 a3a3 headtail null tail->next = v; tail = v; v -> next = NULL; Node *v = head; head = head->next; delete v; a1a1 a2a2 a3a3 null head tail v
11
© M. Gross, ETH Zürich, 2014 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! a0a0 a1a1 a2a2 a3a3 a4a4 top
12
© M. Gross, ETH Zürich, 2014 12 Stacks add element remove element v a1a1 a2a2 a3a3 head null v->next = head; head = v; Node *v = head; head = head->next; delete v; a1a1 a2a2 a3a3 null head v
13
© M. Gross, ETH Zürich, 2014 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.