SlideShare a Scribd company logo
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________




Data Structures

Lecture No. 09

Memory Organization
By the end of last lecture, we discussed the uses of stack to develop a process from an
executable file and then in function calls. When you run an executable, the operating
system makes a process inside memory and constructs the followings for that purpose.
- A code section that contains the binary version of the actual code of the program
    written in some language like C/C++
- A section for static data including global variables
- A stack and
- Finally, a heap




                                                                          Page 1 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________


Stack is used in function calling while heap area is utilized at the time of memory
allocation in dynamic manner.


         Process 1
         (Browser)                                Code
         Process 3                             Static Data
          (Word)
         Process 4                                Stack
          (Excel)
          Process 2
         (Dev-C++)
                                                  Heap
        Windows OS


        Fig 1. Memory Organization


Stack Layout during a Function Call

            Parameters (F)              Parameters (F)                 Parameters (F)

          Local variables (F)        Local variables (F)               Local variables (F)

          Return address (F)          Return address (F)               Return address (F)
                                                                sp
           Parameters (G)             Parameters (G)
sp                                   Local variables (G)                   After Call
          At point of call           Return address (G)
                             sp
                                   During Execution of G
     Fig 2: Stack Layout; When function F calls function G

The above diagrams depict the layout of the stack when a function F calls a function G.
Here sp stands for stack pointer. At the very left, you will find the layout of the stack just
before function F calls function G. The parameters passed to function F are firstly
inserted inside the stack. These are followed by the local variables of the function F and
finally the memory address to return back after the function F finishes. Just before
function is made to the function G, the parameters being passed to the function G, are
inserted into the stack.

                                                                                Page 2 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________

In the next diagram, there is layout of the stack on the right side after the call to the
function G. Clearly, the local variables of the function G are inserted into the stack after
its parameters and the return address. If there are no local variables for a function, the
return address is inserted (pushed) on to the stack.
The layout of the stack, when the function G finishes execution is shown on the right.
You can see that the local variables of function G are no more in the stack. They have
been removed permanently along with the parameters passed to the function G. Now, it
is clear that when a function call is made, all local variables of the called function and the
parameters passed to it, are pushed on to the stack and are destroyed, soon after the the
completion of the called function s execution.
In C/C++ language, the variables declared as static are not pushed on the stack. Rather,
these are stored in another separate section allocated for static data of a program. This
section for global or static data can be seen in the fig 1 of this lecture. It is not destroyed
till the end of the process s execution. If a variable, say x is declared as static inside
function G, x will be stored in the static data section in the process s memory. Whereas,
its value is preserved across G function calls. The visibility of x is restricted to the
function G only. But a static variable declared as a class data is available to all member
functions of the class and a static variable declared at global scope (outside of any class
or function body) is available to all functions of the program.

Now, let s move on to another data structure called queue.

Queues
A queue is a linear data structure into which items can only be inserted at one end and
removed from the other. In contrast to the stack, which is a LIFO (Last In First Out)
structure, a queue is a FIFO (First In First Out) structure.
The usage of queue in daily life is pretty common. For example, we queue up while
depositing a utility bill or purchasing a ticket. The objective of that queue is to serve
persons in their arrival order; the first coming person is served first. The person, who
comes first, stands at the start followed by the person coming after him and so on. At the
serving side, the person who has joined the queue first is served first. If the requirement is
to serve the people in some sort of priority order, there is a separate data structure that
supports priorities. The normal queue data structure, presently under discussion, only
supports FIFO behavior.
Now, let s see what are the operations supported by the queue.

Queue Operations
The queue data structure supports the following operations:

 Operation              Description
 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.
 isEmpty()              Return TRUE if queue is empty, FALSE otherwise


                                                                                 Page 3 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________

Implementing Queue
There are certain points related to the implementation of the queue. Suppose we are
implementing queue with the help of the linked -list structure. Following are the key
points associated with the linked list implementations:
- 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.


                                      front                                       rear
front               rear


                                                  1               7       5
    1    7      5     2

    Fig 3. Queue implementation using linked list
The above figure shows queue elements on the left with two pointers front and rear. This
is an abstract view of the queue, independent of its implementation method of array or
linked list. On the right side is the same queue ,using linked list and pointers of front and
rear. When dequeue() function is called once, the front element 1 is removed. The picture
of the queue showing one element removal is also depicted below. Note that front
pointer has been moved to the next element 7 in the list afer removing the front element
1.
After dequeue() is called once


     front          rear                                  front                    rear


        7      5     2                                1               7       5           2


   Fig 4. Removal of one element from queue using dequeue()

Now at this stage of the queue, we will call enqueue (9) to insert an element 9 in it. . The
following figure shows that the new element is inserted at the rear end and rear pointer
starts pointing this new node with element 9.
At this point of time, the code of these functions of dequeue() and enqueue() should not
be an issue.




                                                                                    Page 4 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________


  Queue after enqueue(9) call
                                        front                                rear
  front             rear

                                                  7           5          2           9
          7     5     2    9

     Fig 5. Insertion of one element using enqueue(9)
