Search An Element in Linked List in C


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
                  {
                    int data;
                    struct node*next;
                  };
 void insert(struct node**p,int num)   /*Function for inserting an
element into a list */
 
 {
   if(*p==NULL)
     {
      (*p)=(struct node*)malloc(sizeof(struct node));
      (*p)->next=NULL;
      (*p)->data=num;
     }
   else
    {
     insert(&((*p)->next),num);
    }
 }
 
void display(struct node*p) /*Function for displaying the list*/
 {
   while(p!=NULL)
    {
     printf(“%d “,p->data);
     p=p->next;
    }
 }
 
 
void reverse(struct node**p) /*Function for reversing the list by
recursion */
 {
   struct node*q,*r,*x;
     int d;
     q=(*p);     /*stores the address of the first element */
     x=q;        /*also stores the element of the first element for
counter pourpose */
     d=q->data;  /*stores the data of the first element*/
     r=q->next;  /*stores the address of the second element in the list
*/
      free(q);   /*deletes the first element of the list*/
      if(x==NULL)
                return ;
                else
                  {
                    reverse(&(r));/*Recursive call*/
                    insert(p,d);  /*This function is put in the stack so the first
                                                     will be taken as last element for the new list */
                  }
 }
 
 void main()
 {
  clrscr();
  struct node*p=NULL;
  int n,d,i=0;
   printf(“How many…?”);
   scanf(“%d”,&n);
    while(i++!=n)
     {
      scanf(“%d”,&d);
      insert(&p,d);
     }
  display(p);
  reverse(&p);
  printf(“The reversed list is…”);
  display(p);
  getch();
 }

Heapsort algorithm Program in C


#include <stdlib.h>  
#include <stdio.h>
 
 
#define uint unsigned int
 
typedef int (*compare_func)(int, int);
 
 
void heap_sort(int This[], compare_func func_pointer, uint len)
{
  /* heap sort */
 
  uint half;
  uint parents;
 
  if (len <= 1)
    return;
 
  half = len >> 1;
  for (parents = half; parents >= 1; –parents)
  {
    int tmp;
    int level = 0;
    uint child;
 
    child = parents;
    /* bottom-up downheap */
 
    /* leaf-search for largest child path */
    while (child <= half)
    {
      ++level;
      child += child;
      if ((child < len) &&
          ((*func_pointer)(This[child], This[child – 1]) > 0))
        ++child;
    }
    /* bottom-up-search for rotation point */
    tmp = This[parents – 1];
    for (;;)
    {
      if (parents == child)
        break;
      if ((*func_pointer)(tmp, This[child – 1]) <= 0)
        break;
      child >>= 1;
      –level;
    }
    /* rotate nodes from parents to rotation point */
    for (;level > 0; –level)
    {
      This[(child >> level) – 1] =
        This[(child >> (level – 1)) – 1];
    }
    This[child – 1] = tmp;
  }
 
  –len;
  do
  {
    int tmp;
    int level = 0;
    uint child;
 
    /* move max element to back of array */
    tmp = This[len];
    This[len] = This[0];
    This[0] = tmp;
 
    child = parents = 1;
    half = len >> 1;
 
    /* bottom-up downheap */
 
    /* leaf-search for largest child path */
    while (child <= half)
    {
      ++level;
      child += child;
      if ((child < len) &&
          ((*func_pointer)(This[child], This[child – 1]) > 0))
        ++child;
    }
    /* bottom-up-search for rotation point */
    for (;;)
    {
      if (parents == child)
        break;
      if ((*func_pointer)(tmp, This[child – 1]) <= 0)
        break;
      child >>= 1;
      –level;
    }
    /* rotate nodes from parents to rotation point */
    for (;level > 0; –level)
    {
      This[(child >> level) – 1] =
        This[(child >> (level – 1)) – 1];
    }
    This[child – 1] = tmp;
  } while (–len >= 1);
}
 
 
#define ARRAY_SIZE 250000
 
int my_array[ARRAY_SIZE];
 
void init()
{
  int indx;
 
  for (indx=0; indx < ARRAY_SIZE; ++indx)
  {
    my_array[indx] = rand();
  }
}
 
int cmpfun(int a, int b)
{
  if (a > b)
    return 1;
  else if (a < b)
    return -1;
  else
    return 0;
}
 
int main()
{
  int indx;
 
  init();
 
  heap_sort(my_array, cmpfun, ARRAY_SIZE);
 
  for (indx=1; indx < ARRAY_SIZE; ++indx)
  {
    if (my_array[indx – 1] > my_array[indx])
    {
      printf(“bad sort\n”);
      return(1);
    }
  }
 
  return(0);
}

CPU scheduling (Round Robin) Program in C


