Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
88 views17 pages

BCS304 - Data

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Queue:

 A Queue is a linear data structure.


 A Queue is an ordered collection of elements in which insertions are made
at one end and deletions are made at the other end.
 Rear end: It is where insertions are made at one end. It is pointing the last
element of the queue.
 Front End: It is where deletions are made at another end. It is pointing the first
element of the queue.
 FIFO: Queue is sometimes called as First In First Out lists.
C code - Operations in the queue using an array:
enQueue(value) - Inserting value into the queue
 In a queue data structure, enQueue() is a function used to insert a new
element into the queue.
 An enqueue operation inserts an element at the rear of the queue.
 The enqueue operation increments the rear end by adding new element
at the rear end.
We can use the following steps to insert an element into the queue...
 Step 1 - Check whether queue is FULL. (rear == SIZE-1)
 Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not
possible!!!" and terminate the function.
 Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and
set queue[rear] = value.
void enQueue(int value)
{
if(rear == SIZE-1)
printf("\n Queue is Full!!! Insertion is not possible!!!");
else
{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\n Insertion success!!!");
}
}
deQueue() - Deleting a value from the Queue
 In a queue data structure, deQueue() is a function used to delete an element
from the queue.
 The dequeue operation delete the elements at the front of the queue.
 The dequeue operation is incrementing the front end and deleting the first
element at the front end.
We can use the following steps to delete an element from the queue...
 Step 1 - Check whether queue is EMPTY. (front == rear)
 Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
 Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++).
Then display queue[front] as deleted element. Then check whether
both front and rear are equal (front == rear), if it TRUE, then set
both front and rear to '-1' (front = rear = -1).
void deQueue()
{
if(front == rear)
printf("\n Queue is Empty!!! Deletion is not possible!!!");
else
{
printf("\n Deleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}

display() - Displays the elements of a Queue


We can use the following steps to display the elements of a queue...
 Step 1 - Check whether queue is EMPTY. (front == rear)
 Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the
function.
 Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set
'i = front+1'.
 Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the
same until 'i' value reaches to rear (i <= rear)
void display()
{
if(rear == -1)
printf("\n Queue is Empty!!! ");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}

isFull() - to check queue is full or not:


 This operation indicates whether the queue is full or not.
 As we are using single dimension array to implement queue, we just check for the
rear pointer to reach at MAXSIZE to determine that the queue is full.
isFull()
{
if(rear == SIZE-1)
printf("\n Queue is Full!!! Insertion is not possible!!!");
}

isempty() - to check queue is empty or not:


 This operation indicates whether the queue is empty or not
 If the value of front is less than 0, it tells that the queue is not yet initialized,
hence empty.
isEmpty()
{
if(rear == -1)
printf("\n Queue is Empty !!! ");
}

**********************************************************************
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;

// function to insert an element in a circular queue

void enqueue(int element)


{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element;
// assigning a value to the queue at the rear position.
}
}

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.

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\n Queue is underflow..");
}
else if(front==rear)
{
printf("\n The dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}

Display Operation

// function to display the elements of a queue

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.

Implementation Singly Linked List (SLL):

The declaration:
struct node
{
int data;
struct node *next;
};

typedef struct node NODE;

Getnode (or) Create a node:


NODE* getnode(NODE *head)
{
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\n Enter new data");
scanf("%s", &newnode->data);
newnode->next=NULL; //Set next to NULL...
head=newnode;
return head;
}
Create a List:
NODE* create(NODE *head)
{
NODE *newnode;
if(head==NULL)
{
newnode=getnode(head);
head=newnode;
}
else
{
newnode=getnode(head);
newnode->next=head;
head=newnode;
}
return head;
}

Insert at the Front (beinning of the List):


NODE* insert_front(NODE *head)
{
if(countnodes(head)==MAX)
printf("\n List is Full or Overflow !!... ");
else
head=create(head); //create()insert nodes at front, return head;
return head;
}

Insert at the End (rear of the List):


NODE* insert_end(NODE *head)
{
NODE *newnode, *p;
if(countnodes(head)==MAX)
printf("\n List is Full or Overflow !!... ");
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
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->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;
}

Search the element in the SLL:

NODE* search(NODE *head)


{
NODE *p;
int key;
if(head == NULL)
printf("\n List is Empty");
else
{
p=head;
printf("\n Enter the element to be search in the list: \n");
Scanf(“%d”,&key);
while(p!=NULL)
{
if (key == p->data)
printf("\n The Given element is present in the list \n");
else
p = p->right; //Go to next node...
}
}
return head;
}
Count the no of data in the list:
int countnodes(NODE *head)
{
NODE *p;
count=0;
p=head;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}

************************************************************************

Doubly linked list

 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.

In C, structure of a node in doubly linked list can be given as :


struct node
{

int data;

struct node *prev;

struct node *next;

 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):

The declaration of linked list is given as follows:


struct node
{
int data;
struct node *left;
struct node *right;
};

typedef struct node NODE;

Getnode (or) Create a node:


NODE* getnode(NODE *head)
{
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\n Enter new data");
scanf("%s", &newnode->data);
newnode->right=NULL;
newnode->left=NULL;
head=newnode;
return head;
}

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;
}

Insert at the Front (beinning of the List):


NODE* insert_front(NODE *head)
{
NODE *p, *newnode;
if(countnodes(head)==MAX)
printf("\n List is Full !!");
else
{
if(head==NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
newnode->right=head;
head->left=newnode;
head=newnode;
}
}
return head;
}
Insert at the End (rear of the List):
NODE* insert_end(NODE *head)
{
NODE *newnode, *p;
if(countnodes(head)==MAX)
printf("\n List is Full or Overflow !!... ");
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
while(p->right!=NULL)
{
p=p->right;
}
p->right=newnode;
}
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;
}

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->right; //Go to next node...
}
}
return head;
}

Search the element in the DLL:

NODE* search(NODE *head)


{
NODE *p;
int key;
if(head == NULL)
printf("\n List is Empty");
else
{
p=head;
printf("\n Enter the element to be search in the list: \n");
Scanf(“%d”,&key);
while(p!=NULL)
{
if (key == p->data)
printf("\n The Given element is present in the list \n");
else
p = p->right; //Go to next node...
}
}
return head;
}

Count the no of data in the list:


int countnodes(NODE *head)
{
NODE *p;
count=0;
p=head;
while(p!=NULL)
{
p=p->right;
count++;
}
return count;
}

************************************************************************

You might also like