BCS304 - Data
BCS304 - Data
BCS304 - Data
**********************************************************************
Advantages of Queue:
A large amount of data can be managed efficiently with ease.
Operations such as insertion and deletion can be performed with ease as it
follows the first in first out rule.
Queues are useful when a particular service is used by multiple consumers.
Queues are fast in speed for data inter-process communication.
Queues can be used in the implementation of other data structures.
Disadvantages of Queue:
The operations such as insertion and deletion of elements from the middle are
time consuming.
Limited Space.
In a classical queue, a new element can only be inserted when the existing
elements are deleted from the queue.
Searching an element takes O(N) time.
Maximum size of a queue must be defined prior.
Advantages of Circular Queue:
It provides a quick way to store FIFO data with a maximum size.
Efficient utilization of the memory.
Doesn’t use dynamic memory.
Simple implementation.
All operations occur in O(1) constant time.
Disadvantages of Circular Queue:
In a circular queue, the number of elements you can store is only as much as the
queue length, you have to know the maximum size beforehand.
Some operations like deletion or insertion can be complex in circular queue.
The implementation of some algorithms like priority queue can be difficult in
circular queue.
Circular queue has a fixed size, and when it is full, there is a risk of overflow if
not managed properly.
In the array implementation of Circular Queue, even though it has space to insert
the elements it shows that the Circular Queue is full and gives the wrong size in
some cases.
**********************************************************************
Circular Queue:
A circular queue is similar to a linear queue as it is also based on the FIFO (First
In First Out) principle except that the last position is connected to the first position
in a circular queue that forms a circle.
It is also known as a Ring Buffer.
Let's understand the enqueue and dequeue operation through the diagrammatic
representation.
Enqueue operation:
The steps of enqueue operation are given below:
o First, we will check whether the Queue is full or not.
o Initially the front and rear are set to -1.
o When we insert the first element in a Queue, front and rear both are set to 0.
o When we insert a new element, the rear gets incremented, i.e., rear=rear+1.
Scenarios for inserting an element
There are two scenarios in which queue is not full:
o If rear != max - 1, then rear will be incremented to mod(maxsize) and the new
value will be inserted at the rear end of the queue.
o If front != 0 and rear = max - 1, it means that queue is not full, then set the value
of rear to 0 and insert the new element there.
There are two cases in which the element cannot be inserted:
o When front ==0 && rear = max-1, which means that front is at the first position
of the Queue and rear is at the last position of the Queue.
o front== rear + 1;
Dequeue Operation
The steps of dequeue operation are given below:
o First, we check whether the Queue is empty or not. If the queue is empty, we
cannot perform the dequeue operation.
o When the element is deleted, the value of front gets decremented by 1.
o If there is only one element left which is to be deleted, then the front and rear are
reset to -1.
Display Operation
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
**********************************************************************
Singly Linked List (SLL):
The declaration of linked list is given as follows:
struct node
{
int data;
struct node *next;
};
In the above declaration, we have defined a structure named as node that contains
two variables, one is data that is of integer type, and another one is next that is a
pointer which contains the address of next node.
The declaration:
struct node
{
int data;
struct node *next;
};
Delete from the front (or) first node (beinning of the List):
NODE* delete_front(NODE *head)
{
NODE *p;
if(head==NULL)
printf("\n List is Empty ");
else
{
p=head;
head=head->next; //Set 2nd NODE as head
free(p);
printf("\n Front(first) node is deleted ... ");
}
return head;
}
Delete from the end (or) last node (rear of the List):
NODE* delete_end(NODE *head)
{
NODE *p, *q;
if(head==NULL)
printf("\n List is Empty ");
else
{
p=head;
while(p!=NULL)
{
q=p;
p = p->next; //Go to next node...
}
q->next=NULL;
free(p);
printf("\n End (Last) node is deleted ... ");
}
return head;
}
Display the data in all nodes (or) Traversing:
NODE* display(NODE *head)
{
NODE *p;
if(head == NULL)
printf("\n List is Empty");
else
{
p=head;
printf("\n ----Data in the list ----\n");
while(p!=NULL)
{
printf("\n%d", p->data);
p = p->next; //Go to next node...
}
}
return head;
}
************************************************************************
Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence.
Therefore, in a doubly linked list, a node consists of three parts:
o node data, pointer to the next node in sequence (next pointer) , pointer to
the previous node (previous pointer).
A sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in the following image.
int data;
The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
Implementation Doubly Linked List (DLL):
Create a List:
NODE* create(NODE *head)
{
NODE *newnode, *p;
p=head;
if(head==NULL)
{
newnode=getnode(head);
head=newnode;
}
else
{
newnode=getnode(head);
while(p->right!=NULL)
{
p=p->right;
}
p->right=newnode;
newnode->left=p;
}
return head;
}
Delete from the front (or) first node (beinning of the List):
NODE* delete_front(NODE *head)
{
NODE *p;
if(head==NULL)
printf("\n List is Empty");
else
{
p=head;
head=head->right;
head->right->left=NULL;
free(p);
printf("\n Front(first) node is deleted");
}
return head;
}
Delete from the end (or) last node (rear of the List):
NODE* delete_end(NODE *head)
{
NODE *p, *q;
if(head==NULL)
printf("\n List is Empty ");
else
{
p=head;
while(p!=NULL)
{
q=p;
p = p->right; //Go to next node...
}
q->right=NULL;
free(p);
printf("\n End (Last) node is deleted ... ");
}
return head;
}
************************************************************************