0% found this document useful (0 votes)
8 views82 pages

Module 2 DS

The document provides an overview of queues, including their definition, properties, and operations such as insertion and deletion. It discusses the implementation of queues using arrays and linked lists, as well as the concept of circular queues to optimize space usage. Additionally, it covers multiple stacks and queues, priority queues, and linked lists, highlighting their advantages over traditional arrays.

Uploaded by

mokshithagowda24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views82 pages

Module 2 DS

The document provides an overview of queues, including their definition, properties, and operations such as insertion and deletion. It discusses the implementation of queues using arrays and linked lists, as well as the concept of circular queues to optimize space usage. Additionally, it covers multiple stacks and queues, priority queues, and linked lists, highlighting their advantages over traditional arrays.

Uploaded by

mokshithagowda24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

MODULE-2

QUEUES
• Queue is a linear list of elements in which insertion and deletion take place at different
ends. The end at which new element is inserted is called as rear and the end from which
element is deleted is called as front
• Queue has the property - FIFO
• To implement queue, front and rear variables are used

A B C D

front rear
Applications of queue
• A queue can be represented in memory either as an array or as a singly linked list.
MEMORY REPRESENTATION OFQUEUES
OPERATIONS ON QUEUE
The two basic operations that can be performed on a queue are:

Insert : to insert an element at rear of the queue


Delete: to delete an element from front of the queue .

Before inserting a new element in the queue, it is necessary to test the condition of overflow.

Overflow occurs when the queue is full and there is no space for a new element.

 Similarly, before removing the element from the queue, it is necessary to check the condition of
underflow.

 Underflow occurs when the queue is empty.


Creation of queue- Array implementation
of queue
#define MAXQ 25
typedef struct {
int front, rear;
char item[MAX]; OR
}QUEUE;
QUEUE q;
q.rear = -1;
q.front = -1;
Insert into queue
Delete from queue
Queue Display
• C:\Users\bhara\Desktop\BCS304\Queue program.docx
Disadvantage of ordinary
queue
• Once MAXQ items are added, even if we delete the items from Front, we
can not add items in vacant places.

• This is because rear has reached MAXQ value and there is no way to
come back to deleted element position.

• This problem may be solved by using array compaction on every item


deletion or by viewing queue as circular not as straight line.
Circular queue [cq]
• In case of queue represented as an array, once the value of the rear
reaches the maximum size of the queue, no more elements can be
inserted.
• However, there may be the possibility that space on the left of the front
index is vacant. Hence, in spite of space on the left of front being
empty, the queue is considered to be full.
• This wastage of space in the array implementation of a queue can be
avoided by shifting the elements to the beginning of array if the space
is available.
• In order to do this, the values of Rear and Front indices have to be
changed accordingly.
Circular queue
• It is “The queue which wrap around the end of the array.” The array
positions are arranged in a circle as shown in figure.
• In this convention the variable front is changed. front variable points
one position counterclockwise from the location of the front element
in the queue. The convention for rear is unchanged.
Implementation of Circular Queue Operations

• When the array is viewed as a circle, each array position has a next and a
previous position.
• The position next to MAX-QUEUE-SIZE -1 is 0, and the position that
precedes 0 is MAX-QUEUE-SIZE -1.
• When the queue rear is at MAX_QUEUE_SIZE-1, the next element is
inserted at position 0.
• In circular queue, the variables front and rear are moved from their
current position to the next position in clockwise direction. This may be
done using code
if (rear = = MAX_QUEUE_SIZE-1)
rear = 0;
else
rear++;
• If the element I is added into the queue as in figure (c), then rear needs to
increment by 1 and the value of rear is 8. Since queue is circular, the next
position should be 0 instead of 8.
• This can be done by using the modulus operator, which computes
remainders.
• (rear +1) % MAX_QUEUE_SIZE
Number of Elements in Circular
Queue
• The total number of elements in a circular queue at any point of time can be calculated
from the current values of the Rear and the Front indices of the queue.
• In case, Front<Rear, the total number of elements = Rear-Front+1. For instance, in Fig­ure
4.5(a), Front=3 and Rear=7.
• Hence, the total number of elements in CQueue at this point of time is 7-3+1=5.
• In case, Front>Rear, the total number of elements = Max+ (Rear-Front) +1. For instance,
in Figure 4.5(b), Front=3 and Rear=0. Hence, the total number of elements in CQueue is
8+(0-3)+l.
QUEUES USING DYNAMIC ARRAYS
• dynamic queue.docx
Dynamic creation of circular queue
dynamic circular queue.docx
MULTIPLE STACKS AND QUEUES
• In multiple stacks, we examine only sequential mappings of stacks
into an array. The array is one dimensional which is
memory[MEMORY_SIZE].
• Assume n stacks are needed, and then divide the available
memory into n segments.
• The array is divided in proportion if the expected sizes of the
various stacks are known.
• Otherwise, divide the memory into equal segments.
MULTIPLE STACKS AND QUEUES
• Assume that i refers to the stack number of one of the n stacks
• To establish this stack, create indices for both the bottom and top
positions of this stack.
• boundary[i] points to the position immediately to the left of the
bottom element of stack i, top[i] points to the top element. Stack i is
empty iff boundary[i]=top[i].
The declarations are:
#define MEMORY_SIZE 100 /* size of memory */
#define MAX_STACKS 10 /* max number of stacks plus 1 */
element memory[MEMORY_SIZE]; /* global memory declaration */
int top [MAX_STACKS];
int boundary [MAX_STACKS] ;
int n; /*number of stacks entered by the user */
MULTIPLE STACKS AND QUEUE
• top[0] = boundary[0] = -1;
• for (j= 1;j<n; j++)
• top[j] = boundary[j] = (MEMORY_SIZE / n) * j;
• boundary[n] = MEMORY_SIZE - 1;