Note that in this queue data structure, the new elements are inserted at rear end and
removed from the front. This is in contrast to stack structure where the elements are
inserted and removed from the same end.

Let s see the code for queue operations:

 /* Remove element from the front */
 1. int dequeue()
 2. {
 3.    int x = front->get();
 4.    Node* p = front;
 5.    front = front->getNext();
 6.    delete p;
 7. return x;
 8. }
 /* Insert an element in the rear */
 9. void enqueue(int x)
 10. {
 11.    Node* newNode = new Node();
 12.    newNode->set(x);
 13.      newNode->setNext(NULL);
 14.     rear->setNext(newNode);
 15. rear = newNode;
 16. }

In dequeue() operation, at line 3, the front element is retrieved from the queue and
assigned to the int variable x.
In line 4, the front pointer is saved in Node pointer variable p.
In line 5, the front pointer is moved forward by retrieving the address of the next node by
using front->getNext() and assigning it to the front pointer.
In line 6, the node pointed to by the front pointer is deleted by using delete front
statement.
At the end of dequeue() implementation, the value of deleted node that was saved in the
int variable x, is returned back.

The enqueue(int ) is used to add an element in the queue. It inserts the element in the rear

                                                                               Page 5 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________

of the queue. At line 11, a new Node object is created using the new Node() statement and
the returned starting address of the created object is assigned to the newNode pointer
variable.
In line 12, the value of the passed in parameter x, is set in the newly created node object
using the set() method.
In line 13, the next pointer in the newly created node is set to NULL.
In line 14, the newly created node is set as the next node of the node currently pointed by
the rear pointer.
Ine line 15, the rear pointer is set to point to the newly created node.

The code of two smaller functions is as under:
 /* To retrieve the front element */
 int front()
 {
         return front->get();
 }

 /* To check if the queue is empty */
 int isEmpty()
 {
        return ( front == NULL );
 }

The front() method is used to retrieve the front element. This is the oldest element
inserted in the queue. It uses the get() method of the Node class.
The isEmpty() method is used to check whether the queue is empty or not. It checks the
address inside the front pointer, if it is NULL. It will return true indicating that the queue
is empty or vice versa.
While studying stack data structure, we implemented it by using both array and linked
list. For queue, until now we have been discussing about implementing queue using
linked list. Now, let s discuss implementing queue with the help of an array.

Queue using Array
A programmer keeps few important considerations into view account before
implementing a queue with the help of an array:
If we use an array to hold the queue elements, both insertions and removal at the front
(start) of the array are expensive. This is due to the fact that we may have to shift up to
 n elements.
For the stack, we needed only one end but for a queue, both are required. To get around
this, we will not shift upon removal of an element.




                                                                                Page 6 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________


front                      real

                                       1           7           5           2
     1       7         5       2
                                       0           1           2           3       4               5           6       7


                                                       front                       rear
                                                           0                           3
 Fig 6. Queue implemented using an array
In the above figure, queue implementation using array is shown. As the array size is 8,
therefore, the index of the array will be from 0 to 7. The number of elements inside array
are 1, 7, 5 and 2, placed at start of the array. The front and rear in this implementation are
not pointers but just indexes of arrays. front contains the starting index i.e. 0 while rear
comprises 3.
Let s see, how the enqueue() works:
                 enqueue(6)
front                          real

                                           1           7           5           2       6
 1       7         5       2       6
                                           0           1           2           3       4               5           6       7
                                                           front                       rear
Fig 7. Insertion of one element 6                              0                           4
As shown in the above diagram, an element i.e. 6 has been inserted in the queue. Now,
the rear index is containing 4 while the front has the same 0 index. Let s see the figure
of the array when another element 8 is inserted in the queue.

             enqueue(8)
  front             real

                                       1       7           5           2       6   8
     1 7 5 2 6 8
                                           0   1           2       3           4   5           6           7
                                                           front                       rear
     Fig 8. Insertion of another element 8                     0                           5

When an element is removed from the queue. It is removed from the front index.

                                                                                                       Page 7 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________



             dequeue( )
          front       real

                                                7           5       2       6            8
          7 5 2 6 8
                                            0   1           2       3       4         5              6        7
                                                            front                            rear
     Fig 9. Removal of an element from front                    1                                5


After another call of dequeue() function:

            dequeue( )
            front    real

                                                            5       2       6            8
             5 2 6 8
                                            0   1           2       3       4         5              6        7
                                                            front                            rear
                                                                2                                5

          Fig 10. Removal of another element from front

With the removal of element from the queue, we are not shifting the array elements. The
shifting of elements might be an expensive exercise to perform and the cost is increased
with the increase in number of elements in the array. Therefore, we will leave them as it
is.

                         enqueue(9)
                         enqueue(12)
  front           real

                                                    5           2       6       8            9           12
  5 2 6 8 9 12
                                       0    1       2           3       4       5         6              7
                                                    front                           rear
                                                        2                            7

   Fig 11. Insertion of elements in the queue

                                                                                                         Page 8 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________

