Posts

Binary tree implementation with inorder traversal in C

              Binary tree has a root node with two child nodes. Child nodes are named as left child and right child. Let us create a binary tree in c . C implementation:   This implementation starts from the including the built-in header files. First, a node structure is created using structure. It has the member variables as data,left and right nodes. ‘create_Node()’   is used to create node. It allocates the memory for new node. The data and its position is assigned. ‘insert()’- This function is used to add the nodes to binary node. It includes the data. ‘in_order()’ -It is a type of traversal to print the data in order. ‘main()’- It helps to get the value from the user and call the functions. Finally, it prints the output value. Code: #include <stdio.h> #include <stdlib.h> // Create   a node structure struct b_Node {     int b_data;     struct b_Node *c_left, *c_r...

Josephus problem implementation in C

              Josephus problem is a puzzle problem. A group of people are there. They stand in a circle. Every kth person is eliminated until only one remains. To solve this problem, circular linked list is the best option. C implementation:               This implementation starts from creating Node structure. It has a data and node. There is a function create_Circle(). It is used to create the memory for node. Create the nodes as circular linked list. Josephus elimination function reads the input as number of persons and k value. Using the k value, keep on moving the circular linked list. The kth person is eliminated until the last one is remaining. Code: #include <stdio.h> #include <stdlib.h> //create a   Node structure with data and node struct cl_Node {     int data;     struct cl_Node *next; }; // circular link...

Circular Linked List traversal in C

              Circular Linked List is a type of linked list . It has a special feature which connects the last node to head as a circular format. So the traversal becomes the circular one. Implementation: It starts with declaring the Node structure . Next, a function create_Node() is written. It allocates the memory. New node’s data is assigned with data. New node’s next is assigned Null. To traverse a linked list, from the head to last node, each node is traversed and displayed the data. Finally, main() function,nodes are created. The traverse function is called and output is displayed. C Code: #include <stdio.h> #include <stdlib.h> // Node structure is declared struct Node {     int data;     struct Node* next; }; // Let us create a new node struct Node* create_Node(int data) {     struct Node* newNode = (struct Node*) malloc (sizeof(struct Node)); ...

Doubly Linked List implementation in C

               Doubly linked list is a type of linked list. It can traverse in both directions. The operations of doubly linked list are given below. ·        Creation of node. ·        Insert the data at the beginning. ·        Insert the data at the end. ·        Delete the data at the beginning. ·        Delete the data at the end. ·        Delete a data based on the value. ·        Traverse data in the forward direction. ·        Traverse data in the backward direction. C Code: #include <stdio.h> #include <stdlib.h> // Define the structure for a doubly linked list node struct Node {     int data;     struct Node* l_prev;   ...

C Program to implement Ticket Counter simulation using Queue

              If you travel around the world. You need to book a ticket. Here, we implement the Ticket Counter simulation . You can add a ticket, get the ticket and view the available tickets of customers. C implementation : ·        Queue size is defined. ·        Queue structure is created and values are initialised. Is_empty() and is_full() functions are created. ·        To insert the values, enqueue() function is used. ·        To delete the values, deque() function is used. ·        If you want to see the customers details, print() is written. ·        In the main() function , the choice of customer is received. According to the choice, the values are displayed. Code: #include < stdio.h > #include < stdlib.h > //defines the maximum s...

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) {     ...