What are the settings I require so I can debug line -by line?
Or else ,I can identify lines of code where run time errors are occuring..
I want to view exact line number of error in this code/

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct node 
{struct node * left,*right;
int freq;
char c;
};
typedef struct node node;
int front=1;
node * q[256];
char * code[128]={0},array[1024];
node * new (int freq,char c,node * a ,node * b)
{
 	 node * n=(node *)malloc(sizeof(node ));
 	 if(freq)
 	 {n->c=c;
 	 n->freq=freq;}
 	 else
 	 {n->left=a;n->right=b;
 	 n->freq=a->freq+b->freq;
	 }
 	 return n;
 	 
}
void insert(node * n)
{int i,j;
i=front++;
j=i/2;
while((j=i/2))
{if(q[j]->freq<=n->freq)
break;
q[i]=q[j];i=j;
}
q[i]=n;
}
node * del()

{	
	 int i=1,l=2;node * n=q[1];	
 	 if(front<2)return 0;
 	 front--;
 	 	printf("hello");
	while ((l=i*2) < front) {
		if (l + 1 < front && q[l + 1]->freq < q[l]->freq) l++;
		q[i] = q[l], i = l;
	}
	q[i] = q[front];
	return n;
}
void create(node * n,char * s,int len)

{	
	static char * out=array;
	if(n->c)
	{
	 s[len]=0;
	 strcpy(out,s);
	 code[n->c]=out;
	 out+=len+1;
	 return;		
	 		
    }
    s[len]='0';create(n->left,s,len+1);
    s[len]='1';create(n->right,s,len+1);
 	 
}

void init(char * s )
{
 	 int i ,freq[128]={0};
 	 char c[20];node  *n,*n1,*n2,*n3;
 	 while(*s!='\0')
 	 {
	  freq[(int )*s++]++;		
     }
     for(i=0;i<128;i++)
     {
		   if(freq[i]){n=new(freq[i],i,0,0);insert(n);}
	 }
	 while(front>2)
	 {
   	       n2=del();
   	       n3=del();
   	       n1=new(0,0,n2,n3);
	 }
	 create(q[1],c,0);
}

void encode(char * s,char * out)
{
 	while(*s!='\0')
	 {
	  	 strcpy(out,code[*s]); 
		 out+=strlen(code[*s++]);	 
	 }
}





int main()
{
char * s="hello",array[1024];int i;
init(s);
	for (i = 0; i < 128; i++)
		if (code[i]) printf("'%c': %s\n", i, code[i]);
 
	encode(s, array);
	printf("encoded: %s\n", array);
 
 
getch();
return 0; 	
}

Edited 5 Years Ago by swissknife007: n/a

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

#define MAXBITS 32
#define MAXNODES 50
#define MAXSYMBS 50

void insert(int,int);
int delete();

struct hlist
{
       int pos;
       int hfreq;
       struct hlist *next;
};

struct nodetype
{
   int freq;
   int father;
   int isleft;
};

struct codetype
{
   int bits[MAXBITS];
   int startpos;
};

typedef struct hlist hnode;
hnode *root=NULL,*temp;

int main()
{
    int n,i,k,p1,p2,p,root1;
    struct codetype cd,code[MAXSYMBS];
    char symb,alpha[MAXSYMBS];
    struct nodetype node[MAXNODES];
    for(i=0;i<MAXSYMBS;i++)
    alpha[i]=' ';
    root=0;
    printf("enter n:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {               
                    
                    printf("enter freq:");
                    scanf("%d",&node[i].freq);
                    printf("enter character:");
                    scanf("%s",&symb);
                    alpha[i]=symb;
                    insert(i,node[i].freq);
    }
    
    for(p=n;p<(2*n-1);p++)
    {
                          p1=delete();
                          p2=delete();
                          node[p].freq = node[p1].freq + node[p2].freq;
                          node[p1].father=p;
                          node[p2].father=p;
                          node[p1].isleft=1;
                          node[p2].isleft=0;
                          insert(p,node[p].freq);
    }
    root1=delete();
    for(i=0;i<n;i++){
	 cd.startpos = MAXBITS;
	 p=i;
	 while(p!=root1){
		--cd.startpos ;
		if(node[p].isleft)
		   cd.bits[cd.startpos] =0;
		else
		   cd.bits[cd.startpos] =1;
		p =node[p].father;
	 }
	 for(k=cd.startpos;k<MAXBITS;k++)
		code[i].bits[k]=cd.bits[k];
	 code[i].startpos =cd.startpos;
  }
  for(i=0;i<n;i++){
	 printf("\n%c  %d",alpha[i],node[i].freq);
	 for(k=code[i].startpos;k<MAXBITS;k++)
		printf(" %d",code[i].bits[k]);
	 printf("\n");
	 }
	 getch();
	 return(0);
  }
  
    
    
    


void insert(int pos,int freq)
{
     hnode *new1=malloc(sizeof(hnode));
     new1->next=NULL;
     new1->pos=pos;
     new1->hfreq=freq;
     if(root==NULL)
     {
                   root=new1;
     }
     else if(root->hfreq>=freq)
     {
          new1->next=root;
          root=new1;    
     }
     else
     {
         hnode*save=root;
         temp=root->next;
         while(temp!=NULL)
         {
            if(temp->hfreq>=freq)
            {
                  new1->next=temp;
                  save->next=new1;
                  break;              
            }   
            save=temp;
            temp=temp->next;           
         }
         if(temp==NULL)
         {
                       save->next=new1;
         }
         
     }
 }

int delete()
{
    int p;
    p=root->pos;
    root=root->next;
    return(p);
}

Run it and use it. it is completely working huffman's code... check it out. may be it can help u .. :-)

This article has been dead for over six months. Start a new discussion instead.