Friday, 26 June 2009

Priority Queue

Description:-
This is a complete program of priority queue using single linked list.
Priority queue is a queue where value is inserted sequentially based on its priority.
So here the structure contain two int type fields named "priority" & "info" & there is a link which hold the address of the next structure type node.

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropriate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.

Source code:-


/* Program of priority queue using linked list*/
#include
#include
#include
struct node
{
int priority;
int info;
struct node *link;
}*front = NULL;

main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
struct node *tmp,*q;
int added_item,item_priority;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the item value to be added in the queue : ");
scanf("%d",&added_item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
tmp->info = added_item;
tmp->priority = item_priority;
/*Queue is empty or item to be added has priority more than first item*/
if( front == NULL || item_priority <>priority )
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while( q->link != NULL && q->link->priority <= item_priority ) q=q->link;
tmp->link = q->link;
q->link = tmp;
}/*End of else*/
}/*End of insert()*/

del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp = front;
printf("Deleted item is %d\n",tmp->info);
front = front->link;
free(tmp);
}
}/*End of del()*/

display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}/*End of else */
}/*End of display() */

Tuesday, 16 June 2009

De_queue

Description:-
One of the famous program in the field of data structure is De_queue. In this program a simple de_queue is constructed using an array.
Normally in several de_queue programmes a lot of functions involved to manage the input & output operations but in my de_queue all the operations are performed with switch case command.
Another version of De_queue using single linked list will be introduced shortly.

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.



Source code:-
/*Complete program of de_queue with array*/
#include
#include
#include
#define MAX 5
int arr[MAX];
static int rear=-1;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Push into the queue.");
printf("\n2.Pop from queue.");
printf("\n3.Display the queue.");
printf("\n4.Exit.");
printf("\nEnter your choice=>");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice!");
break;
}
}
}
void push()
{
int ch1,ch2,i,val;
if(rear!=MAX)
{
printf("\nEnter the value=>");
scanf("%d",&val);
printf("\n1.Input restricted.");
printf("\n2.Output restricted.");
printf("\nEnter your choice=>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
arr[++rear]=val;
break;
case 2:
printf("\n1.Input into first.");
printf("\n2.Input into last.");
printf("\nEnter your choice=>");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
for(i=rear;i>=0;i--)
arr[i+1]=arr[i];
rear++;
arr[0]=val;
break;
case 2:
arr[++rear]=val;
break;
default:
printf("\nWrong choice!");
break;
}
break;
default:
printf("\nWrong choice!");
break;
}
}
else
printf("\nQueue overflow!");
}
void display()
{
int i;
if(rear==-1)
printf("\nNo element in the queue!");
else
{
printf("\nQueue element=>\n");
for(i=0;i<=rear;i++)
printf("%d ",arr[i]);
}
}
void pop()
{
int ch1,ch2,i;
if(rear!=-1)
{
printf("\n1.Input restricted.");
printf("\n2.Output restricted.");
printf("\nEnter your choice=>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\n1.Pop from first.");
printf("\n2.Pop from last.");
printf("\nEnter your choice=>");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
for(i=1;i<=rear;i++)
arr[i-1]=arr[i];
rear--;
return;
case 2:
rear--;
return;
default:
printf("\nWrong choice!");
break;
}
case 2:
rear--;
return;
default:
printf("\nWrong choice!");
break;
}
}
else
printf("\nNo element to pop!");
}

Sunday, 14 June 2009

Single Linked List

Description:-
If you are a student of a computer science then u have to do the program of "Single Link List" in the "DATA STRUCTURE" subject. Then this program helps u a lot.
This program has a complete program of single link list with reversing the list(physically).

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.



Source code:-

