Posts

Queue Implementation

Image
                                    Queue Implementation in C Language /**  * Queue implementation using array.  */ #include <stdio.h> #include <stdlib.h> #include <limits.h> // Queue capacity #define CAPACITY 100 /**  * Global queue declaration.  */ int queue[CAPACITY]; unsigned int size  = 0; unsigned int rear  = CAPACITY - 1;   // Initally assumed that rear is at end unsigned int front = 0; /* Function declaration for various operations on queue */ int enqueue(int data); int dequeue(); int isFull(); int isEmpty(); int getRear(); int getFront(); /* Driver function */ int main() {     int ch, data;     /* Run indefinitely until user manually terminates */     while (1)     {         /* Queue menu */         printf("...

Stack Implementation

Image
                Stack Implementation in C Language #include<stdio.h> int MAXSIZE =10; // Declaring the maximum size of the stack int stack[10];  int top = -1; int peek(){ // peek function to display the top element of the stack return stack[top]; } int isFull(){ // isFull function to check whether the stack is full or not if(top==MAXSIZE) return 1; else return 0; } int isEmpty(){ // isEmpty function to check whether the stack is empty or not if(top==-1) return 1; else return 0; } int pop(){ //pop function to delete the top element of the stack if(!isEmpty()){ int value; value = stack[top]; top =  top -1; return value; } else printf("Stack is empty, cannot pop elements\n"); } int push(int value){ // push function to insert elements into the stack if(!isFull()){ top = top + 1; stack[top] = value; } else printf("Stack is full, cannot insert mo...

Library Problem

Image
A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. No, think of the following two queries which can be performed on these shelves. ● Change the number of books in one of the shelves. ● Obtain the number of books on the shelf having the kth rank within the range of shelves. A shelf is said to have the kth rank if its position is k when the shelves are sorted based on the number of the books they contain, in ascending order. Can you write a program to simulate the above queries? Input Format : The first line contains a single integer T, denoting the number of test cases. The first line of each test case contains an integer N denoting the number of shelves in the library. The next line contains N space separated integers where the ith integer represents the number of books on the ith shelf where 1<=i<=N. The next line contains an integer Q denoting the number of queries to ...