#include<stdio.h>
#include<conio.h>
main()
{
  int st[10],bt[10],wt[10],tat[10],n,tq;
  int i,count=0,swt=0,stat=0,temp,sq=0;
  float awt=0.0,atat=0.0;
  clrscr();
  printf(“Enter number of processes:”);
  scanf(“%d”,&n);
  printf(“Enter burst time for sequences:”);
  for(i=0;i<=n;i++)
   {
     scanf(“%d”,&bt[i]);
     st[i]=bt[i];
   }
   printf(“Enter time quantum:”);
   scanf(“%d”,&tq);
   while(1)
   {
     for(i=0,count=0;i<=n;i++)
     {
       temp=tq;
       if(st[i]==0)
       {
  count++;
  continue;
       }
       if(st[i]=>tq)
 st[i]=st[i]-tq;
       else
 if(st[i]>=0)
 {
   temp=st[i];
   st[i]=0;
 }
 sq=sq+temp;
 tat[i]=sq;
     }
     if(n==count)
     break;
   }
   for(i=0;i<=n;i++)
   {
    wt[i]=tat[i]-bt[i];
    swt=swt+wt[i];
    stat=stat+tat[i];
   }
   awt=(float)swt/n;
   atat=(float)stat/n;
   printf(“Process_no Burst time Wait time Turn around time
“);
   for(i=0;i<=n;i++)
    printf(“%d  %d  %d  %d
“,i+1,bt[i],wt[i],tat[i]);
    printf(“Avg wait time is %f
Avg turn around time is %f”,awt,atat);
    getch();
}

Tree Operations – INSERTION ,INORDER , PREORDER , POSTORDER TRAVERSAL in C | Data structure example on Tree Operation in C | C Assignments in Tree Sorting




# include< stdio.h>
# include < conio.h>
# include < malloc.h>

struct node
{
struct node *left;
int data;
struct node *right;
} ;

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();

printf("
Enter the number of terms you want to add to the tree.
");
scanf("%d",&will);

/* Getting Input */
for(i=0;i
{
printf("
Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("

INORDER TRAVERSAL

");
inorder(ptr);
getch();
printf("

PREORDER TRAVERSAL

");
preorder(ptr);
getch();
printf("

POSTORDER TRAVERSAL

");
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("
Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("

REPEATED ENTRY ERROR
VALUE REJECTED

");
return;
}
if(num<(*p)->data)
{
printf("
Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("
Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("
Data :%d",p->data);
inorder(p->right);
}
else
return;
}


void preorder(struct node *p)
{
if(p!=NULL)
{
printf("
Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("
Data :%d",p->data);
}
else
return;
}

C Program to implement topological sort.


#include<stdio.h>
#define MAX 200
int n,adj[MAX][MAX];
int front = -1,rear = -1,queue[MAX];
void main()
{
 int i,j = 0,k;
 int topsort[MAX],indeg[MAX];
 create_graph();
 printf(“The adjacency matrix is:\n”);
 display();
 for(i=1;i<+n;i++)
 {
  indeg[i]=indegree(i);
  if(indeg[i]==0)
   insert_queue(i);
 }
 while(front<=rear)
 {
  k=delete_queue();
  topsort[j++]=k;
  for(i=1;i<=n;i++)
  {
   if(adj[k][i]==1)
   {
    adj[k][i]=0;
    indeg[i]=indeg[i]-1;
    if(indeg[i]==0)
     insert_queue(i);
   }
  }
 }
 printf("Nodes after topological sorting are:\n");
 for(i=0;i<=n;i++)
  printf("%d",topsort[i]);
 printf("\n");
}
create_graph()
{
 int i,max_edges,origin,destin;
 printf("\n Enter number of vertices:");
 scamf("%d",&n);
 max_edges = n * (n - 1);
 for(i = 1;i <= max_edges;i++)
 {
  printf("\n Enter edge %d (00 to quit):",i);
  scanf("%d%d",&origin,&destin);
  if((origin == 0) && (destin == 0))
  {
   printf("Invalid edge!!\n");
   i–;
  }
  else
   adj[origin][destin] = 1;
 }return;
}
display()
{
 int i,j;
 for(i = 0;i <= n;i++)
 {
  for(j = 1;jrear)
  {
   printf(“Queue Underflow”);
   return;
  }
  else
  {
   del_item = queue[front];
   front = front + 1;
   return del_item;
  }
 }
 int indegree(int node)
 {
  int i,in_deg = 0;
  for(i = 1;i <= n;i++)
   if(adj[i][node] == 1)
    in_deg++;
  returnin_deg;
 }

Selection sort algorithm implementation in c


#include<stdio.h>
 
main()
{
int array[100], n, c, d, swap;
 
printf("Enter number of elements\n");
scanf("%d", &n);
 
printf("Enter %d integers\n", n);
 
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
 
for ( c = 0 ; c < ( n - 1) ; c++ )
{
for ( d = ( c + 1 ) ; d <= ( n - 1 ) ; d++ )
{
if ( array[c] > array[d] )
{
swap = array[c];
array[c] = array[d];
array[d] = swap;
}
}
}
 
printf("Sorted list in ascending order:\n");
 
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
 
return 0;
}