Breadth First Search (BFS) in C

Breadth First Search/Traversal

C program to implement Breadth First Search(BFS). Breadth First Search is an algorithm used to search a Tree or Graph. BFS search starts from root node then traverses into next level of graph or tree, if item found it stops other wise it continues with other nodes in the same level before moving on to the next level. The algorithm can also be used for just Tree/Graph traversal, without actually searching for a value. This is what being done in the program below. The disadvantage of BFS is it requires more memory compare to Depth First Search(DFS).

The Program

#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
for(i=1; i <= n; i++) {
q[i] = 0;
visited[i] = 0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1; i <= n; i++) {
if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
}
view raw bfs.c hosted with ❤ by GitHub

Sample Output

BFS in C

The graph’s matrix representation is used as input to our program. A value of 1 at [i][j] represents presence of a path from i to j. 0 represents no path.

Related Programs

To get regular updates on new C programs, you can Follow @c_program on Twitter. Or you can discuss these programs on our Facebook Page.

Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com

C Program to implement Dijkstra’s algorithm.

C Program to implement Dijkstra’s algorithm. Dijkstra’s Algorithm finds the shortest path with the lower cost in a Graph. Dijkstra’s Algorithm solves the Single Source Shortest Path problem for a Graph. It is a Greedy algorithm and similar to Prim’s algorithm. Read more about C Programming Language .


/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include "stdio.h"
#include "conio.h"
#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}

void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("n Enter the number of nodes:");
scanf("%d",&n);
printf("n Enter the cost matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("n Shortest path:n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%dn",v,i,dist[i]);
getch();
}
Read more Similar C Programs
Data Structures
Prims Algorithm

Learn C Programming

You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to solve Knapsack problem

