Posts

Binary Tree implementation in C

               Tree is a non-linear data structure which deals with root element and child nodes.Root is the main node which connects the entire data structure. It has childs which is divided into 2 types. The two categories are Left and right. Let us create a binary tree which has exactly two child is given below… C implementation of binary Tree using Linked list: Here, the binary tree is created using linked list . A node is created as root. It has two nodes and data. ‘createNode()’ creates a node with memory and data. There are three traversal to print the node data. Pre-order traversal : It follows ‘Root-left child-right child’ format. Post – order traversal :it uses ‘left child – right child- root’ format. In-order traversal :It has ‘left child- root- right child’ format. Code: #include <stdio.h> #include <stdlib.h> // create the tree as linked list struct T_Node {     int data; ...

Graph implementation in C

                 Graph is a non-linear one which has vertices and edges. Vertices are the nodes and the edges are links which connect the nodes. General representation of G is represented by G = (V,E) Where, V represents by vertices and E represents by edges. Let us implement the graph in two different ways as follows.. 1.       Adjacency Matrix 2.       Adjacency List 1.Adjacency Matrix implementation of Graph:               This method follows a two-dimensional Matrix to implement the graph. It creates a 2D matrix to get the graph structure. It reads the edges from the user and assigns the value. Finally displays the graph. C Program: #include <stdio.h> #define V 4 void printIt(int adjM_graph[V][V]) {     for (int i = 0; i < V; i++) {         f...

Linked List implementation for deletion, search and reverse Operations in C

              As earlier post contains the linked list implementation and insertion operation in C. here, I include the operations on linked list which contains deletion, search and reverse. Code: #include <stdio.h> #include <stdlib.h> // Node structure is created struct L_Node {     int n_data;     struct L_Node* next; }; //Let us create a new node struct L_Node* createNode(int n_data) {     struct L_Node* newNode = (struct L_Node*)malloc(sizeof(struct L_Node));     newNode->n_data = n_data;     newNode->next = NULL;     return newNode; } // creation of Insertion at end void insertEnd(struct L_Node** head, int n_data) {     struct L_Node* newNode = createNode(n_data);     if (*head == NULL) {         *head = newNode;    ...

Linked list implementation in C

               It is a mostly used linear data structure in programming. Linked list has a collection of nodes and pointers. Pointers has the address of the next node. Node has the data. First node is called Head node. Last node has null pointer. Let us implement the linked list with insertion operation. Program: #include <stdio.h> #include <stdlib.h> // creation of   the node structure struct l_Node {     int n_data;     struct l_Node* next; }; //create a new node struct l_Node* createNode(int n_data) {     struct l_Node* newNode = (struct l_Node*)malloc(sizeof(struct l_Node));     if (!newNode) {         printf("Memory is not allocated\n");         exit(1);     }     newNode->n_data = n_data;     newNode->next = NUL...

Queue implementation in C

                Queue is another linear data structures . Have you stand in a queue. People who stands in the front will go first. Same principle is applicable here. This principle is called FIFO(First In First Out). Functions and variables in queue: First element denotes front. Last element represents rear. To insert an element in rear end use enqueue (). To delete an element, consider dequeue() function. ‘peek()’ is a function which makes the user to view the front element. It doesn’t delete the element. Let us create a queue using arrays in C . Program: #include < stdio.h > #include < stdlib.h > #define MAX 9    // Let us declare the maximum size of queue int queue[MAX]; int front = -1, rear = -1; // queue is full or not int isFull() {     return (rear == MAX - 1); } // Queue empty checking int isEmpty() {     return (fr...

Stack implementation in C

              Stack is a primitive data structure which has a principle of LIFO (Last In First Out).It means the last element inserted in the stack will be coming first out. Stack data structure is very much useful in memory management, expression evaluation, towers of Hanoi problem and so on. Let us implement Stack in C language as array. Basic Operations of the Stack are ‘push()’ – To insert a data ‘pop()’ – To Delete a data ‘peek()’ -It makes you to view the top data without deleting it. ‘isEmpty()’-   It checks for Stack emptiness. ‘isFull()’- It checks for Stack is full or not. Variable:   A pointer variable is called top. C Program: #include <stdio.h> #define MAX 12 int stack[MAX]; int top = -1; // let us Check if the stack is empty or not int isEmpty() {     return top == -1; } // It is used to Check if the stack is full or not int isFull() {     return top ==...

String Traversal implementation in C

       Strings are a collection of characters followed by a ‘null’ character. ’Null’ is represented by ‘\0’. In C, string  is considered as character array Syntax: To declare a string: ‘char string_name[]; To assign a value: ‘string_name[]’ = “value”; ‘value’ may be a string or set of characters as follows. a[]= “Sample”; a[15] = {‘S’,’a’,’m’,’p’,’l’,’e’,’\0’}; How to get the input and print the output? There are some built-in functions to read and write the string. ‘printf()’,’puts()’ -To print the output. ‘scanf()’,’gets()’- To get the input from the user. ‘fgets()’ – it is used to get the input from file. Built-in functions to perform String operations: ‘strlen(string)’ – it gives you the length of the string. ‘strcpy(destination, source) – It Copies source data into destination. ‘strcat(dest, src)’ – it helps in concatenation of two strings. ‘strcmp(str1, str2)’ - it Compares two strings. It returns 0 if equal, otherwise it is 1...