Posts

Double ended Queue implementation in C

               It is called deque . You can add or delete data in both the ends. It can be implemented by arrays or linked list . Key terms: Front: it represents the first element. Rear: it denotes the last element. Operations: Insert the data in front end. Delete data in front end. Insert the data in rear end. Delete the data in rear end. Display the elements. Let us implement the deque in C as follows. #include <stdio.h> #define MAX 9 int deque[MAX]; int d_front = -1, d_rear = -1; void insert_Front(int x) {     if ((d_front == 0 && d_rear == MAX-1) || (d_front == d_rear+1)) {         printf("Deque is full with data elements.\n");         return;     }     if (d_front == -1) {         d_front = d_rear = 0;     } else if (d_fr...

Circular Queue implementation in C

              Circular queue is a type of queue in which both of the end wrapped together to form a circle. It has two pointers rear and front. Features: It follows FIFO principle . Front points the element to be dequeued. Rear points the last inserted element. Functions in the circular Queue: ‘isFull()’ – Checks for queue is full or not. ‘isEmpty()’-Checks for emptiness. ‘peek()’ -View the front element without deleting. ‘Dequeue()’ – Deletes the front element. ‘Enqueue()’ – Inserts the value to the queue. Code implementation : #include <stdio.h> #define SIZE 6 int c_queue[SIZE]; int cq_front = -1, cq_rear = -1; // Check the queue is full or not int isFull() {     return (cq_front == (cq_rear + 1) % SIZE); } // Check the queue is empty or not int isEmpty() {     return (cq_front == -1); } // Add elements to the queue void enqueue(int element) {     ...

C Program to implement Queue using Arrays

                Queue is a linear data structure which uses the principle FIFO(First In,First Out). Imagine a queue. The person who comes first, will leave first. Let us implement the Queue in C language . Queue implementation using Arrays :               This program starts by including the built-in header file. It has following steps. Steps: ·        It defines the maximum size as 6. ·        An array is created as integer with max size we defined already. ·        Front and rear are the two variables to represent front end and rear end. ·        First, we check the queue is full or not. Next, we check for empty or not. ·        ‘Enqueue()’ is the operation to insert the values to the queue. It checks for queue is full or n...

C program to check for balanced parentheses using Stack:

              Let us create a c program for balance parentheses . It uses Stack for expression evaluation . Program implementation: This program includes the built-in header files .   It defines the MAX value as 50. let us create a stack of MAX size. Initialise the value as -1. Create functions push(),pop(),isMatching(),is_Balanced(). ‘push()’ – it takes a data and insert into the stack. It checks the top value is equals to MAX-1. If it is equal. Then print the stack is overflow. Otherwise, it inserts the value into stack. ‘pop()’ – it deletes the data. It checks the top value is equal to -1. If yes, it returns 0.otherwise, it returns the top value. ‘isMatching()’ – it takes input as two characters. The characters are   checked with characters like ‘(‘,’)’,’{‘,’}’,’[‘,’]’. ‘is_Balanced()’ - it reads an expression. It separates the parenthesis of any type and stores in the stack. It uses ‘isMatching()’ function to check for match. If it...

Stack implementation using arrays in C

               Stack is a linear data structure which stores data. It uses the LIFO(Last In First Out) principle. It has following functions. ‘push()’ – it inserts the data into stack. ‘pop()’ – it deletes the data from the stack. Program implementation: ·        This program includes the built-in header file . It defines the maximum size as 50. ·        Stack and top values are initialised. ·        To insert the value, check top value with maximum size to check for overflow . If it is overflow, the message is displayed. Otherwise, the data is inserted. ·        To delete the value, check for top value. If it is -1, then it is called underflow . Otherwise, the data is removed from the top. ·        Finally, data is displayed using print (). C Code : #include < stdio.h > ...

C program to count Vowels, Consonants and digits in a String

              String is a collection of characters. This program counts the vowels present in the input string, consonants appear in the input and numbers available in the string. Let us implement it in C language .               This program reads the input string from the user using fgets() function . Using a for loop , each character is separated. Each character is checked whether it is an alphabet or a digit . If it is a digit, the count of digit variable is increased by one. If it is an alphabet, the alphabet is converted as lowercase. It is compared with vowels. If it a vowel, vowel count is increased. Otherwise, consonant count is increased. Finally, each count is displayed. C Code: #include <stdio.h> #include <ctype.h>   //It has built-in function to check the characters and numbers int main() {     char s[100]; ...

Reverse a string in C

               Reverse a string deal with reverse each character of the entire string. It can be done by user defined function or built-in function. First, we implement it using the user defined function. It uses reverse_String() for implementing the function. 👉C Code: //include the header files #include <stdio.h> #include <string.h> //a user defined function ‘reverse_String()’ is defined here. //starting and ending point and temporary variables are created. //each character is read and reversed using temp variable. void reverse_String(char s[]) {     int start = 0;     int end = strlen(s) - 1;     char temp;     while (start < end) {         // Exchange the characters         temp = s[start];         s[start] = s[end];   ...