After insertion of two elements in the queue, the array that was used to implement it, has
reached its limit as the last location of the array is in use now. We know that there is
some problem with the array after it attained the size limit. We observed the similar
problem while implementing a stack with the help of an array.
We can also see that two locations at the start of the array are vacant. Therefore, we
should can consider how to use those locations appropriately in to insert more elements
in the array.
Although, we have insert and removal operations running in constantly, yet we created a
new problem that we cannot insert new elements even though there are two places
available at the start of the array. The solution to this problem lies in allowing the queue
to wrap around.
How can we wrap around? We can use circular array to implement the queue. We know
how to make a linked list circular using pointers. Now we will see how can we make a
circular array.

                                                              0               1
                                                                                                    front
front                      rear
                                                 7                                     2                 2
                                                         12                       5
  5      2    6   8   9     12
                                                 6        9                       2
                                                                  8       6            3           rear

 Fig 12. Circular array to implement queue                                                           7
                                                              5               4
The number of locations in the above circular array are also eight, starting from index 0
to index 7. The index numbers are written outside the circle incremented in the clock-
wise direction. To insert an element 21 in the array , we insert this element in the
location, which is next to index 7.

             enqueue(21)
                                                     0                1
                                                                                      front       size
   front                   rear                          21
                                           7                                      2     2           8
                                                12                        5
        5 2 6 8 9 12 21
                                           6     9                        2
                                                         8        6               3   rear     noElements
  Fig 13. An element added in circular array                                           0             7
                                                     5                4

Now, we will have to maintain four variables. front has the same index 2 while the, size is
8. rear has moved to index 0 and noElements is 7. Now, we can see that rear index has
decreased instread of increasing. It has moved from index 7 to 0. front is containing index
2 i.e. higher than the index in rear. Let see, how do we implement the enqueue()
method.

                                                                                             Page 9 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________


 void enqueue( int x)
 {
 1. rear = (rear + 1) % size;
 2. array[rear] = x;
 3. noElements = noElements + 1;
 }

In line 1 of the code, 1 is added in rear and the mod operator (that results in remainder of
the two operands) is applied with size variable. This expression on the right of
assignment in line 1 can result from 0 to 7 as size is containing value 8. This operator
ensures that value of this expression will always be from 0 to 7 and increase or decrease
from this. This resultant is assigned to the rear variable.
In line 2, the x (the value passed to enqueue() method to insert in the queue) is inserted in
the array at the rear index position. Therefore, in the above case, the new element 21 is
inserted at index 0 in the array.
In line 3, noElements is added to accumulate another element in the queue.

Let s add another element in the queue.
        enqueue(7)
                                                   0            1
                                                                                front      size
front                    rear                          21       7
                                          7                                 2     2          8
                                              12                        5
 5 2 6 8 9 12 21 7
                                          6   9                         2
                                                       8    6               3   rear    noElements
                                                                                 1            8
                                           5                        4
Fig 14. Another element added in circular array

Now, the queue, rather the array has become full. It is important to understand, that queue
does not have such characteristic to become full. Only its implementation array has
become full. To resolve this problem, we can use linked list to implement a queue. For
the moment, while working with array, we will write the method isFull(), to determine
the fullness of the array.

 int isFull()
 {
    return noElements == size;
 }

 int isEmpty()
 {
         return noElements == 0;
 }

                                                                                        Page 10 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________


isFull() returns true if the number of elements (noElements) in the array is equal to the
size of the array. Otherwise, it returns false. It is the responsibility of the caller of the
queue structure to call isFull() function to confirm that there is some space left in the
queue to enqueue() more elements.
Similarly isEmpty() looks at the number of elements (noElements) in the queue. If there
is no element, it returns true or vice versa..

Let s see the dequeue() method.
                                                   0           1
              dequeue()
                                                                           front      size
      front               rear                      21         7
                                         7                             2     4          8
                                              12
         6 8 9 12 21 7
                                         6     9
                                                       8   6           3   rear    noElements
                                                                            1            8
                                           5                       4
 Fig 15. Element removed from the circular array


 int dequeue()
 {
    int x = array[front];
           front = (front + 1) % size;
           noElements = noElements - 1;
           return x;
 }
In the first line, we take out an element from the array at front index position and store it
in a variable x. In the second line, front is incremented by 1 but as the array is circular,
the index is looped from 0 to 7. That is why the mod (%) is being used. In the third line,
number of elements (noElements) is reduced by 1 and finally the saved array element is
returned.

Use of Queues
We saw the uses of stack structure in infix, prefix and postfix expressions. Let s see the
usage of queue now.
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., SimCity, Flight Simulator etc. Each object and action in the
simulation has a counterpart in the real world. Computer simulation is very powerful tool
and it is used in different high tech industries, especially in engineering projects. For
example, it is used in aero plane manufacturing. Actually Computer Simulation is full-
fledged subject of Computer Science and contains very complex Mathematics,
sometimes. For example, simulation of computer networks, traffic networks etc.

                                                                                  Page 11 of 12
