Computer notes data structures - 9

1,146 views
1,072 views

Published on

Published in: Technology, Design
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,146
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
86
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • End of lecture 8
  • End of lecture 9
  • Computer notes data structures - 9

    1. 1. Class No.09 Data Structures http://ecomputernotes.com
    2. 2. Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (excel) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
    3. 3. Stack Layout during a call <ul><li>Here is stack layout when function F calls function G: </li></ul>During execution of G After call At point of call http://ecomputernotes.com Parameters(F) Local variables(F) Return address(F) Parameters(G) Parameters(F) Local variables(F) Return address(F) Parameters(F) Local variables(F) Return address(F) Parameters(G) Local variables(G) Return address(G) sp sp sp
    4. 4. Queues <ul><li>A stack is LIFO (Last-In First Out) structure. </li></ul><ul><li>In contrast, a queue is a FIFO (First-In First-Out ) structure. </li></ul><ul><li>A queue is a linear structure for which items can be only inserted at one end and removed at another end. </li></ul>http://ecomputernotes.com
    5. 5. Queue Operations <ul><li>Enqueue(X) – place X at the rear of the queue. </li></ul><ul><li>Dequeue() -- remove the front element and return it. </li></ul><ul><li>Front() -- return front element without removing it. </li></ul><ul><li>IsEmpty() -- return TRUE if queue is empty, FALSE otherwise </li></ul>http://ecomputernotes.com
    6. 6. Implementing Queue <ul><li>Using linked List: Recall </li></ul><ul><li>Insert works in constant time for either end of a linked list. </li></ul><ul><li>Remove works in constant time only. </li></ul><ul><li>Seems best that head of the linked list be the front of the queue so that all removes will be from the front. </li></ul><ul><li>Inserts will be at the end of the list. </li></ul>http://ecomputernotes.com
    7. 7. Implementing Queue <ul><li>Using linked List: </li></ul>front 2 5 7 1 7 5 2 front rear rear http://ecomputernotes.com 1
    8. 8. Implementing Queue <ul><li>Using linked List: </li></ul>front 2 5 7 1 7 5 2 front rear rear front 2 5 7 7 5 2 front rear rear dequeue() http://ecomputernotes.com 1 1
    9. 9. Implementing Queue <ul><li>Using linked List: </li></ul>front 2 5 7 1 7 5 2 front rear rear front 2 5 7 9 7 5 2 front rear rear enqueue(9) 9 http://ecomputernotes.com 1
    10. 10. Implementing Queue <ul><li>int dequeue() </li></ul><ul><li>{ </li></ul><ul><li>int x = front->get(); </li></ul><ul><li>Node* p = front; </li></ul><ul><li>front = front->getNext(); </li></ul><ul><li>delete p; </li></ul><ul><li>return x; </li></ul><ul><li>} </li></ul><ul><li>void enqueue(int x) </li></ul><ul><li>{ </li></ul><ul><li>Node* newNode = new Node(); </li></ul><ul><li>newNode->set(x); </li></ul><ul><li>newNode->setNext(NULL); </li></ul><ul><li> rear->setNext(newNode); </li></ul><ul><li>rear = newNode; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    11. 11. Implementing Queue <ul><li>int front() </li></ul><ul><li>{ </li></ul><ul><li>return front->get(); </li></ul><ul><li>} </li></ul><ul><li>int isEmpty() </li></ul><ul><li>{ </li></ul><ul><li>return ( front == NULL ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    12. 12. Queue using Array <ul><li>If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive. </li></ul><ul><li>This is because we may have to shift up to “n” elements. </li></ul><ul><li>For the stack, we needed only one end; for queue we need both. </li></ul><ul><li>To get around this, we will not shift upon removal of an element. </li></ul>http://ecomputernotes.com
    13. 13. Queue using Array front 2 5 7 1 rear http://ecomputernotes.com 6 5 7 0 0 1 3 2 4 front 1 7 5 2 3 rear
    14. 14. Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 4 rear enqueue(6) 6 6 http://ecomputernotes.com
    15. 15. Queue using Array front 2 5 7 1 rear 6 5 7 0 0 1 3 2 4 front 1 7 5 2 5 rear enqueue(8) 6 6 8 8 http://ecomputernotes.com
    16. 16. Queue using Array front 2 5 7 rear 6 5 7 1 0 1 3 2 4 front 7 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
    17. 17. Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 5 rear dequeue() 6 6 8 8 http://ecomputernotes.com
    18. 18. Queue using Array front 2 5 rear 6 5 7 2 0 1 3 2 4 front 5 2 7 rear enqueue(9) enqueue(12) 6 6 8 8 9 9 12 12 enqueue(21) ?? http://ecomputernotes.com
    19. 19. Queue using Array <ul><li>We have inserts and removal running in constant time but we created a new problem. </li></ul><ul><li>Cannot insert new elements even though there are two places available at the start of the array. </li></ul><ul><li>Solution: allow the queue to “wrap around”. </li></ul>http://ecomputernotes.com
    20. 20. Queue using Array <ul><li>Basic idea is to picture the array as a circular array . </li></ul>front 2 5 rear 2 front 7 rear 6 8 9 12 http://ecomputernotes.com 6 5 7 0 1 3 2 4 5 2 6 8 9 12
    21. 21. Queue using Array <ul><li>void enqueue(int x) </li></ul><ul><li>{ </li></ul><ul><li>rear = (rear+1)%size; </li></ul><ul><li>array[rear] = x; </li></ul><ul><li> noElements = noElements+1; </li></ul><ul><li>} </li></ul>front 2 5 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(21) 21 21 7 noElements http://ecomputernotes.com 2 front 0 rear 8 size
    22. 22. Queue using Array <ul><li>int isFull() </li></ul><ul><li>{ </li></ul><ul><li>return noElements == size; </li></ul><ul><li>} </li></ul><ul><li>int isEmpty() </li></ul><ul><li>{ </li></ul><ul><li> return noElements == 0; </li></ul><ul><li>} </li></ul>front 2 5 rear 6 8 9 12 6 5 7 0 1 3 2 4 5 2 6 8 9 12 enqueue(7) 21 21 8 noElements 7 7 http://ecomputernotes.com 2 front 1 rear 8 size
    23. 23. Queue using Array <ul><li>int dequeue() </li></ul><ul><li>{ </li></ul><ul><li>int x = array[front]; </li></ul><ul><li> front = (front+1)%size; </li></ul><ul><li> noElements = noElements-1; </li></ul><ul><li> return x; </li></ul><ul><li>} </li></ul>front rear 6 8 9 12 6 5 7 0 1 3 2 4 6 8 9 12 dequeue() 21 21 6 noElements 7 7 http://ecomputernotes.com 4 front 1 rear 8 size
    24. 24. Use of Queues <ul><li>Out of the numerous uses of the queues, one of the most useful is simulation . </li></ul><ul><li>A simulation program attempts to model a real-world phenomenon. </li></ul><ul><li>Many popular video games are simulations, e.g., SimCity, FlightSimulator </li></ul><ul><li>Each object and action in the simulation has a counterpart in real world. </li></ul>http://ecomputernotes.com
    25. 25. Uses of Queues <ul><li>If the simulation is accurate, the result of the program should mirror the results of the real-world event. </li></ul><ul><li>Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. </li></ul><ul><li>Let us look at an example. Suppose there is a bank with four tellers. </li></ul>http://ecomputernotes.com
    26. 26. Simulation of a Bank <ul><li>A customer enters the bank at a specific time (t 1 ) desiring to conduct a transaction. </li></ul><ul><li>Any one of the four tellers can attend to the customer. </li></ul><ul><li>The transaction (withdraw, deposit) will take a certain period of time (t 2 ). </li></ul><ul><li>If a teller is free, the teller can process the customer’s transaction immediately and the customer leaves the bank at t 1 +t 2 . </li></ul>http://ecomputernotes.com

    ×