C Program to solve Knapsack problem. Knapsack problem is also called as rucksack problem. In Knapsack problem, given a set items with values and weights and a limited weight bag . We have to find the optimum solution so that, in minimum cost(value) fill the bag with the maximum weight. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("nEnter the number of elementsn");
scanf("%d",&n);
printf("Enter the profit and weights of the elementsn");
for(i=1;i<=n;i++)
{
printf("For item no %dn",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("nEnter the capacity n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included aren");
printf("Sl.notweighttprofitn");
for(i=1;i<=n;i++)
if(x[i])
printf("%dt%dt%dn",++count,w[i],p[i]);
printf("Total profit = %dn",profit);
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement HEAP sort

Data structures using C, Heap sort algorithm starts by building a heap from the given elements,and then heap removes its largest element from the end of partially sorted array. After removing the largest element, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from the end of the partially sorted array. This is repeated until there are no items left in the heap and the sorted array is full. Elementary implementations require two arrays – one to hold the heap and the other to hold the sorted elements. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>


void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);




main()
{
int n,i,a[50];
system("clear");

printf("nEnter the limit:");
scanf("%d",&n);

printf("nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

heapsort(a,n);

printf("nThe Sorted Elements Are:n");
for(i=0;i<n;i++)
printf("t%d",a[i]);
printf("n");
}

void heapsort(int a[],int n)
{
int i,t;

heapify(a,n);

for(i=n-1;i>0;i--)
{
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}


void heapify(int a[],int n)
{
int k,i,j,item;

for(k=1;k<n;k++)
{
item = a[k];
i = k;
j = (i-1)/2;

while((i>0)&&(item>a[j]))
{
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}

void adjust(int a[],int n)
{
int i,j,item;

j = 0;
item = a[j];
i = 2*j+1;

while(i<=n-1)
{
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i])
{
a[j] = a[i];
j = i;
i = 2*j+1;
}
else
break;
}
a[j] = item;
}

Read more Similar C Programs
Data Structures
C Sorting Techniques
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement quick sort

C Program to implement quick sort. Quick sort algorithm is based on divide and conquer strategy. In a quick sort we take the one element called as pivot,then we list all the smaller elements than pivot, and greater than pivot. after partitioning we have pivot in the final position. After recursively sorting the partition array, we get the sorted elements. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}//end swap

void quicksort( int a[], int low, int high )
{
int pivot;
// Termination condition!
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
} //end quicksort

int partition( int a[], int low, int high )
{
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{
// Move left while item < pivot
while( a[left] <= pivot_item )
left++;
// Move right while item > pivot
while( a[right] > pivot_item )
right--;
if ( left < right )
swap(a,left,right);
}
// right is final position for the pivot
a[low] = a[right];
a[right] = pivot_item;
return right;
}//end partition

// void quicksort(int a[], int, int);
void printarray(int a[], int);

int main()
{
int a[50], i, n;
printf("nEnter no. of elements: ");
scanf("%d", &n);
printf("nEnter the elements: n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("nUnsorted elements: n");
printarray(a,n);
quicksort(a,0,n-1);
printf("nSorted elements: n");
printarray(a,n);

}//end main


void printarray(int a[], int n)
{
int i;
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("n");
}//end printarray

Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to Implement the multiple priority queue.

Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple multiple priority queue. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#define MAX 3
struct myqueue {
int f, r;
int a[MAX];
};
typedef struct myqueue pqueue;
void insert(pqueue *);
void delete(pqueue *);
void display(pqueue *, pqueue *, pqueue *);
int isempty(pqueue *);
int isfull(pqueue *);

main() {
pqueue q[3];
int ch, i, n;
for (i = 0; i < 3; i++) {
q[i].f = 0;
q[i].r = -1;
}
while (1) {
printf("n******* MENU ********n");
printf("1.Insertn2.Deleten3.Displayn4.Exit");
printf("nEnter your choice:n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("nEnter priority:n");
scanf("%d", &n);
insert(&q[n - 1]);
break;

case 2:
if (isempty(&q[0])) {
if (isempty(&q[1])) {
if (isempty(&q[2])) {
printf("nAll queues are empty..n");
break;
} else
n = 2;
} else
n = 1;
} else
n = 0;
delete(&q[n]);
break;

case 3:
display(&q[0], &q[1], &q[2]);
break;
case 4:
exit(0);
default:
printf("nInvalid choice..");
break;
}
}
}

int isempty(pqueue *q) {
if (q->f > q->r)
return 1;
else
return 0;
}

int isfull(pqueue *q) {
if (q->r == MAX - 1)
return 1;
else
return 0;
}

void insert(pqueue *q) {
int x;
if (isfull(q)) {
printf("nQueue overflow..");
return;
} else {
printf("nEnter the element to be inserted:n");
scanf("%d", &x);
(q->r)++;
q->a[q->r] = x;
}
}

void delete(pqueue *q) {
int n;
n = q->a[q->f];
(q->f)++;
printf("The deleted element is = %d", n);
}

void display(pqueue *q1, pqueue *q2, pqueue *q3) {
int i;
if (isempty(q1))
printf("nQueue1 is empty..");
else
printf("nThe contents of queue1 are:n");
for (i = q1->f; i <= (q1->r); i++) {
printf("%dt", q1->a[i]);
}
if (isempty(q2))
printf("nQueue2 is empty..");
else
printf("nThe contents of queue2 are:n");
for (i = q2->f; i <= (q2->r); i++) {
printf("%dt", q2->a[i]);
}
if (isempty(q3))
printf("nQueue3 is empty..");
else
printf("nThe contents of queue3 are:n");
for (i = q3->f; i <= (q3->r); i++) {
printf("%dt", q3->a[i]);
}
}

/
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to implement Towers of Hanoi and Binary Search

C Recursion:
C Program to implement Towers of Hanoi and Binary Search using the Recursion method. Recursive functions solves the complexity of the problem by calling the function again and again itself. Using recursive methods we can save execution time and memory. In this program we have two recursive functions for Binary search and the Tower of Hanoi problem. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
main() {
int n, a[50], key, opn, i, pos;

do {
clrscr();
printf(
" nn Press 1 -> Binary Search , 2-> Towers of Hanoi 3-> Quitn");
scanf("%d", &opn);
switch (opn) {
case 1:
printf(" How Many Elements?");
scanf("%d", &n);
printf(" Read all the elements is ASC order n");
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf(" Read the Key Elementn");
scanf("%d", &key);
pos = BS(a, key, 1, n);
if (pos)
printf(" Success: %d found at %d n", key, pos);
else
printf(" Falure: %d Not found in the list ! n", key);
break;
case 2:
printf("nn How Many Disks ?");
scanf("%d", &n);
printf("nn Result of Towers of Hanoi for %d Disks n", n);
tower(n, 'A', 'B', 'C');
printf("nn Note: A-> Source, B-> Intermediate, C-> Destinationn");
break;
case 3:
printf(" Terminating n");
break;
default:
printf(" Invalid Option !! Try Again !! n");
}
printf(" Press a Key. . . ");
getch();
} while (opn != 3);
}

int BS(int a[], int key, int low, int high) {
int mid;

if (low > high)
return 0; /* failure */
else {
mid = (low + high) / 2;
if (a[mid] == key)
return mid; /* Success */
if (key < a[mid])
return (BS(a, key, low, mid - 1));
return (BS(a, key, mid + 1, high));
}
}

tower(int n, char src, char intr, char dst) {
if (n > 0) {
tower(n - 1, src, dst, intr);
printf("Move disk %d from %c to %c n", n, src, dst);
tower(n - 1, intr, src, dst);
}
}

Read more Similar C Programs

Data Structures


C Recursion

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program for Binary Search Tree Creation and Traversals

C Program for Binary Search Tree Creation and Traversals. Binary Search Tree is a Tree which has the following properties, 1.The left sub tree of a node contains smaller nodes than a root node. 2.The right sub tree of a node contains greater nodes than a root node. 3.Both the left and right sub trees must also be binary search trees. There are three types of tree traversals, Preorder, Postorder, and Inorder. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/


#include <stdlib.h>
typedef struct tnode {
 int data;
 struct tnode *right, *left;
} TNODE;

TNODE *CreateBST(TNODE *, int);
void Inorder(TNODE *);
void Preorder(TNODE *);
void Postorder(TNODE *);

main() {
 TNODE *root = NULL; /* Main Program */
 int opn, elem, n, i;
 do {
  clrscr();
  printf("n ### Binary Search Tree Operations ### nn");
  printf("n Press 1-Creation of BST");
  printf("n       2-Traverse in Inorder");
  printf("n       3-Traverse in Preorder");
  printf("n       4-Traverse in Postorder");
  printf("n       5-Exitn");
  printf("n       Your option ? ");
  scanf("%d", &opn);
  switch (opn) {
  case 1:
   root = NULL;
   printf("nnBST for How Many Nodes ?");
   scanf("%d", &n);
   for (i = 1; i <= n; i++) {
    printf("nRead the Data for Node %d ?", i);
    scanf("%d", &elem);
    root = CreateBST(root, elem);
   }
   printf("nBST with %d nodes is ready to Use!!n", n);
   break;
  case 2:
   printf("n BST Traversal in INORDER n");
   Inorder(root);
   break;
  case 3:
   printf("n BST Traversal in PREORDER n");
   Preorder(root);
   break;
  case 4:
   printf("n BST Traversal in POSTORDER n");
   Postorder(root);
   break;
  case 5:
   printf("nn Terminating nn");
   break;
  default:
   printf("nnInvalid Option !!! Try Again !! nn");
   break;
  }
  printf("nnnn  Press a Key to Continue . . . ");
  getch();
 } while (opn != 5);
}

TNODE *CreateBST(TNODE *root, int elem) {
 if (root == NULL) {
  root = (TNODE *) malloc(sizeof(TNODE));
  root->left = root->right = NULL;
  root->data = elem;
  return root;
 } else {
  if (elem < root->data)
   root->left = CreateBST(root->left, elem);
  else if (elem > root->data)
   root->right = CreateBST(root->right, elem);
  else
   printf(" Duplicate Element !! Not Allowed !!!");

  return (root);
 }
}
void Inorder(TNODE *root) {
 if (root != NULL) {
  Inorder(root->left);
  printf(" %d ", root->data);
  Inorder(root->right);
 }
}

void Preorder(TNODE *root) {
 if (root != NULL) {
  printf(" %d ", root->data);
  Preorder(root->left);
  Preorder(root->right);
 }
}

void Postorder(TNODE *root) {
 if (root != NULL) {
  Postorder(root->left);
  Postorder(root->right);
  printf(" %d ", root->data);
 }
}

Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement Doubly Linked List Operations

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In Doubly Linked List, each node contains three fields, one is to store data and other two fields stores the reference(address) of the previous and next node. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdlib.h>
typedef struct dnode {
int data;
struct dnode *next, *prev;
} DNODE;

DNODE *InsFront(DNODE *, int);
DNODE *InsBefore(DNODE *, int, int);
DNODE *DelNode(DNODE *, int);
void Display(DNODE *);

main() {
DNODE *start = NULL; /* Main Program */
int opn, elem, info, n, i;
do {
clrscr();
printf("n ### Doubly Linked List Operations ### nn");
printf("n Press 1-Creation with front Insertion");
printf("n 2-Insert Before a Given Node");
printf("n 3-Delete a Given Node");
printf("n 4-Display");
printf("n 5-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnHow Many Nodes ?");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("nRead the Data for Node %d ?", i);
scanf("%d", &elem);
start = InsFront(start, elem);
}
printf("nDoubly Linked list with %d nodes is ready toUse!!n", n);
break;
case 3:
printf(" Read the Info of the Node to be deleted ? ");
scanf("%d", &info);
start = DelNode(start, info);
break;
case 2:
printf(" Read the Data for New noden");
scanf("%d", &elem);
printf(
" Read the Info of the Node(to the left of which new node tobe inserted ? ");
scanf("%d", &info);
start = InsBefore(start, elem, info);
break;
case 4:
printf(" Doubly Linked List is n");
Display(start);
break;
case 5:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 5);
}

DNODE *InsFront(DNODE *start, int elem) {
DNODE *temp;
temp = (DNODE *) malloc(sizeof(DNODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (start);
} else {
temp->data = elem;
temp->prev = NULL;
temp->next = start;
start->prev = temp;
printf(" New Node has been inserted at Front Successfully !!");
return (temp);
}
}

DNODE *InsBefore(DNODE *start, int elem, int info) {
DNODE *temp, *t;
temp = (DNODE *) malloc(sizeof(DNODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (start);
} else {
temp->data = elem;
temp->next = NULL;
temp->prev = NULL;

if (start->data == info) /* Front Insertion */
{
temp->next = start;
start->prev = temp;
return (temp);
} else {
t = start;
while (t != NULL && t->data != info)
t = t->next;
if (t->data == info) /* Node found */
{
temp->next = t;
temp->prev = t->prev;
t->prev->next = temp;
t->prev = temp;
} else
printf(" Node not found,Invalid Info !!!");
return (start);
}
}
}

DNODE *DelNode(DNODE *start, int info) {
DNODE *t;
if (start == NULL) {
printf(" Underflow!!!");
return (start);
} else {
t = start;
if (start->data == info) /* Front Deletion */
{
start = start->next;
start->prev = NULL;
t->next = NULL;
free(t);
return (start);
} else {
while (t != NULL && t->data != info)
t = t->next;
if (t->data == info) /* node to be deleted found*/
{
t->prev->next = t->next;
t->next->prev = t->prev;
t->next = t->prev = NULL;
free(t);
} else
printf("Node not found, Invalid Info !!");
return (start);
}
}
}

void Display(DNODE *start) {
DNODE *t;
if (start == NULL)
printf("Empty Listn");
else {
t = start;
printf("Forward Traversal nn Start->");
while (t) {
printf("[%d]->", t->data);
t = t->next;
}
printf("Nulln");
}
}

Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement QUEUE operations using Linked Lists

Data structures using C,
C Program to implement QUEUE operations using Linked list. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. Here to implement queue we used the Linked lists!. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdlib.h>
typedef struct node {
int data;
struct node *link;
} NODE;

void Insert(int);
int Delete();
void Display();
NODE *front, *rear; /* Global Declarations */

main() {
/* Main Program */
int opn, elem;
front = rear = NULL;
do {
clrscr();
printf("n ### Linked List Implementation of QUEUE Operations ### nn");
printf("n Press 1-Insert, 2-Delete, 3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the Element to be Inserted ?");
scanf("%d", &elem);
Insert(elem);
break;
case 2:
elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %dn", elem);
break;
case 3:
printf("Linked List Implementation of Queue: Status:n");
Display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

void Insert(int info) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else {
temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp;
} /* First Node? */
else {
rear->link = temp;
rear = temp;
} /* Insert End */
printf(" Node has been inserted at End Successfully !!");
}
}

int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1;
} else {
t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info);
}
}

void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queuen");
else {
t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Rearn");
}
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com