ecomputernotes.com Data Structures                          Lecture No. 09
___________________________________________________________________

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.
A customer enters the bank at a specific time (t1) desiring to conduct a transaction.
Any one of the four tellers can attend to the customer. The transaction (withdraws,
deposit) will take a certain period of time (t2). If a teller is free, the teller can process the
customer s transaction immediately and the customer leaves the bank at t1+t2. It is
possible that none of the four tellers is free in which case there is a line of customers at
each teller. An arriving customer proceeds to the back of the shortest line and waits for
his turn. The customer leaves the bank at t2 time units after reaching the front of the line.
The time spent at the bank is t2 plus time waiting in line.
So what we want to simulate is the working environment of the bank that there are
specific number of queues of customers in the bank in front of the tellers. The tellers are
serving customers one by one. A customer has to wait for a certain period of time before
he gets served and by using simulation tool, we want to know the average waiting time of
a bank customer. We will talk about this simulation in the next lecture and will do coding
also in order to understand it well.




                                                                                  Page 12 of 12

More Related Content

What's hot (20)

PPT
Queue Data Structure
Lovely Professional University
 
PPTX
Basic of octave matlab programming language
Aulia Khalqillah
 
PPT
Stack, queue and hashing
Dumindu Pahalawatta
 
PPTX
Computer Science Assignment Help
Programming Homework Help
 
PDF
Polymorphic Table Functions in 18c
Andrej Pashchenko
 
PPTX
Programming Assignment Help
Programming Homework Help
 
PDF
GNU octave
gauravmalav
 
PDF
Tutorial2
ashumairitar
 
PDF
computer notes - Priority queue
ecomputernotes
 
PDF
Data structure and algorithm.(dsa)
mailmerk
 
PPTX
A quick introduction to R
Angshuman Saha
 
PPSX
Queue by rajanikanth
btechsmartclass
 
PDF
Data structure lab manual
nikshaikh786
 
PPTX
Queue
Krishanu Ghosh
 
PPTX
Computer Science Assignment Help
Programming Homework Help
 
PPTX
My lectures circular queue
Senthil Kumar
 
PDF
stacks and queues
DurgaDeviCbit
 
PPT
Data Structures by Maneesh Boddu
maneesh boddu
 
PDF
Queue as data_structure
eShikshak
 
Queue Data Structure
Lovely Professional University
 
Basic of octave matlab programming language
Aulia Khalqillah
 
Stack, queue and hashing
Dumindu Pahalawatta
 
Computer Science Assignment Help
Programming Homework Help
 
Polymorphic Table Functions in 18c
Andrej Pashchenko
 
Programming Assignment Help
Programming Homework Help
 
GNU octave
gauravmalav
 
Tutorial2
ashumairitar
 
computer notes - Priority queue
ecomputernotes
 
Data structure and algorithm.(dsa)
mailmerk
 
A quick introduction to R
Angshuman Saha
 
Queue by rajanikanth
btechsmartclass
 
Data structure lab manual
nikshaikh786
 
Computer Science Assignment Help
Programming Homework Help
 
My lectures circular queue
Senthil Kumar
 
stacks and queues
DurgaDeviCbit
 
Data Structures by Maneesh Boddu
maneesh boddu
 
Queue as data_structure
eShikshak
 

Viewers also liked (20)

PPTX
Memory organisation
ankush_kumar
 
PPTX
Memory Organization
Dilum Bandara
 
PDF
Memory organization
Dr. Abhineet Anand
 
DOCX
R statistics
Konstantinos Kalandrakis
 
PDF
310471266 chapter-7-notes-computer-organization
srinoni
 
DOC
Ise iv-computer organization [10 cs46]-notes new
dilshad begum
 
DOCX
Computer Organization and 8085 microprocessor notes
Lakshmi Sarvani Videla
 
PDF
Computer Organization (Unit-1)
Harsh Pandya
 
PDF
Decoders and encoders
sanket1996
 
DOC
Computer
17somya
 
PPTX
Fundamental units of computer
Sandeep Menon
 
PDF
Computer organization memory
Deepak John
 
PPTX
Functional units
Jeeva Nanthini
 
PDF
Computer Organization Lecture Notes
FellowBuddy.com
 
PDF
combinational_circuits
Bindu Madhavi
 
PPT
basic computer programming and micro programmed control
Rai University
 
PPT
2.computer org.
Mahesh Kumar Attri
 
PDF
Data Mining & Data Warehousing Lecture Notes
FellowBuddy.com
 
PPT
Unit 1 basic structure of computers
chidabdu
 
Memory organisation
ankush_kumar
 
Memory Organization
Dilum Bandara
 
Memory organization
Dr. Abhineet Anand
 
310471266 chapter-7-notes-computer-organization
srinoni
 
Ise iv-computer organization [10 cs46]-notes new
dilshad begum
 
