CSE23302_Module 2 _Chapter 1.Pptx
CSE23302_Module 2 _Chapter 1.Pptx
Data Structures
Module 2-Chapter 1
QUEUES
Dr. R Kanagavalli
Professor
Department of CSE
Global Academy of Technology
Bangalore
Objectives
To understand the concept of Queue
To understand the primitive operations on queue
To implement queues in C
To understand the different types of queue
To understand the applications of queues.
2
Queue - Definition
• Ordered collection of items .
• Items are inserted from one end of the queue called rear end of the
queue.
• Items are removed from one end of the queue called front end of the
queue.
• Items first inserted into the queue are removed first
• Called as FIFO structure.
3
Type of queues
1. Linear Queue
2. Circular queue.
3. Priority queue
4. Deque
4
Array Representation of Queue
20 30 40
front rear
5
Primitive operations
• Three primitive operations
☞ Insert
☞ Remove
☞ Empty
6
Primitive operations- Insert
• Inserts an element in the position pointed by the rear end of the
queue
• General syntax : insert (item)
• To insert an element into the queue, do the following steps
Increment the rear pointer
Add the element
7
Primitive operations-Remove
• Removes the element pointed by the front pointer from the queue.
• General syntax: remove()
• To remove an element from the queue , do the following steps
Remove the element
Increment the front pointer
8
Primitive operations- empty
• Checks the emptiness of queue
• Returns true if the queue contains no elements otherwise false
• Has to be applied before attempting a remove operation in a queue.
• General syntax : empty(queue)
9
• Initially rear is set to -1 and front is set to 0.
• The queue is empty whenever rear < front
• The number of elements in the queue at any time is equal to the
value of rear –front +1
10
Declaration of Queue as array
int Q[MAX_SIZE];
int front=0, rear=-1;
11
Insert Operation
void addQ()
printf("\n Enter data: ");
{
scanf("%d", &data);
int data;
Q[++rear] = data;
if(rear == MAX-1)
printf("\n Data Inserted in the Queue ");
{
printf("\n Linear Queue is full"); }
exit(0);
}
12
Remove Operation
void delQ() printf("\n Deleted element from Queue is
{ %d", Q[front]);
if(front>rear)
front++;
{
}
printf("\n\n Queue is Empty..");
exit(0);
}
13
Display Operation
void displayQ()
printf("\n Elements in Queue are: ");
{
for(i = front; i <=rear; i++)
int i;
if(front >rear) printf("%d\t", Q[i]);
{ }
15
q.items q.items
4
4
3
3
2 7 q.rear = 2
2
1 6
1
0 q.front = 0 0 5 q.front = 0
(a) q.rear = -1 (b)
q.items q.items
4 9 q.rear = 4
4
3 8
3
2 7 q.front = 2 7 q.front = 2
1 q.rear = 2
1
0
0
(c) (d)
16
• In linear queue, once the rear value reaches MAX – 1, further
insertions cannot be done though there may be some vacant slots in
the queue .
• This is a major drawback of a linear queue.
• To resolve this problem, we have two solutions.
• One solution is
modify the remove operation so that when an item is deleted, the
entire queue is shifted to the beginning of the array.
17
• One solution is
modify the remove operation so that when an item is deleted, the
entire queue is shifted to the beginning of the array.
x = q.items[0];
for( i=0; i<q.rear; i++)
q.items[i] = q.items[i+1];
q.rear--;
18
• Another solution:
- view the array that holds the queue as circle rather than a straight
line.
This implies that even if the last element is occupied , a new value can
be inserted behind it in the first element of the array as long as that
first element is empty.
This arrangement is called as Circular Queues
19
Circular Queues
• A Circular Queue is linear data structure in which elements are
arranged in a circular fashion where each element has next and
previous elements.
• Circular queues have a fixed size.
• Circular queue follows FIFO principle.
• Queue items are added at the rear end and the items are deleted at
front end of the circular queue.
20
21
Insertion in Circular Queue
22
• int Q[size];
• int r= -1, f=0;
23
• void insert() {
void insert( )
r=(r+1)%SIZE;
{
printf("\nEnter the item:");
if(count==SIZE)
scanf(" %c",&q[r]);
printf("\n Q is Full\n");
count++;
else
}
•}
24
Deletion in Circular Queue
• To delete an element from the circular queue we increment front by 1
in clockwise direction.
25
void delet() {
if(count==0) %c",q[f]);
else f=(f+1)%SIZE;
26
Display operation
{
void display() i=f;
{ for(j=1;j<=count;j++)
if(count==0) {
printf("\n Q is Empty\n"); printf(" %c",q[i]);
else i=(i+1)%SIZE;
}
}
}
27
Q Q
4 9 rear = 4 4 9
3 8 3 8
2 7 front = 2 2 7 front = 2
1 1
0 0 6 rear = 0
(a) (b)
4 9 front = 4
3
2
1
0 6 rear = 0
(c)
28
Q Q
4 9 front = 4 4
3 3
2 2
1 5 rear = 1 1 5 rear = 1
0 6 0 6 front = 0
(d) (e)
29
Priority Queue
• Data structure in which each element has been
assigned a value called the priority
• Elements can be inserted or deleted not only at the
ends but at any position of the queue.
A B … … X S
P1 P2 … … Pi Pn
front rear
30
• The order in which the elements are deleted and processed comes
from the following rules:
❖ An element with higher priority is processed before an element with a
lower priority.
❖ Two elements with the same priority are processed according to the
order in which they were added to the queue.
31
• In the priority queue insertion of elements may be in any form (or
from rear end of the queue) but deletion of elements are based on
priority of element assigned to it.
• In the computer memory, a priority queue can be maintained by using
a separate queue for each level of priority.
32
• Priority queue is a data structure in which the intrinsic ordering of the
elements does determine the results of its basic operations.
• Two types of priority queues
An ascending priority queue – items are inserted arbitrarily and from
which only the smallest item can be removed.
A descending priority queue - items are inserted arbitrarily and from
which only the largest item can be removed
33
Structure Definition of Priority Queue
# define MAXPQ 100
struct pqueue
{
int items[MAXPQ];
int front,rear;
};
struct pqueue PQ;
34
•pq.front is the position of the smallest element .
•pq.rear is 1 greater than the position of the largest.
35
• Deletion Operation
Involves increasing pq.front (for ascending queue)
Involves decreasing pq.rear(for descending queue)
• Insertion Operation
Locate the proper position of the new element
Shift the preceeding or succeeding elements.
Since the array is ordered , searching is less expensive.
36
CSE23302
Data Structures
Module 2-Chapter 2
Dynamic Memory Allocation
Dr. R Kanagavalli
Professor
Department of CSE
Global Academy of Technology
Bangalore
Memory Allocation Functions
• Two ways of reserving memory locations for an object
Static memory allocation
Dynamic memory allocation
• Memory is divided into
program memory
data memory
38
Memory Allocation Functions-Program Memory
• Consists of the memory used for main and called functions.
• Main is in memory at all times.
• Each called function must be in memory only while it or any of its
called functions are active
39
Memory Allocation Functions-Stack Memory
• The local variables for the function are available only when it is active.
• If more than one version of the function is active at a time, then
multiple copies of the local variables are allocated.
• This memory facility is known as the stack memory.
40
Memory Allocation Functions-Heap memory
• Unused memory allocated to the program and available to be
assigned during its execution.
• Part of memory pool which is allocated on request by the memory
allocation functions.
41
Memory – conceptual view
main function
Program memory
Data memory
memory
42
Static Memory Allocation
• Requires the declaration and definition of memory be fully specified
in the source program
• The number of bytes reserved cannot be changed during run time.
43
Dynamic Memory Allocation
• Uses predefined functions to allocate and release memory during run
time.
• Effectively postpones the data definition to run time.
• Can be used with both standard and derived data types.
44
Memory allocation functions
• Four memory management functions are used with dynamic memory.
• Found in stdlib.h
malloc
calloc
realloc
free
45
Memory allocation functions-malloc
• Allocates a block of memory that contains the number of bytes
specified in its parameter.
• Returns a void pointer to the first byte of the allocated memory.
• The allocated memory is not initialized.
• Memory overflow is indicated by returning a NULL pointer.
• malloc always allocates a block of contiguous bytes
46
• Syntax
type *p;
p = (type *) malloc (byte_size);
47
•p = (int *) malloc(100 * sizeof(int));
•A memory space equivalent to 100 times the size of an int
bytes is reserved
•The address of the first byte of the allocated memory is
assigned to the pointer p of type int
p
49
Memory allocation functions- calloc
• Primarily used to allocate memory for arrays.
• Sets memory to null characters.
• Syntax :
•void *calloc (size_t element_count,size_t element_size);
50
Memory allocation functions- realloc
• When given a pointer to a previously allocated block of memory ,
realloc changes the size of the block by deleting or extending the
memory at the end of block.
• Can be highly inefficient
• syntax:
•void *realloc(void *ptr,size_t newsize);
51
Memory allocation functions-free
• When the memory allocated is no longer needed, they should be
freed using the predefined function free.
• It is error
To free memory with a null pointer
To free memory with a pointer to other than the first element of the
allocated block
To free memory with a pointer of different type.
52
Using malloc
• Once the memory is allocated, it can be used with pointers, or with
array notation
int *p, n, i;
scanf(“%d”, &n);
p = (int *) malloc (n * sizeof(int));
for (i=0; i<n; ++i)
scanf(“%d”, &p[i]);
53
• The n integers allocated can be accessed as *p, *(p+1), *(p+2),…,
*(p+n-1) or just as p[0], p[1], p[2], …,p[n-1]
54
int main() printf("Input heights for %d
{ students \n",N);
int i,N; for (i=0; i<N; i++)
float *height; scanf ("%f", &height[i]);
float sum=0,avg;
for(i=0;i<N;i++)
printf("Input no. of sum += height[i];
students\n");
scanf("%d", &N); avg = sum / (float) N;