CD3291 -DATA STRUCTURES -UNIT 2 -NOTES
CD3291 -DATA STRUCTURES -UNIT 2 -NOTES
List ADT – array-based implementations – linked list implementations – singly linked lists –
circularly linked lists – doubly linked lists – Stack ADT – Queue ADT – double ended queues –
applications
Data Structures
Data Structures is a way of organizing the data which also includes several operations to
perform on that data.
• List: used in implementation of File allocation Table (FAT), Process Control Block (PCB)
etc.
• Stack: It is always implicitly used in a program, whether or not we declare it explicitly,
during the program execution.
• Queue: It is used to order the task as they arrive to get the attention of the CPU.
• Tree: Directory Structure is implemented using tree DS.
• Graph: used in Networking and mapping of tasks to processors in multiprocessor
Platforms.
1. Primitive Data Structures: All the fundamental DS that can be directly manipulated by the
machine level instructions.
2. Non Primitive Data Structures: refer to all those that are derived from one or more
primitive DS.
3. Linear Data Structures: arranged in sequential form
4. Non Linear Data Structures: not sequential order. Ex : data elements in a hierarchical
fashion (Tree).
Array is a collection of specific number of same type of data stored in consecutive memory
locations. Array is a static data structure.
i.e., size of the array should be allocated in advance and the size is fixed.
1. Insertion and Deletion operation are expensive as it requires more data movements.
Worst case it requires O(N) Movements. Best case it requires O(1).
Insertion / Deletion of data in the List: Insert / Delete operation is used to Insert/ Delete an
element at particular position in the existing list. Inserting/ deleting the element in the last
position of an array is easy.
But inserting/ Deleting the element at a particular position in an array is quite difficult since it
involves all the subsequent elements to be shifted one position to the right / left.
Display/ Traversal all data in the List: Traversal is the process of visiting the elements in an
array. Display( ) operation is used to display all the elements stored in the list from the index 0
to n-1.
Searching for a data in the list: Search( ) operation is used to determine whether a
particular element is present in the list or not.
Procedure to create / insert / delete
Create Insert Delete
void Delete( )
void Insert()
void Create()
{ int i,pos;
{ int i, data, pos;
{ int i;
printf("Enter the
printf("Enter data to insert");
printf("Enter number of position to delete”);
scanf("%d", &data);
elements");
scanf("%d", &pos);
printf("Enter the position to
scanf("%d",&n);
printf("The data
insert");
printf("Enter the array deleted is: %d",
scanf("%d", &pos);
elements"); list[pos-1]);
for(i = n-1; i >= pos-1 ; i--)
for(i=0; i<n; i++) for(i=pos-1;i<n-1;
{list[i+1] = list[i];} i++)
{scanf("%d", &list[i]); }
Display(); // swap // {list[i]=list[i+1]; }
list[pos-1] = data;
n=n-1;
}
n= n+1; Display(); }
Display(); }
//Main Program //
#include<stdio.h> #include<conio.h>
#define maxsize 10
int list[maxsize],n; // Declaration of Array//
void Create(); void Insert(); void Delete(); void Display(); void Search(); void
main()
{ int choice;
do
{printf("Array Implementation”);
printf("\t1.Create\n");printf("\t2.Insert\n");
printf("\t3.Delete\n");printf("\t4.Display\n");
printf("\t5.Search\n"); printf("\t6.Exit\n");
printf("\nEnter your choice:\t"); scanf("%d",&choice);
switch(choice)
{ case 1: Create(); break;
case 2: Insert(); break;
case 3: Delete(); break;
case 4: Display(); break;
case 5: Search();
break;
default: printf("Enter option between 1 - 5"); break; } }
while(choice<6); }
A Linked list is an ordered collection of elements. Each element in the list is referred as a
node. Each node contains two fields namely,
• Data field-The data field contains the actual data of the elements to be stored in the list.
• Next / Pointer field- The next field contains the address of the next node in the list
The nodes in the linked list are not necessarily adjacent in memory.
Insertion & Takes more time in array Are fast and easy
Deletion
Dynamic allocation
The process of allocating memory to the variables during execution of the program or at run
time is known as dynamic memory allocation.
When we do not know memory requirements in advance, we can use Dynamic memory
allocation.
C language has four library routines to support Dynamic allocation.
To use dynamic memory allocation functions, include the header file stdlib.h.
malloc() and calloc()
malloc() calloc()
Returns a pointer type of void after memory The malloc() function allocates a single
allocation, which can be casted to any form. block of memory.
However, if the space is insufficient, Whereas, calloc() allocates multiple
allocation fails and returns a NULL pointer blocks of memory and initializes them to
zero.
Example : ptr = (int*) malloc(100 * Example: ptr = (float*) calloc(25,
sizeof(int)); sizeof(float));
Considering the size of int is 4 bytes, this This statement allocates contiguous space
statement allocates 400 bytes of memory. in memory for 25 elements each with the
And, the pointer ptr holds the address of the size of float
first byte in the allocated memory.
free() realloc()
the memory is returned back to the free list Relloactes the previous memory
within the heap.
int main()
{ int n, i, *ptr;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL)
{ printf("Error! memory not allocated."); }
else { printf(“Memory allocated”); }
free(ptr); return 0;
}
A singly linked list is a linked list in which each node contains data field and only one link
field pointing to the next node in the list.
Head node which always points to the first node. Head node is also called as sentinel node.
Basic operations on a singly linked list are
1. Insert – Inserts a new node in the list.
2. Delete – Deletes any node from the list.
3. Find – Finds the position (address) of any node in the list.
4. FindPrevious - Finds the position (address) of the previous node.
5. FindNext - Finds the position(address) of the next node in the list.
6. Display - displays the data in the list
7. Search - finds whether an element is present in the list or not
Insertion: The insertion into a singly linked list can be performed in 3 ways.
1. Insertion at beginning: It involves inserting any element at the front of the list.
• Insertion at end of the list: It involves insertion at the last of the linked list. It can be done
in 2 ways
← The node is being added to an empty list (only one node in the list)
← The node can be inserted as the last one.
1.Insertion at a given position (after specified node): It involves insertion after the specified
node of the linked list.
else
else
{printf("Enter value");
{printf(“Enter value");
{printf("Enter value");
scanf("%d",&item);
scanf("%d",&item);
scanf("%d",&item);
ptr→data = item;
ptr→data = item;
ptr→data = item;
→
ptr next = head;
if(head == NULL)
printf("Enter the
head = ptr;
{ptr → next = NULL; location after which you
printf("Node inserted"); want to insert ");
head = ptr;
} } printf("Node inserted");} scanf("%d",&loc);
return; }
}
ptr →next =
temp→→next;
temp next = ptr;
printf("Node inserted");
}
}
Deletion: The deletion into a singly linked list can be performed in 3 ways.
1. Deletion at beginning: deleting an element at the front of the list.
• Deletion at end of the list: It can be done in 2 ways
← There is only one node in the list and that needs to be deleted.
← There are more than one node in the list and the last node of the list will be deleted.
• Deletion at a given position (after specified node): It involves deletion after the specified
node of the linked list.
{ printf("Main Menu");
printf("Choose one option from the following list ...");
printf("1.Insert in beginning 2.Insert at last 3.Insert at any random location 4.Delete from
Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element
8.Show 9.Exit");
printf("Enter your choice?"); scanf("%d", &choice);
switch(choice)
{ case 1: beginsert(); break; case 2:
lastinsert(); break; case 3:
randominsert(); break; case 4:
begin_delete(); break;
case 5: last_delete(); break;
case 6: random_delete(); break;
case 7: search(); break;
case 8: display(); break;
case 9: exit(0);
break;
default: printf("Please enter valid choice.."); }
} }
Complexity
Advantages of SLL
/ The elements can be accessed using the next link
/ Occupies less memory than DLL as it has only one next field.
Disadvantages of SLL
/ Traversal in the backwards is not possible
/ Less efficient to for insertion and deletion.
A doubly linked list is a linked list in which each node has three fields namely Data, Next,
Prev. The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.
Insertion: The insertion into a doubly linked list can be performed in 3 ways.
• Insertion at beginning: It involves inserting any element at the front of the list.
• Insertion at end of the list: It involves insertion at the last of the linked list. It can be done
in 2 ways
← The node is being added to an empty list (only one node in the list)
← The node can be inserted as the last one.
insertion after the new
specified node of the element
linked list.
into a
• Insertion at a P doubly
given position
(after specified rocedure for linked list
node): It involves Inserting a
Insertion at a given
Insertion at beginning Insertion at end of the list
position
• deletion at a given position (after specified node): It involves deletion after the specified
node of the linked list.
Procedure for Deleting a node into a doubly linked list
Deletion at a given
Deletion at beginning Deletion at end of the list
position
only one link field is there which stores next node which makes accessing of any node
difficult.
Disadvantages of DLL
The DLL has two pointer fields. One field is prev link field and another is next link field.
Because of these two pointer fields, more memory space is used by DLL compared to SLL
(ii) In a circular singly linked list, the last node of the list contains a pointer to the first
node of the list.
(iii)We traverse a circular singly linked list until we reach the same node where we
started.
(iv)The circular singly liked list has no beginning and no ending.
(v) There is no null value present in the next part of any of the nodes.
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
};
void main ()
while(choice != 7)
scanf("\n%d",&choice);
switch(choice)
{ case 1:
28
beginsert();
break;
default:
} }
void beginsert()
int item;
{ printf("\nOVERFLOW"); }
else
if(head == NULL)
{ head = ptr;
else
temp = temp->next;
ptr->next = head;
head = ptr; }
printf("\nnode inserted\n");
void lastinsert()
int item;
else
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
else
temp = head;
printf("\nnode inserted\n");
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{ printf("\nUNDERFLOW");
{ head = NULL;
free(head);
printf("\nnode deleted\n"); }
}
else
{ ptr = head;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
void last_delete()
if(head==NULL)
{ printf("\nUNDERFLOW"); }
free(head);
printf("\nnode deleted\n");
else
{ ptr = head;
{ preptr=ptr;
ptr = ptr->next; }
free(ptr);
printf("\nnode deleted\n");
void search()
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{ printf("\nEmpty List\n"); }
else
scanf("%d",&item);
{ if(ptr->data == item)
flag=0; break; }
else
{ flag=1; }
i++;
} }
if(flag != 0)
void display()
if(head == NULL)
{ printf("\nnothing to print"); }
else
{printf("\n printing values ... \n"); while(ptr ->
next != head)
} }
ptr = ptr -> next;
}
Applications of lists:
• Polynomial ADT
• Radix sort
• Multilist
Procedure for Polynomial Addition subtraction & differentiation using linked list:
Declaration of Linked list implementation of Polynomial:
struct poly{
Creation of Polynomial:
{ poly*ptr;
if(head==NULL)
{ head=newnode;
return(head); } else
{ ptr=head;
while(ptr-> next!=NULL)
ptr=ptr->next;
ptr->next=newnode; }
return(head);
Addition of Polynomials:
To add two polynomials we need to scan them once. If we find terms with the same exponent in
the two polynomials, then we add the coefficients; otherwise, we copy the term of larger
exponent into the sum and go on. When we reach at the end of one of the polynomial, then
remaining part of the other is copied into the sum.
• Add them.
Addition of Polynomials:
void add()
ptr1=list1;
ptr2=list2;
{ newnode=malloc(sizeof(struct poly));
if(ptr1->power==ptr2->power)
newnode->power=ptr1->power; newnode-
>next=NULL; list3=create(list3,newnode);
ptr1=ptr->next;
ptr2=ptr2->next;
else
{ newnode->coeff = ptr1->coeff
newnode->power=ptr1->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr1->next; }
else
{ newnode->coeff = ptr2->coeff
newnode->power=ptr2->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr2=ptr2->next; }
SUBTRACTION OF POLYNOMIALS:
MULTIPLICATION OF POLYNOMIALS:
void Mul()
ptr2=list2;
return;
{ newnode=malloc(sizeof(struct poly));
while(ptr2!=NULL)
newnode->power=ptr1->power + ptr2->power;
list3=create(list3,newnode); ptr2=ptr2->next;
}
ptr2=list2;
ptr1=ptr1->next;
} }
}}
Multilists:
• In a general multi-linked list each node can have any number of pointers to other nodes,
and there may or may not be inverses for each pointer.
• Multi-lists are essentially the technique of embedding multiple lists into a single data
structure.
• A multi-list has more than one next pointer, like a doubly linked list, but the pointers
create separate lists
• i. e Given a linked list where in addition to the next pointer, each node has a child
pointer, which may or may not point to a separate list.
• These child lists may have one or more children of their own, and so on, to produce a
multilevel data structure, as shown in below figure.
• You are given the head of the first level of the list.
• We can Flatten the list so that all the nodes appear in a single-level linked list.
• You need to flatten the list in way that all nodes at first level should come first, then
nodes of second level, and so on.
struct List
int data;
};
The above list can be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8-
>3->19->15
Stack ADT:
• Stack is a Linear Data Structure that follows Last in First Out (LIFO) principle.
• Stack is an ordered list in which, insertion and deletion can be performed only at
one end that is called top
• i.e. the element which is inserted last in the stack, will be deleted first from the stack.
• Example: - Pile of coins, stack of trays
TOP pointer
• It will always point to the last element inserted in the stack. For empty stack, top
will be pointing to -1. (TOP = -1) .
Operations On stack :
• Two fundamental operations performed on the stack are PUSH and POP.
• Return the top element , stack overflow, and stack underflow are other additional
functions.
(c) PUSH:
• It is the process of inserting a new element at the Top of the stack.
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
(b)POP:
• Peek operation involves returning the element which is present at the top of the stack
without deleting it.
• Underflow condition can occur if we try to return the top element in an empty stack.
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
3. Stack Overflow
• An Attempt to insert an element X when the stack is Full, is said to be stack overflow.
• For every Push operation, we need to check this condition.
4. Stack Underflow:
Implementation of Stack
• In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory.
• Each node contains a pointer to its immediate successor node in the stack.
• Stack is said to be overflown if the space left in the memory heap is not enough to create
a node.
void main ()
{ int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{ printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit"); printf("\n
Enter your choice \n"); scanf("%d",&choice);
switch(choice)
{ case 1: push();
break;
case 2: pop();
break;
case 3: display();
break; }
case 4: printf("Exiting....");
break;
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr; }
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{ printf("Underflow"); }
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{ printf("Stack is empty\n"); }
else
{
Applications of Stack
INFIX: The arithmetic operator appears between the two operands to which it is being applied.
Example1 : A + B
Example2 : A / B + C
POSTFIX: The arithmetic operator appears directly after the two operands to which it applies.
Also called reverse polish notation.
Example1 : A B +
Example2 : A B / C +
PREFIX: The arithmetic operator is placed before the two operands to which it applies.
Also called polish notation
Example1 : + A B
Example2 : + / A B C
Read the infix expression one character at a time until it encounters the delimiter “#”
Step 4: If the character is a ‘)’ , pop all the operators from the stack till it encounters ‘(‘, and
discard both the parenthesis in the output.
Step 5: If the Charater is ‘#’ pop all the character from stack
Output : A B C + / D *
Note : *, / , and % (modulus) are equal in precedence and higher precedence than +
and - .
^ is an exponential power operator. ^ has highest precedence than *, / , and %.
BODMAS: Its letters stand for Brackets, Order (meaning powers), Division, Multiplication,
Addition, Subtraction.
Stack
Input
(Note :Top of Stack Postfix Expression
Character
is bold)
A A
+ + A
( +( A
B +( AB
* +(* AB
C +(* ABC
- +(- ABC*
( +(-( ABC*
D
/
+(-( ABC*D
E
+(-(/ ABC*D ^
+(-(/ ABC*DE F
+(-(/^ ABC*DE )
+(-(/^ ABC*DEF *
+(- A B C *DEF^/ G
)
+(-* A B C *DEF^/ *
+(-* A B C *DEF^/G H
+ A B C *DEF^/G* - #
+* A B C *DEF^/G* -
+* A B C *DEF^/G* - H ABC*DEF^/G*-H*+
Output:
Resultant Postfix Expression: ABC*DEF^/G*-H*+
Eg 4: Write the procedure to convert the infix expression to postfix expression and steps
involved in evaluating the postfix expression. Convert the expression A-(B/C+
(D%E*F)/G)*H to postfix form. Evaluate the given postfix expression 9 3 4 * 8 + 4 / -.
Input: 20 6 4 + / 3*# .
Stack Action Taken
Input
(Note :Top of Output
Character
Stack is bold)
20 20 Push 2
6 20 6 Push 6
4 2064 Push 4
Pop 6 and 4 , perform +, and
+ 20 10
push the result
Pop 20and 10 , perform /,
/ 2
and push the result
3 23 Push 3
Pop 2 and 3 , perform *, and
* 6
push the result
# 6 Output
Example: Let us consider the expression ((a+b) * (d / e)) # Check the expression has
Balanced symbol.
Queue ADT:
• Queue is a Linear Data Structure that follows First in First out (FIFO) principle.
• Insertion of element is done at one end of the Queue called “Rear “end of the Queue.
• Deletion of element is done at other end of the Queue called “Front “end of the Queue.
• Example: - Waiting line in the ticket counter.
Front Pointer:- It always points to the first element inserted in the Queue.
Rear Pointer:- It always points to the last element inserted in the Queue.
For Empty Queue:-
Front (F) = - 1
Rear (R) = - 1
Operations on Queue
• It is the process of inserting a new element at the rear end of the Queue.
• For every EnQueue operation
oCheck for Full Queue
oIf the Queue is full, Insertion is not possible.
o Otherwise, increment the rear end by 1 and then insert the element in the rear end of the
Queue.
• It is the process of deleting the element from the front end of the queue.
• For every DeQueue operation
o Check for Empty queue
o If the Queue is Empty, Deletion is not possible.
o Otherwise, delete the first element inserted into the queue and then increment the front by
1.
• An Attempt to insert an element X at the Rear end of the Queue when the Queue is full
is said to be Queue overflow.
• For every Enqueue operation, we need to check this condition.
(iv) Queue Underflow: (front == -1 || front > rear)
• An Attempt to delete an element from the Front end of the Queue when the Queue is
empty is said to be Queue underflow.
• For every DeQueue operation, we need to check this condition.
/ Enqueue Operation
void enqueue()
{
int item;
printf("\nEnter the element\n");
scanf("%d",&item);
if(rear == maxsize-1)
{ printf("OVERFLOW"); return; }
if(front == -1 && rear == -1)
{ front = 0; rear = 0; }
else
/ Dequeue Operation
void dequeue ()
{ int item;
if (front == -1 || front > rear)
{ printf("\nUNDERFLOW\n"); return; } else
{
item = queue[front];
if(front == rear)
{ front = -1;
rear = -1 ;}
else
{ front = front + 1;}
printf("\nvalue deleted ");
}
}
//Display / front Operation
void display()
{ int i; if(rear
== -1)
{ printf("\nEmpty queue\n"); else
}
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{ printf("\n%d\n",queue[i]); }
}
}
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void enqueue ();
void dequeue ();
void display();
• Memory wastage: The space of the array, which is used to store queue elements, can never
be reused to store the elements of that queue because the elements can only be inserted at
front end and the value of front might be so high so that, all the space before that, can never
be filled.
• Deciding the array size: The extension in the array size is a time taking process and
almost impossible to be performed at runtime since a lot of reallocations take place. Due to
this reason, the array is declared as large enough, but most of the array slots can never be
reused. It will again lead to memory wastage.
• The array implementation of queue cannot be used for the large scale applications.
• In a linked queue, each node of the queue consists of two parts i.e. data part and the link
part. Each element of the queue points to its immediate next element in the memory.
• In the linked queue, there are two pointers maintained in the memory i.e. front pointer and
rear pointer. The front pointer contains the address of the starting element of the queue
while the rear pointer contains the address of the last element of the queue.
• Insertion and deletions are performed at rear and front end respectively. If front and rear
both are NULL, it indicates that the queue is empty. The linked
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1: insert(); break;
void insert()
{
struct node *ptr;
int item;
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data); ptr
= ptr -> next;
}
}
}
Applications of Queue
2. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
3. In real life, Call Center phone systems will use Queues, to hold people calling them in an
order, until a service representative is free.
4. Handling of interrupts in real-time systems. The interrupts are handled in the same order as
they arrive, First come first served.
5. Batch processing in operating system.
• With the array implementation of Queue, the element can be deleted logically only by
moving Front = Front + 1.
• Here the Queue space is not utilized fully.
Circular Queue
A circular queue uses its storage array as if it were a circle instead of a linear list. In Circular
Queue, elements are added at the rear end and the items are deleted at front end of the circular
queue. if the last location of the queue is full, then first element comes just after the last element.
Operations on Circular Queue
Fundamental operations performed on the Circular Queue are
• Circular Queue Enqueue
• Circular Queue Dequeue
Formula to be used in Circular Queue
int CDequeue ( )
{ if (front = = -1)
print ("Queue is underflow");
else { X = CQueue [Front];
if (front = = rear)
front = rear = -1;
else
front = (front + 1)% maxsize;
}
return (X);
}
ADVANTAGES
It is a structure that allows data to be passed from one process to another while making the most
efficient use of memory.
In DEQUE, insertion and deletion operations are performed at both ends of the Queue.
Exceptional Condition of DEQUE
(i) Input Restricted DEQUE
Here insertion is allowed at one end and deletion is allowed at both ends.
Operations on DEQUE
Four cases for inserting and deleting the elements in DEQUE are
1. Insertion At Rear End [ same as Linear Queue ]
2. Insertion At Front End
3. Deletion At Front End [ same as Linear Queue ]
4. Deletion At Rear End
/* The following program in C, implements the logic of Double ended queue, in which the
insertion & deletion from end as well as starting is allowed(circular array) */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 100 int queue[SIZE];
int F = -1; int R = -1;
/* To insert element in the rear of Double ended queue*/
void insert_r(int x)
{ if(F == (R+1)%SIZE)
{ printf("\nQueue Overflow"); }
else if(R == -1)
{ F = 0; R = 0; queue[R] =
x; } else
{ R = (R+1) %SIZE; queue[R] = x; } }
void insert_f(int x)
{ if(F == (R+1)%SIZE)
{ printf("\nQueue Overflow"); }
else if(R == -1) { F = 0; R = 0;
queue[R] = x; }
else { F = (F+SIZE-1) %SIZE; queue[F] = x; }
}
{ printf("\nQueue Underflow"); }
else if(F == R)
{ x = queue[F];
F=-1;R=-1;}
else
{ x = queue[R];
R = (R+SIZE-1)%SIZE;
}
return x; }
void main()
{ char choice; int x;
while(1)
{ printf("1: Insert From Front\n");
printf("2: Insert From Rear\n");
printf("3: Delete From Front\n");
printf("4: Delete From Rear\n");
printf("5: Exit Program\n");
printf("Enter Your Choice:");
choice = getche();
switch(choice) { case '1': printf("\nEnter Integer Data :");
scanf("%d",&x); insert_f(x); break;
case '2': printf("\nEnter Integer Data :");
scanf("%d",&x); insert_r(x); break;
case '3': printf("\nDeleted Data From Front End: %d",delete_f()); break;
case '4': printf("\nDeleted Data From Back End: %d",delete_r()); break;
case '5': exit(0); break; } }}
Definition:
A priority queue is a data structure that allows two operations:
• insert (enqueue operation) and
• delete_min (queue's dequeue operation with priroity), which finds, returns and removes the
minimum element in the heap.