Computer Organization and 8085 microprocessor notes
Lakshmi Sarvani Videla
 
Computer Organization (Unit-1)
Harsh Pandya
 
Decoders and encoders
sanket1996
 
Computer
17somya
 
Fundamental units of computer
Sandeep Menon
 
Computer organization memory
Deepak John
 
Functional units
Jeeva Nanthini
 
Computer Organization Lecture Notes
FellowBuddy.com
 
combinational_circuits
Bindu Madhavi
 
basic computer programming and micro programmed control
Rai University
 
2.computer org.
Mahesh Kumar Attri
 
Data Mining & Data Warehousing Lecture Notes
FellowBuddy.com
 
Unit 1 basic structure of computers
chidabdu
 
Ad

Similar to computer notes - Memory organization (20)

PPT
computer notes - Data Structures - 9
ecomputernotes
 
PPT
Lec-07 Queues.ppt queues introduction to queue
AmsaAzeem
 
PDF
computer notes - Stack
ecomputernotes
 
PPT
Computer notes data structures - 9
ecomputernotes
 
PPTX
Data structures
naveeth babu
 
PPT
Data structure.ppt
SajalFayyaz
 
PPT
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
MaximusAranha
 
PPT
Unit i(dsc++)
Durga Devi
 
PPTX
Data Structures_Linear Data Structures Queue.pptx
RushaliDeshmukh2
 
PDF
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
PDF
IRJET- Comparison of Stack and Queue Data Structures
IRJET Journal
 
PDF
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
PDF
computer notes - Reference variables
ecomputernotes
 
PPTX
The presention is about the queue data structure
gaurav77712
 
PDF
DSA Lab Manual C Scheme.pdf
Bharati Vidyapeeth COE, Navi Mumbai
 
PDF
Lesson 4 stacks and queues
MLG College of Learning, Inc
 
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
workspaceabhishekmah
 
PDF
C++ questions And Answer
lavparmar007
 
PDF
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
PDF
computer notes - Conversion from infix to postfix
ecomputernotes
 
computer notes - Data Structures - 9
ecomputernotes
 
Lec-07 Queues.ppt queues introduction to queue
AmsaAzeem
 
computer notes - Stack
ecomputernotes
 
Computer notes data structures - 9
ecomputernotes
 
Data structures
naveeth babu
 
Data structure.ppt
SajalFayyaz
 
intr_qyyuujjjjjjjkkkkkkkkkkkkkjkueue.ppt
MaximusAranha
 
Unit i(dsc++)
Durga Devi
 
Data Structures_Linear Data Structures Queue.pptx
RushaliDeshmukh2
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET Journal
 
Fibonacci Function Gallery - Part 2 - One in a series
Philip Schwarz
 
computer notes - Reference variables
ecomputernotes
 
The presention is about the queue data structure
gaurav77712
 
DSA Lab Manual C Scheme.pdf
Bharati Vidyapeeth COE, Navi Mumbai
 
Lesson 4 stacks and queues
MLG College of Learning, Inc
 
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
workspaceabhishekmah
 
C++ questions And Answer
lavparmar007
 
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
computer notes - Conversion from infix to postfix
ecomputernotes
 
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
ecomputernotes
 
PPT
computer notes - Data Structures - 39
ecomputernotes
 
PPT
computer notes - Data Structures - 11
ecomputernotes
 
PPT
computer notes - Data Structures - 20
ecomputernotes
 
PPT
computer notes - Data Structures - 15
ecomputernotes
 
DOC
Computer notes - Including Constraints
ecomputernotes
 
DOC
Computer notes - Date time Functions
ecomputernotes
 
DOC
Computer notes - Subqueries
ecomputernotes
 
DOC
Computer notes - Other Database Objects
ecomputernotes
 
PPT
computer notes - Data Structures - 28
ecomputernotes
 
PPT
computer notes - Data Structures - 19
ecomputernotes
 
PPT
computer notes - Data Structures - 31
ecomputernotes
 
PPT
computer notes - Data Structures - 4
ecomputernotes
 
PPT
computer notes - Data Structures - 13
ecomputernotes
 
DOC
Computer notes - Advanced Subqueries
ecomputernotes
 
DOC
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
PPT
computer notes - Data Structures - 16
ecomputernotes
 
PPT
computer notes - Data Structures - 22
ecomputernotes
 
PPT
computer notes - Data Structures - 35
ecomputernotes
 
PPT
computer notes - Data Structures - 36
ecomputernotes
 
computer notes - Data Structures - 30
ecomputernotes
 
computer notes - Data Structures - 39
ecomputernotes
 
computer notes - Data Structures - 11
ecomputernotes
 
computer notes - Data Structures - 20
ecomputernotes
 
computer notes - Data Structures - 15
ecomputernotes
 
Computer notes - Including Constraints
ecomputernotes
 
Computer notes - Date time Functions
ecomputernotes
 
Computer notes - Subqueries
ecomputernotes
 
Computer notes - Other Database Objects
ecomputernotes
 
