Static Huffman Encoding

#include<iostream.h>

#include<stdio.h>

#include<conio.h>

struct node

{

node *left;

node *right;

node *parent;

char ch;

int wt;

int val;

};

int m,n;

node *a[30],*b[30];

void sort()

{

node *temp;

for(int i=0;i<m-1;i++)

{

for(int j=i+1;j<m;j++)

{

if(a[i]->wt>a[j]->wt)

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

}

void maketree()

{

node *ptr;

ptr=new node;

ptr->wt=a[0]->wt+a[1]->wt;

ptr->left=a[0];

ptr->right=a[1];

a[0]->parent=ptr;

a[1]->parent=ptr;

a[0]->val=0;

a[1]->val=1;

a[0]=ptr;

for(int i=1;i<m;i++)

{

a[i]=a[i+1];

}

n–;

}

/*void display(int m)

{

for(int i=0;i<m;i++)

{

cout<<“\nname”<<n[i].ch;

cout<<“\t wt”<<n[i].wt;

}

} */

int p[10],j;

void print2(int i)

{

cout<<b[i]->ch;

for(int k=j-1;k>=0;k–)

{

cout<<p[k];

}

cout<<“\n”;

}

 

void bincode()

{  node *ptr,*ptr2;

ptr=a[0];

for(int i=0;i<m;i++)

{

ptr2=b[i];

j=0;

while(ptr2!=ptr)

{

p[j]=ptr2->val;

j++;

ptr2=ptr2->parent;

}

print2(i);

}

}

void main()

{

clrscr();

cout<<“\nEnter size”;

cin>>m;

n=m;

cout<<“\nEnter tree”;

for(int i=0;i<m;i++)

{

a[i]=new node;

cout<<“Enter Name\n”;

cin>>a[i]->ch;

cout<<“Enter weight”;

cin>>a[i]->wt;

a[i]->right=NULL;

a[i]->left=NULL;

a[i]->parent=NULL;

b[i]=a[i];

}

n–;

while(n!=0)

{

sort();

maketree();

}

bincode();

//display(m);

getch();

}

 

Theory:

Coding made simple!

Coding made simple!

Its howard wolowitz urging you to code 🙂

The first 90 percent of t…

The first 90 percent of the code accounts for the first 90 percent of the development time…The remaining 10 percent of the code accounts for the other 90 percent of the development time.
–Tom Cargill

Circular Doubly Linked List

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};

struct list *node;

void create(struct list *n)
{
char ch;
node=n;
printf(“\nWant to create location(y/n):-“);
scanf(“%c”,&ch);
fflush(stdin);

while(ch!=’n’)
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf(“\Enter value:-“);
scanf(“%d”,&node->num);
fflush(stdin);
printf(“\Any more (y/n)”);
scanf(“%c”,&ch);
}

node->next=n;
n->prev=node;
}

void display(struct list *n)
{
node=n->next;

while(node!=n)
{
printf(“%d\n”,node->num);
node=node->next;
}

}

void insert(struct list *n)
{
struct list *node,*new1;
int c=1,count;
node=n;
new1=(struct list*)malloc(sizeof(struct list));
printf(“\nEnter the location where the new location will be inserted:”);
scanf(“%d”,&count);
printf(“\Enter value:-“);
scanf(“%d”,&new1->num);

do
{
if(c==count)
break;
node=node->next;
c++;
}while(node!=n);

new1->next=node->next;
node->next->prev=new1;
node->next=new1;
new1->prev=node;
}

void main()
{
struct list *start;
clrscr();
start=(struct list*)malloc(sizeof(struct list));
/* header node without any value */
create(start);
printf(“Now display the value of the circular lists:-\n”);
display(start);
insert(start);
printf(“\nAfter insertion, the list is\n”);
display(start);
getch();
}

Linked List (menu driven)

#include<iostream.h>
#include<conio.h>

struct node
{
int data;
node *next;
}

void insert();
void deleterec();
void count();

void main()
{
clrscr();
int choice;
int n;
node *first,*last,*ptr;

first=new(node);
cout<<“enter the first data”;
cin>>first->data;
first->next=NULL;
ptr=first;

cout<<“enter the number of recors”;
cin>>n;

for(int a=0;a<n-1;a++)
{
last=new(node);
cout<<“\n enter data”;
cin>>last->data;
last-next=NULL;
first->next=last;
first=last;
}

do
{
cout<<” enter 1 for insertion
enter 2 for deletion
enter 3 for counting
enter 4 for exit”;
cin>>choice;

switch(choice)
{
case 1: insert();
break;
case 2: deleterec();
break;
case 3: count();
break;

default:cout<<“enter corrct choice”;
}

}while(choice!=4)

getch();
}

void insert(node *ptr)
{
int d;
cout<<“enter the data”;
cin>>d;
node *x;

if(d<ptr->data);
{
x=new(node);
x->data=d;
x->next=ptr;
}

else
{
while(d>ptr->data)
{
ptr=ptr->next;
}
x=new(node);
x->data=d;
x->next=ptr->next;
ptr=x;
}
}

void deleterec(node *ptr)
{
node *last;
last=NULL;
int temp,dlt;
cout<<“enter data to be deleted”;
cin>>dlt;

if(dlt==ptr->data)
{
temp=ptr->data;
ptr=ptr->next;\\delete statement to be used or not
}

else
{
while(dlt>ptr->data)
{
ptr=ptr->next;
last=ptr;
}
dlt=ptr->data;
last->next=ptr->next;
}
}

void count(node *ptr)
{
int count;
while(ptr!=NULL)
{
ptr=ptr->next;
count++;
}
cout<<“the number of entries is “<<count;
}

Hello world!

Welcome to WordPress.com! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives