Lecture No 09 Data Structures Dr Sohail Aslam
- Slides: 26
Lecture No. 09 Data Structures Dr. Sohail Aslam
Memory Organization Process 1 (browser) Code Process 3 (word) Static data Process 4 (excel) Stack Process 2 (dev-c++) Windows OS Heap
Stack Layout during a call § Here is stack layout when function F calls function G: Parameters(F) sp Parameters(F) Local variables(F) Return address(F) Parameters(G) sp Local variables(G) sp At point of call Return address(G) During execution of G After call
Queues § A stack is LIFO (Last-In First Out) structure. § In contrast, a queue is a FIFO (First-In First-Out ) structure. § A queue is a linear structure for which items can be only inserted at one end and removed at another end.
Queue Operations Enqueue(X) – place X at the rear of the queue. Dequeue() -- remove the front element and return it. Front() -return front element without removing it. Is. Empty() -return TRUE if queue is empty, FALSE otherwise
Implementing Queue § Using linked List: Recall § Insert works in constant time for either end of a linked list. § Remove works in constant time only. § Seems best that head of the linked list be the front of the queue so that all removes will be from the front. § Inserts will be at the end of the list.
Implementing Queue § Using linked List: front 1 7 rear 5 2 rear front 1 7 5 2
Implementing Queue § Using linked List: front rear 1 7 5 2 rear front 1 7 5 2 dequeue() front 7 rear 5 2 rear front 1 7 5 2
Implementing Queue § Using linked List: front 1 7 5 rear front rear 1 2 7 5 2 enqueue(9) front 7 rear 5 2 9 rear front 7 5 2 9
Implementing Queue int dequeue() { int x = front->get(); Node* p = front; front = front->get. Next(); delete p; return x; } void enqueue(int x) { Node* new. Node = new Node(); new. Node->set(x); new. Node->set. Next(NULL); rear->set. Next(new. Node); rear = new. Node; }
Implementing Queue int front() { return front->get(); } int is. Empty() { return ( front == NULL ); }
Queue using Array § If we use an array to hold queue elements, both insertions and removal at the front (start) of the array are expensive. § This is because we may have to shift up to “n” elements. § For the stack, we needed only one end; for queue we need both. § To get around this, we will not shift upon removal of an element.
Queue using Array front 1 7 rear 5 2 1 0 7 1 5 2 2 3 4 front rear 0 3 5 6 7
Queue using Array enqueue(6) front 1 7 rear 5 2 6 1 0 7 1 front 0 5 2 6 2 3 4 rear 4 5 6 7
Queue using Array enqueue(8) front 1 rear 7 5 2 6 8 1 0 7 1 front 0 5 2 6 8 2 3 4 5 rear 5 6 7
Queue using Array dequeue() front 7 rear 5 2 6 7 8 0 1 front 1 5 2 6 8 2 3 4 5 rear 5 6 7
Queue using Array dequeue() front 5 rear 2 6 8 0 1 front 2 5 2 6 8 2 3 4 5 rear 5 6 7
Queue using Array enqueue(9) enqueue(12) front 5 rear 2 6 8 9 12 0 enqueue(21) ? ? 1 front 2 5 2 6 8 9 12 2 3 4 5 6 7 rear 7
Queue using Array § We have inserts and removal running in constant time but we created a new problem. § Cannot insert new elements even though there are two places available at the start of the array. § Solution: allow the queue to “wrap around”.
Queue using Array § Basic idea is to picture the array as a circular array. 0 front 5 rear 2 6 8 7 9 12 6 1 12 5 9 2 8 5 6 4 2 3 front 2 rear 7
Queue using Array enqueue(21) front 5 rear 2 6 8 7 9 12 21 6 0 21 1 12 5 9 2 8 6 5 void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; no. Elements = no. Elements+1; } 4 2 3 front 2 size 8 rear 0 no. Elements 7
Queue using Array enqueue(7) front 5 rear 2 6 8 7 9 12 21 7 6 0 21 1 7 12 5 9 2 8 5 6 int is. Full() { return no. Elements == size; } int is. Empty() { return no. Elements == 0; } 4 2 3 front 2 size 8 rear 1 no. Elements 8
Queue using Array dequeue() 0 front 6 rear 8 7 9 12 21 7 6 1 21 7 2 12 9 8 5 3 6 int dequeue() { int x = array[front]; front = (front+1)%size; no. Elements = no. Elements-1; return x; } 4 front 4 size 8 rear 1 no. Elements 6
Use of Queues § Out of the numerous uses of the queues, one of the most useful is simulation. § A simulation program attempts to model a real-world phenomenon. § Many popular video games are simulations, e. g. , Sim. City, Flight. Simulator § Each object and action in the simulation has a counterpart in real world.
Uses of Queues § If the simulation is accurate, the result of the program should mirror the results of the real-world event. § Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. § Let us look at an example. Suppose there is a bank with four tellers.
Simulation of a Bank § A customer enters the bank at a specific time (t 1) desiring to conduct a transaction. § Any one of the four tellers can attend to the customer. § The transaction (withdraw, deposit) will take a certain period of time (t 2). § 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.
- Dr sohail aslam
- Dr sohail lectures
- Dian laundry
- Aslam has to study four books
- Aslam anis
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Homologous structure
- Exploratory data analysis lecture notes
- Bayesian classification in data mining lecture notes
- Data mining lecture notes
- Data visualization lecture
- Data mining lecture notes
- Data mining lecture notes
- Btechsmartclass
- Data structures in r
- Oblivious data structures
- Linux kernel data structures
- Introduction to data structures
- Introduction to data structures
- Ajit diwan
- Esoteric data structures
- Geometric data structures
- Princeton data structures and algorithms
- Data structures and algorithms tutorial
- Data integrity in hadoop
- Features of macro processor
- Advanced data structures in java