computer notes - Data Structures - 28
ecomputernotes
 
computer notes - Data Structures - 19
ecomputernotes
 
computer notes - Data Structures - 31
ecomputernotes
 
computer notes - Data Structures - 4
ecomputernotes
 
computer notes - Data Structures - 13
ecomputernotes
 
Computer notes - Advanced Subqueries
ecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
ecomputernotes
 
computer notes - Data Structures - 16
ecomputernotes
 
computer notes - Data Structures - 22
ecomputernotes
 
computer notes - Data Structures - 35
ecomputernotes
 
computer notes - Data Structures - 36
ecomputernotes
 

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 

computer notes - Memory organization

  • 1. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ Data Structures Lecture No. 09 Memory Organization By the end of last lecture, we discussed the uses of stack to develop a process from an executable file and then in function calls. When you run an executable, the operating system makes a process inside memory and constructs the followings for that purpose. - A code section that contains the binary version of the actual code of the program written in some language like C/C++ - A section for static data including global variables - A stack and - Finally, a heap Page 1 of 12
  • 2. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ Stack is used in function calling while heap area is utilized at the time of memory allocation in dynamic manner. Process 1 (Browser) Code Process 3 Static Data (Word) Process 4 Stack (Excel) Process 2 (Dev-C++) Heap Windows OS Fig 1. Memory Organization Stack Layout during a Function Call Parameters (F) Parameters (F) Parameters (F) Local variables (F) Local variables (F) Local variables (F) Return address (F) Return address (F) Return address (F) sp Parameters (G) Parameters (G) sp Local variables (G) After Call At point of call Return address (G) sp During Execution of G Fig 2: Stack Layout; When function F calls function G The above diagrams depict the layout of the stack when a function F calls a function G. Here sp stands for stack pointer. At the very left, you will find the layout of the stack just before function F calls function G. The parameters passed to function F are firstly inserted inside the stack. These are followed by the local variables of the function F and finally the memory address to return back after the function F finishes. Just before function is made to the function G, the parameters being passed to the function G, are inserted into the stack. Page 2 of 12
  • 3. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ In the next diagram, there is layout of the stack on the right side after the call to the function G. Clearly, the local variables of the function G are inserted into the stack after its parameters and the return address. If there are no local variables for a function, the return address is inserted (pushed) on to the stack. The layout of the stack, when the function G finishes execution is shown on the right. You can see that the local variables of function G are no more in the stack. They have been removed permanently along with the parameters passed to the function G. Now, it is clear that when a function call is made, all local variables of the called function and the parameters passed to it, are pushed on to the stack and are destroyed, soon after the the completion of the called function s execution. In C/C++ language, the variables declared as static are not pushed on the stack. Rather, these are stored in another separate section allocated for static data of a program. This section for global or static data can be seen in the fig 1 of this lecture. It is not destroyed till the end of the process s execution. If a variable, say x is declared as static inside function G, x will be stored in the static data section in the process s memory. Whereas, its value is preserved across G function calls. The visibility of x is restricted to the function G only. But a static variable declared as a class data is available to all member functions of the class and a static variable declared at global scope (outside of any class or function body) is available to all functions of the program. Now, let s move on to another data structure called queue. Queues A queue is a linear data structure into which items can only be inserted at one end and removed from the other. In contrast to the stack, which is a LIFO (Last In First Out) structure, a queue is a FIFO (First In First Out) structure. The usage of queue in daily life is pretty common. For example, we queue up while depositing a utility bill or purchasing a ticket. The objective of that queue is to serve persons in their arrival order; the first coming person is served first. The person, who comes first, stands at the start followed by the person coming after him and so on. At the serving side, the person who has joined the queue first is served first. If the requirement is to serve the people in some sort of priority order, there is a separate data structure that supports priorities. The normal queue data structure, presently under discussion, only supports FIFO behavior. Now, let s see what are the operations supported by the queue. Queue Operations The queue data structure supports the following operations: Operation Description 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. isEmpty() Return TRUE if queue is empty, FALSE otherwise Page 3 of 12
  • 4. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ Implementing Queue There are certain points related to the implementation of the queue. Suppose we are implementing queue with the help of the linked -list structure. Following are the key points associated with the linked list implementations: - 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. front rear front rear 1 7 5 1 7 5 2 Fig 3. Queue implementation using linked list The above figure shows queue elements on the left with two pointers front and rear. This is an abstract view of the queue, independent of its implementation method of array or linked list. On the right side is the same queue ,using linked list and pointers of front and rear. When dequeue() function is called once, the front element 1 is removed. The picture of the queue showing one element removal is also depicted below. Note that front pointer has been moved to the next element 7 in the list afer removing the front element 1. After dequeue() is called once front rear front rear 7 5 2 1 7 5 2 Fig 4. Removal of one element from queue using dequeue() Now at this stage of the queue, we will call enqueue (9) to insert an element 9 in it. . The following figure shows that the new element is inserted at the rear end and rear pointer starts pointing this new node with element 9. At this point of time, the code of these functions of dequeue() and enqueue() should not be an issue. Page 4 of 12
  • 5. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ Queue after enqueue(9) call front rear front rear 7 5 2 9 7 5 2 9 Fig 5. Insertion of one element using enqueue(9) Note that in this queue data structure, the new elements are inserted at rear end and removed from the front. This is in contrast to stack structure where the elements are inserted and removed from the same end. Let s see the code for queue operations: /* Remove element from the front */ 1. int dequeue() 2. { 3. int x = front->get(); 4. Node* p = front; 5. front = front->getNext(); 6. delete p; 7. return x; 8. } /* Insert an element in the rear */ 9. void enqueue(int x) 10. { 11. Node* newNode = new Node(); 12. newNode->set(x); 13. newNode->setNext(NULL); 14. rear->setNext(newNode); 15. rear = newNode; 16. } In dequeue() operation, at line 3, the front element is retrieved from the queue and assigned to the int variable x. In line 4, the front pointer is saved in Node pointer variable p. In line 5, the front pointer is moved forward by retrieving the address of the next node by using front->getNext() and assigning it to the front pointer. In line 6, the node pointed to by the front pointer is deleted by using delete front statement. At the end of dequeue() implementation, the value of deleted node that was saved in the int variable x, is returned back. The enqueue(int ) is used to add an element in the queue. It inserts the element in the rear Page 5 of 12
  • 6. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ of the queue. At line 11, a new Node object is created using the new Node() statement and the returned starting address of the created object is assigned to the newNode pointer variable. In line 12, the value of the passed in parameter x, is set in the newly created node object using the set() method. In line 13, the next pointer in the newly created node is set to NULL. In line 14, the newly created node is set as the next node of the node currently pointed by the rear pointer. Ine line 15, the rear pointer is set to point to the newly created node. The code of two smaller functions is as under: /* To retrieve the front element */ int front() { return front->get(); } /* To check if the queue is empty */ int isEmpty() { return ( front == NULL ); } The front() method is used to retrieve the front element. This is the oldest element inserted in the queue. It uses the get() method of the Node class. The isEmpty() method is used to check whether the queue is empty or not. It checks the address inside the front pointer, if it is NULL. It will return true indicating that the queue is empty or vice versa. While studying stack data structure, we implemented it by using both array and linked list. For queue, until now we have been discussing about implementing queue using linked list. Now, let s discuss implementing queue with the help of an array. Queue using Array A programmer keeps few important considerations into view account before implementing a queue with the help of an array: If we use an array to hold the queue elements, both insertions and removal at the front (start) of the array are expensive. This is due to the fact that we may have to shift up to n elements. For the stack, we needed only one end but for a queue, both are required. To get around this, we will not shift upon removal of an element. Page 6 of 12
  • 7. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ front real 1 7 5 2 1 7 5 2 0 1 2 3 4 5 6 7 front rear 0 3 Fig 6. Queue implemented using an array In the above figure, queue implementation using array is shown. As the array size is 8, therefore, the index of the array will be from 0 to 7. The number of elements inside array are 1, 7, 5 and 2, placed at start of the array. The front and rear in this implementation are not pointers but just indexes of arrays. front contains the starting index i.e. 0 while rear comprises 3. Let s see, how the enqueue() works: enqueue(6) front real 1 7 5 2 6 1 7 5 2 6 0 1 2 3 4 5 6 7 front rear Fig 7. Insertion of one element 6 0 4 As shown in the above diagram, an element i.e. 6 has been inserted in the queue. Now, the rear index is containing 4 while the front has the same 0 index. Let s see the figure of the array when another element 8 is inserted in the queue. enqueue(8) front real 1 7 5 2 6 8 1 7 5 2 6 8 0 1 2 3 4 5 6 7 front rear Fig 8. Insertion of another element 8 0 5 When an element is removed from the queue. It is removed from the front index. Page 7 of 12
  • 8. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ dequeue( ) front real 7 5 2 6 8 7 5 2 6 8 0 1 2 3 4 5 6 7 front rear Fig 9. Removal of an element from front 1 5 After another call of dequeue() function: dequeue( ) front real 5 2 6 8 5 2 6 8 0 1 2 3 4 5 6 7 front rear 2 5 Fig 10. Removal of another element from front With the removal of element from the queue, we are not shifting the array elements. The shifting of elements might be an expensive exercise to perform and the cost is increased with the increase in number of elements in the array. Therefore, we will leave them as it is. enqueue(9) enqueue(12) front real 5 2 6 8 9 12 5 2 6 8 9 12 0 1 2 3 4 5 6 7 front rear 2 7 Fig 11. Insertion of elements in the queue Page 8 of 12
  • 9. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ After insertion of two elements in the queue, the array that was used to implement it, has reached its limit as the last location of the array is in use now. We know that there is some problem with the array after it attained the size limit. We observed the similar problem while implementing a stack with the help of an array. We can also see that two locations at the start of the array are vacant. Therefore, we should can consider how to use those locations appropriately in to insert more elements in the array. Although, we have insert and removal operations running in constantly, yet we created a new problem that we cannot insert new elements even though there are two places available at the start of the array. The solution to this problem lies in allowing the queue to wrap around. How can we wrap around? We can use circular array to implement the queue. We know how to make a linked list circular using pointers. Now we will see how can we make a circular array. 0 1 front front rear 7 2 2 12 5 5 2 6 8 9 12 6 9 2 8 6 3 rear Fig 12. Circular array to implement queue 7 5 4 The number of locations in the above circular array are also eight, starting from index 0 to index 7. The index numbers are written outside the circle incremented in the clock- wise direction. To insert an element 21 in the array , we insert this element in the location, which is next to index 7. enqueue(21) 0 1 front size front rear 21 7 2 2 8 12 5 5 2 6 8 9 12 21 6 9 2 8 6 3 rear noElements Fig 13. An element added in circular array 0 7 5 4 Now, we will have to maintain four variables. front has the same index 2 while the, size is 8. rear has moved to index 0 and noElements is 7. Now, we can see that rear index has decreased instread of increasing. It has moved from index 7 to 0. front is containing index 2 i.e. higher than the index in rear. Let see, how do we implement the enqueue() method. Page 9 of 12
  • 10. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ void enqueue( int x) { 1. rear = (rear + 1) % size; 2. array[rear] = x; 3. noElements = noElements + 1; } In line 1 of the code, 1 is added in rear and the mod operator (that results in remainder of the two operands) is applied with size variable. This expression on the right of assignment in line 1 can result from 0 to 7 as size is containing value 8. This operator ensures that value of this expression will always be from 0 to 7 and increase or decrease from this. This resultant is assigned to the rear variable. In line 2, the x (the value passed to enqueue() method to insert in the queue) is inserted in the array at the rear index position. Therefore, in the above case, the new element 21 is inserted at index 0 in the array. In line 3, noElements is added to accumulate another element in the queue. Let s add another element in the queue. enqueue(7) 0 1 front size front rear 21 7 7 2 2 8 12 5 5 2 6 8 9 12 21 7 6 9 2 8 6 3 rear noElements 1 8 5 4 Fig 14. Another element added in circular array Now, the queue, rather the array has become full. It is important to understand, that queue does not have such characteristic to become full. Only its implementation array has become full. To resolve this problem, we can use linked list to implement a queue. For the moment, while working with array, we will write the method isFull(), to determine the fullness of the array. int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; } Page 10 of 12
  • 11. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ isFull() returns true if the number of elements (noElements) in the array is equal to the size of the array. Otherwise, it returns false. It is the responsibility of the caller of the queue structure to call isFull() function to confirm that there is some space left in the queue to enqueue() more elements. Similarly isEmpty() looks at the number of elements (noElements) in the queue. If there is no element, it returns true or vice versa.. Let s see the dequeue() method. 0 1 dequeue() front size front rear 21 7 7 2 4 8 12 6 8 9 12 21 7 6 9 8 6 3 rear noElements 1 8 5 4 Fig 15. Element removed from the circular array int dequeue() { int x = array[front]; front = (front + 1) % size; noElements = noElements - 1; return x; } In the first line, we take out an element from the array at front index position and store it in a variable x. In the second line, front is incremented by 1 but as the array is circular, the index is looped from 0 to 7. That is why the mod (%) is being used. In the third line, number of elements (noElements) is reduced by 1 and finally the saved array element is returned. Use of Queues We saw the uses of stack structure in infix, prefix and postfix expressions. Let s see the usage of queue now. 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., SimCity, Flight Simulator etc. Each object and action in the simulation has a counterpart in the real world. Computer simulation is very powerful tool and it is used in different high tech industries, especially in engineering projects. For example, it is used in aero plane manufacturing. Actually Computer Simulation is full- fledged subject of Computer Science and contains very complex Mathematics, sometimes. For example, simulation of computer networks, traffic networks etc. Page 11 of 12
  • 12. ecomputernotes.com Data Structures Lecture No. 09 ___________________________________________________________________ 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. A customer enters the bank at a specific time (t1) desiring to conduct a transaction. Any one of the four tellers can attend to the customer. The transaction (withdraws, deposit) will take a certain period of time (t2). If a teller is free, the teller can process the customer s transaction immediately and the customer leaves the bank at t1+t2. It is possible that none of the four tellers is free in which case there is a line of customers at each teller. An arriving customer proceeds to the back of the shortest line and waits for his turn. The customer leaves the bank at t2 time units after reaching the front of the line. The time spent at the bank is t2 plus time waiting in line. So what we want to simulate is the working environment of the bank that there are specific number of queues of customers in the bank in front of the tellers. The tellers are serving customers one by one. A customer has to wait for a certain period of time before he gets served and by using simulation tool, we want to know the average waiting time of a bank customer. We will talk about this simulation in the next lecture and will do coding also in order to understand it well. Page 12 of 12