C Program for Depth First Binary Tree Search without using Recursion

C Program for Depth First Binary Tree Search without using Recursion : The following C program performs a Depth First Search traversal. Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure or graph. The concept of backtracking is used in DFS. In this program we are performing DFS on a binary tree. In DFS, the deepest and unvisited node is visited and backtracks to it’s root if no siblings of that node exists. Here is the source code of the C program to apply DFS on a binary tree: #include <stdio.h> #include <stdlib.h> struct node { int a; struct node *left; struct node *right; int visited; }; void generate(struct node **, int); void DFS(struct node *); void delete(struct node **); …

Read More »

C Program to Traverse the Tree without using Recursion

C Program to Traverse the Tree without using Recursion : This C program will traverse the tree without using Recursion. The following C program, using iteration, searches for a given node in a tree. The tree we have used is the binary search tree. A binary search tree follows a concept of the nodes whose numbers are lesser than the parent/pointed node are linked to the left and the nodes whose are greater than the parent/pointed node are linked to the right. Here is the source code of the C program to search for an element in a tree: #include <stdio.h> #include <stdlib.h> struct node { int a; struct node *left; struct node *right; }; void generate(struct node **, int); int search(struct node *, int); …

Read More »

C Program to Search for an Element in the Linked List without using Recursion

C Program to Search for an Element in the Linked List without using Recursion : This C program, using iteration, searches for an element in a linked list. A linked list is an ordered set of data elements, each containing a link to its successor. Here is the source code of the C program to search for an element in a linked list. The C program is successfully compiled and run on a Linux system. #include <stdio.h> #include <stdlib.h> struct node { int a; struct node *next; }; void generate(struct node **, int); void search(struct node *, int); void delete(struct node **); int main() { struct node *head = NULL; int key, num; printf("Enter the number of nodes: "); scanf("%d", &num); printf("\nDisplaying the list\n"); generate(&head, …

Read More »

C Program to Solve Tower of Hanoi Problem using Recursion

The tower of hanoi is a mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod. Here is the source code of the C program for solving towers of hanoi. The C Program is successfully compiled and run on a Linux system. The program output is also shown below : /* * C program for Tower of Hanoi using Recursion */ #include <stdio.h> void towers(int, char, char, char); int main() { int num; printf("Enter the number of disks : "); …

Read More »

C Program to Solve the Magic Squares Puzzle

The following C program finds the magic square for a given odd sized number, using iteration. A magic square is an arrangement of numbers from 1 to n^2 in an [n x n] matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below : /* * C Program to Solve the Magic Squares Puzzle without using * Recursion */ #include <stdio.h> void magicsq(int, int [][10]); int main( ) { int size; int a[10][10]; …

Read More »

C Program to Implement a Stack using Linked List

In our previous code, we have seen how to write a C program to implement a stack. Today we will learn how to implement stack using Linked Lists. C Program to Implement a Stack using Linked List : This C Program implement a stack using linked list. Stack is a type of queue that in practice is implemented as an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are “pushed onto” the stack. When the function returns,these locals and parameters are “popped.” Because of this, the size of a program’s stack fluctuates constantly …

Read More »