/* THIS IS THE COMPLETE PROGRAM OF "SINGLE LINKED LIST" */
#include
#include
#include
#include
struct node
{
int data;
struct node * link;
} *start;
void create_list(int);
void showlist();
void add_beg();
void insert(int);
void del();
void del1();
void reverse();
int count();
void main()
{
int ch,i,n,num,c;
clrscr();
start=NULL;
while(1)
{
printf("\n\n\n");
printf("\n1. Create list.");
printf("\n2. Show list.");
printf("\n3. Add at begining.");
printf("\n4. Count how many node?");
printf("\n5. Insert any specified position.");
printf("\n6. Delete.");
printf("\n7. Reverse the list.");
printf("\n8. Delete any node by value.");
printf("\n9. Exit.\n\n");
printf("\nEnter your choice=>");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow many number want to create=>");
scanf("%d",&n);
for(i=0;i {
printf("Enter the number=>");
scanf("%d",&num);
create_list(num);
}
break;
case 2:
showlist();
break;
case 3:
add_beg();
break;
case 4:
c=count();
printf("\nNo of node=>%d",c);
break;
case 5:
printf("\nIn which position=>");
scanf("%d",&num);
c=count();
if(num>c)
printf("Specified position is not possible!");
else if(num==1)
add_beg();
else
insert(num);
break;
case 6:
del();
break;
case 7:
reverse();
break;
case 8:
del1();
break;
case 9:
exit(0);

}
}
}
void create_list(int val)
{
struct node *q,*tmp;
tmp=(node *)malloc(sizeof(struct node));
tmp->data=val;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void showlist()
{
struct node *q;
q=start;
if(start==NULL)
{
printf("List is empty");
return;
}
else
{
printf("\nCreated list=>\n");
while(q!=NULL)
{
printf("%d ",q->data);
q=q->link;
}
printf("\n");
}
}
void add_beg()
{
struct node *tmp;
int num;
printf("\nEnter the number=>");
scanf("%d",&num);
tmp=(node *)malloc(sizeof(struct node));
tmp->data=num;
tmp->link=start;
start=tmp;
}
void insert(int val)
{
struct node *q,*tmp;
int i=1,num;
printf("Enter the number=>");
scanf("%d",&num);
q=start;
while(i!=val)
{
q=q->link;
i++;
}
tmp=(node *)malloc(sizeof(struct node));
tmp->data=num;
tmp->link=q;
q=start;
i=1;
while(i!=val-1)
{
q=q->link;
i++;
}
q->link=tmp;
}
int count()
{
struct node *q;
int cnt=0;
q=start;
if (start==NULL)
printf("\nThere is no node exist");
else
{
while(q!=NULL)
{
cnt++;
q=q->link;
}
}
return cnt;
}
void del()
{
int pos,i=1;
struct node *q,*tmp;
q=start;
printf("\nWhich position=>");
scanf("%d",&pos);
if(pos>count())
printf("\nNode does not exist");
else if(pos==1)
{
while(i!=pos+1)
{
q=q->link;
i++;
}
start=q;
}
else if(pos==count())
{
while(i!=pos-1)
{
q=q->link;
i++;
}
q->link=NULL;
}
else
{
while(i!=pos+1)
{
q=q->link;
i++;
}
tmp=q;
q=start;
i=1;
while(i!=pos-1)
{
q=q->link;
i++;
}
q->link=tmp;
}
}
void reverse()
{
struct node *q,*x,*p;
printf("\nReversed the created list\n");
q=start;
x=NULL;
p=start;
while(x!=p)
{
while(q->link!=x)
q=q->link;
if(p->link!=q->link)
{
p->data=p->data*q->data;
q->data=p->data/q->data;
p->data=p->data/q->data;
x=q;
p=p->link;
q=start;
}
else
return;
}
}
void del1()
{
struct node *q,*r;
int c=1,val;
q=start;
r=start;
printf("\nEnter the value=>");
scanf("%d",&val);
while(q->link!=NULL&&q->data!=val)
{
q=q->link;
c++;
}
if(q->data==val)
{
if(c==1)
{
q=q->link;
start=q;
}
else if(c==count())
{
q=start;
c--;
while(c!=1)
{
q=q->link;
c--;
}
q->link=NULL;
}
else
{
q=q->link;
c--;
while(c!=1)
{
r=r->link;
c--;
}
r->link=q;
}
}
else
printf("\nGiven value does not exist!");
}

Prime factor

Description:-
In our mathematics it is very important to find out the factors(prime) of a number.
For example:
Given number=12
Factors= 2,2,3
Sometimes this task will be difficult if there is a number greater than 1000 or very huge number, then you need very long time to find the factors of that given number.
Through this program you can quickly find out the prime factors of the given number.
You need to give that number as input automatically factors are coming out as a result.

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.


Source code:-

/*This program helps to find the factor of a given number*/
#include
void main()
{
long i,n;
clrscr();
printf("Enter a number=>");
scanf("%ld",&n);
for(i=2;i<=n;i++)
{
while(n%i==0)
{
n=n/i;
printf("%ld ",i);
}
}
}

Saturday, 9 May 2009

Arrange the name in alphabetical order

Description:-
If u want to arrange ur name & others name in the alphabetical order then this is the program which will help u to do that job very well.

For example if u give the following names in the order:-
a)santanu
b)arnab
c)biswajit
d)sourav
e)joy
f)agni
The output will be:-

a)agni
b)arnab
c)biswajit
d)joy
e)santanu
f)sourav

-:Screen shots:-




Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.


Source code:-

#include
#include
#include
void reorder(int n,char x[][12]);
main()
{
int i,n=0;
char x[100][12];
printf("Enter each name in a single line\n\n");
printf("Type 'end' when finished\n\n'");
do
{
printf("string %d: ",n+1);
scanf("%s",x[n]);
}
while(strcmp(x[n++],"end"));
n--;
reorder(n,x);
for(i=0;i printf("\nstring %d: %s",i+1,x[i]);
}
void reorder(int n,char x[][12])
{
char temp[12];
int i,item;
for(item=0;item {
for(i=item+1;i {
if(strcmp(x[item],x[i])>0)
{
strcpy(temp,x[item]);
strcpy(x[item],x[i]);
strcpy(x[i],temp);
}
}
}
return;
}

Friday, 8 May 2009

Binary

Description:-
Enter the decimal number & this program gives u the binary equivalent number of that given decimal number.
For example:-
Enter the decimal number=2654
Binary equivalent=>
101001011110

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.


Source code:-

#include
void main()
{
int x[100],i=0;
long n;
printf("Enter the number=>");
scanf("%ld",&n);
printf("\nBinary equivalent=>\n\n");
while(n!=0)
{
x[i]=n%2;
i++;
n=n/2;
}
for(n=(i-1);n>=0;n--)
printf(" %d ",x[n]);
}



Get the abbreviated form

Description:-
If u tired to make ur name in the abbreviated form here is the simple solution of this problem. Through this program u can convert any English name into the abbreviated format. For example if ur name is "Santanu biswas" then the program returns "S biswas"; also if your name is "Sunil kumar ghosh" then the program returns "S k ghosh".

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.



Source code:-

#include
#include
void main()
{
int i,n,len;
char name[50];
printf("Enter the name(Firstname Middlename Lastname)=>");
gets(name);
len=strlen(name);
n=len-1;
while(name[n]!=' ')
n--;
for(i=0;i {
if(i==0)
printf("%c ",name[i]);
else if(name[i]==' ')
printf("%c ",name[i+1]);
}
for(i=n+1;i printf("%c",name[i]);
}