0 1 [ m/n ] 2[ m/n ] m-1

boundary[ 0] boundary[1] boundary[ 2] boundary[n]


top[ 0] top[ 1] top[ 2]

All stacks are empty and divided into roughly equal segments.

In the figure, n is the number of stacks entered by the user, n < MAX_STACKS, and
m =MEMORY_SIZE. Stack i grow from boundary[i] + 1 to boundary [i + 1] before
it is full. A boundary for the last stack is needed, so set boundary [n] to MEMORY_SIZE-1 .
Two stacks
• When an array of STACK[n] is used to represent two stacks, say Stack A and
Stack B. Then the value of n is such that the combined size of both the
Stack[A] and Stack[B] will never exceed n.
• Stack[A] will grow from left to right, whereas Stack[B] will grow in opposite
direction i.e right to left.
#include <stdio.h>
#include <malloc.h>
#define MAX 10
int stack[MAX], topA = -1, topB = MAX;
void pushA(int val)
{
if(topA == topB-1)
printf("\n Overflow");
else
{
topA+=1;
stack[topA] = val;
}
}
int popA()
{
int val;
if(topA == -1)
{
printf("\n Underflow");
}
else
{
val = stack[topA];
topA--;
}
return val;
}
void display_stackA()
{
int i;
if(topA == -1)
printf("\n Stack A is empty");
else
{
for(i = topA;i >= 0;i--)
printf("\t %d",stack[i]);
}
}
void pushB(int val)
{
if(topB-1 == topA)
printf("\n Overflow");
else
{
topB-=1;
stack[topB] = val;
}
}
int popB()
{
int val;
if(topB == MAX)
{
printf("\n Underflow");
}
else
{
val = stack[topB];
topB++;
}
}
void display_stackB()
{
int i;
if(topB == MAX)
printf("\n Stack B is Empty");
else
{
for(i = topB; i < MAX;i++)
printf("\t %d",stack[i]);
}
}
Priority queues (pq)
• Priority Queue is a data structure in which the elements are assigned
priority such that the order in which elements are deleted and processed
follow the following rule:
• Element with the higher priority is processed before elements with lower
priority.
• Elements with same priority are processed in the order they are added to
the queue.
• 2 types of PQ are
Ascending PQ (APQ): In which items are inserted arbitrarily and from which
smallest item can be removed.
Descending PQ (DPQ): In which items are inserted arbitrarily and from
which largest item can be removed.
Why Linked List?
• Arrays can be used to store linear data of similar types, but arrays have
the following limitations.

• 1) The size of the arrays is fixed: So we must know the upper limit on
the number of elements in advance. Also, generally, the allocated
memory is equal to the upper limit irrespective of the usage.

• 2) Inserting a new element in an array of elements is expensive


because the room has to be created for the new elements and to
create room existing elements have to be shifted.

• Advantages over arrays


1) Dynamic size
2) Ease of insertion/deletion
LINKED LIST
Definition :
A linked list, or one-way list, is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers. That is, each node is
divided into two parts:
• The first part contains the information of the element, and
• The second part, called the link field or nextpointer field, contains the address of
the next node in the list.

Node Address of the next


node
Data link
In the above figure each node is pictured with two parts.

• The left part represents the information part of the node, which may contain an entire record of
data items.

• The right part represents the next pointer field of the node

• An arrow drawn from a node to the next node in the list.

• The pointer of the last node contains a special value, called the null pointer, which is any invalid
address.

• A pointer variable called H E A D o r START or FIRST which contains the address of the first node.

• A special case is the list that has no nodes, such a list is called the null list or empty list and is
denoted by the null pointer in the variable START.
Types of linked list
1. Singly linked list
2. Doubly linked list
3. Circular singly linked list
4. Circular doubly linked list
REPRESENTATION OF LINKED LISTS IN MEMORY
LIST will be maintained in memory as follows.

1. LIST requires two linear arrays such as INFO and LINK- such that
INFO[K] and LINK[K] contains the information part and the next-
pointer field of a node of LIST.

2. LIST also requires a variable name such as START which


contains the location of the beginning of the list, and a
nextpointer sentinel denoted by NULL-which indicates the end of
the list.

3. The subscripts of the arrays INFO and LINK will be positive, so


choose NULL = 0, unless otherwise stated.
REPRESENTING Linked list IN C
// A linked list node
struct node {
int data;
struct node* next;
};

Create structure variable for


node
struct node *NODE

Create Pointer variable for


NODE
NODE start

start =NULL //Empty linked list

To create a New Node


//Singly Linked List Implementation: Creating List code
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void create()
{
int choice;
//create linked list
struct node *head=NULL, *newnode,*temp;
while(choice)
{
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
temp=head=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
printf("Do you want to add one more node(0/1)\n");
scanf("%d",&choice);
Insertion algorithms
LINKED STACK AND QUEUES

• Notes
Polynomials

You might also like