Posts

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

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