what is Priority Queue?

A priority queue is a collection of elements where the elements are stored according to their priority levels. The order in which the elements should get added or removed is decided by the priority of the element. Following rules are applied to maintain a priority queue.

(a) The element with a higher priority is processed before any element of lower priority.

(b) If there are elements with the same priority, then the element added first in the queue would get processed.

Priority queues are used for implementing job scheduling by the operating system where jobs with higher priorities are to be processed first. Another application of priority queues is simulation systems where priority corresponds to event limes.

Related Links :

Whats is an array Implementation Of A Priority Queue

Like stacks and queues even a priority queue can be represented using an array. However, if an array is used to store elements of a priority queue, then insertion of elements to the queue would be easy, but deletion of elements would be difficult This is because while inserting elements in the priority queue they are not inserted in an order. As a result, deleting an element with the highest priority would require examining the entire array to search for such an element- Moreover, an element in a queue can be deleted from the front end only.

Related Links :

What is Circular Queues?

The queue that we implemented using an array suffers from one limitation. In that implementation there is a possibility that the queue is reported as full (since rear has reached the end of the array), even though in actuality there might be empty slots at the beginning of the queue. To overcome this limitation we can implement the queue as a circular queue. Here as we go on adding elements to the queue and reach the end of the array, the next element is stored in the first slot of the array (provided it is free). More clearly, suppose an array arr of n elements is used to implement a circular queue. Now if we go on adding elements to the queue we may reach arr[*-l|. We can not add any more elements to the queue since we have reached the end of the array. Instead of reporting the queue as full, if some elements in the queue have been deleted then there might be empty slots at the beginning of the queue. In such a case these slots would be filled by new elements being added to the queue. In short just because we have reached the end of the array the queue would not be reported as full. The queue would be reported as full only when all the slots in the array stand occupied. Figure 7-6 shows the pictorial representation of a circular queue.

Related Links :

Representation Of A Queue As An Array

Queue, being a linear data structure can be represented in various ways such as arrays and linked lists. Representing a queue as an array would have the same problem that we discussed in case of stacks. An array is a data structure that can store a fixed number of elements. The size of an array should be fixed before using it Queue, on the other hand keeps on changing as we remove elements from the front end or add new elements at the rear end. Declaring an array with a maximum size would solve this problem. The maximum size should be large enough for a queue to expand or shrink.

Related Links :

Use of Function padd() in c ?

This function adds the node to the resultant list (i.e. p3) in the descending order of the exponents of the polynomial.

To begin with we initialize a structure pointer temp with a value p, where p is pointer to the first node of the resultant list. Its value would be NULL when called for the first time.

Initially a condition is checked whether the resultant list is empty or not. If it is so then we need to add the first node. Hence memory is allocated for the new node and the value of coefficient and exponent is assigned to the coefficient and exponent part of the new node, temp which holds a NULL value to begin with is stored in the link part of the resultant new node.

When padd( ) is called to add the second node we need to compare the exponent value of the new node with that of the first node. If the exponent value of the new node is greater than the exponent value of the first node in the resultant list, then the new node is made the first node.

If both the above two conditions are false then the resultant list is traversed for searching the proper position where the new node is to be inserted. If the exponent of the same order already exists .

Related Links :

Whats is Function d_addatbeg() in C ?

The d_addatbeg( ) function adds a node at the beginning of the existing list. This function too accepts parameter dura, which holds an integer to be added to the Jist.

Memory is allocated for the new node whose address is stored in q. Then num is stored in the data part of the new node. A NULL value is stored in prev part of the new node as this is going to be the first node of the list. The next pari of this new node should contain the address of the first node of the list. This is done through the statement

q -> next = p;

Now what remains to be done is to store the address of this new node into the prev part of the first node and make this new node the first node in the list. This is done through the statements

Related Links :

What is Doubly Linked Lists?

In the lists that we have used so far each node provides information about where is the next node in the list. It has no knowledge about where the previous node lies in memory. If we are at say the 15th node in the list, then to reach the 14lh node we have lo Traverse the lis! right from the first node. To avoid this we can store in each node not only the address of ne*( node but also the address of the previous node in the linked list. This arrangement is often known as a 'Doubly Linked List'.

Related Links :

how to create & Storage of two-dimensional array in memory in C

Two or More dimensional arrays works on table structure, as table stores values it stores values in table like structure (rows & Column).

• A two dimensional array has two subscripts.
(More dimension More Subscripts)
• First subscript is for row and second for column.
• The syntax for declaration of two dimensional array is
data_type array[row_size][column_size];

• Consider the example int pop[5][5];

Here aa is an integer array of size 5x5
i.e. total 25 elements.

• The array elements are represented using the subscript.
• While storing in memory, each dimension of the array is indexed from zero to it’s maximum size-1.
• The first subscript indicate row and second subscript indicates column.
• For eg. Consider an integer array of size 2x3 as

int X[2][3]={10,20,30,40,50,60};


The array b is represented in memory as

Col.0 Col.1 Col.2
row0 110 120 130
row1 140 150 160

Where,
x[0][0]=110
x[0][1]=120
x[0][2]=130
x[1][0]=140
x[1][1]=150
x[1][2]=160

Related Links :

What is the difference between array and ordinary variable in C?

Array is the set of an multiple values where as variable can store single value at a time.

• The difference between the definition of array and ordinary variable is the, array is always declared, initialized, and accessed using subscript whereas ordinary variable do not have any subscript.


• The syntax for ordinary variable definition is data_type v1, v2, ….;

• And the syntax for array variable is data_type v1[N1],v2[N2],…; where v1,v2 are name of variable and N1, N2 are the integer constants indicating the maximum size of array.


Related Links :

Error handling during input/output operations in C

• During input/output, Errors in C occurs due to missing of Semicolon (;),Comma(,) Quotation marks(“), Incompatible format specifier (%).

• Other than these, the commonly occurring errors during input/output are explained below with their cause.

Related Links :

What is Trigraph characters is C?

Trigraph characters is C

• The C character set is contained within seven-bit ASCII.
• In order to write programs in the reduced set, corresponding single characters replace the occurrences of the trigraph characters.
• The replacement occurs before any processing.

Trigraph charager replaced by
??= #
??/ \
??’ ^
??( [
??) ]
??! |
??< {
??> }
??- ~

Related Links :

What is Escape sequence & its purpose & characteristics in C

• The escape sequence characters are also called as backslash character constants.
• These are used for formatting the output.
• The escape sequence characters are as follows :

\a audible bell
\b back space
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ back slash
\0 null character
\’ single quote
\” double quote
\000 octar number
\xhh hexadecimal number

Characteristics

1) Although it consists of two characters, it represents single character.
2) Every combination starts with back slash(\)
3) They are non-printing characters.
4) It can also be expressed in terms of octal digits or hexadecimal sequence.
5) Each escape sequence has unique ASCII value.
6) Escape sequence in character constants and string literals are replaced by their equivalent and then adjacent string literals are concatenated.

Related Links :

Rules for constructing (Declaring) variable name in C

• Variable name may be a combination of alphabets, digits or underscores.
• Sometimes, compilers in which case its length should not exceed 8 characters impose an additional constraint on the number of characters in the name.
• First character must b e alphabet or an underscore.
• No. comma or blank space is allowed.
• Among the special symbols, only underscore can be used in variable name.
• No word, having a reserved meaning in C can be used for variable name.

Related Links :

Program To input a number from user and calculate its factorial using recursion.



To input a number and calculate its factorial using recursion.

#include
#include
long int fact(int);

main()
{
int i,n;
long int f;
clrscr();
printf("Input a number : ");
scanf("%d",&n);
f=fact(n);
printf("\nFactorial of %d is %ld",n,f);
}
long int fact(int m)
{
long int f;
if(m<=1)
return(1);
else
f=m*fact(m-1);
return(f);
}

Related Links :

Program to calculate sum of numbers 1...n using recursion.



To calculate sum of numbers 1...n using recursion.

#include
#include
int sum(int);

main()
{
int i,n;
int s;
clrscr();
printf("Input a number : ");
scanf("%d",&n);
s=sum(n);
printf("\nsum of number 1...%d is %d",n,s);
}
int sum(int m)
{
int s;
if(m==1)
return(1);
else
s=m+sum(m-1);
return(s);
}

Related Links :

Program to find largest element and its position in an array using pointer.



To find largest element and is position in an array using pointer.

#include
#include

main()
{
int a[20],i,n,l,p=0;
int *b;
clrscr();
printf("\nInput array size ");
scanf("%d",&n);
printf("\Input %d array elements\n",n);
for(i=0;iscanf("%d",&a[i]);
b=a;
l=0;
printf("\nEntered array is \n");
for(i=0;i{
printf("\n%d",*b);
if(*b>1)
{
l=*b;
p=i+1;
}
}
printf("\nLargest element is %d at position %d",l,p);
}

Related Links :

Program to Convert a string to uppercase using pointer.



Convert a string to uppercase using pointer.

#include
#include

main()
{
char a[20];
char *p;
int i;
p=a;
clrscr();
printf("\nInput a string ");
gets(a);
puts("\nEntered string is ");
puts(a);
printf("\nEntered string in UPPERCASE is");
for(;*p!='\0';*p++)
{
if(*p>='a' && *p<='z')
*p-=32;
}
puts(a);
}

Related Links :

Program to compare two strings using pointer without strcmp function.



/*To compare two strings using pointer.*/

#include
#include

main()
{
char a[10],b[10];
char *p,*q;
int i,j,status=0;
p=a;
q=b;
clrscr();
printf("\nInput two strings\n");
gets(a);
gets(b);
for(;*p!='\0' || *q!='\0';*p++,*q++)
if(*p!=*q)
{
printf("\nStrings are different");
status=1;
break;
}
if(status==0)
printf("\nStrings are same");
}

Related Links :

Program to combine two strings using pointer & add them.



#include
#include

main()
{
char a[10],b[10],c[20];
char *p,*q;
int i,j;
p=a;
q=b;
clrscr();
printf("\nInput two strings ");
gets(a);
gets(b);
for(i=0;*p!='\0';c[i]=*p++,i++);
for(j=0;*q!='\0';c[i]=*q++,j++,i++);
c[i]='\0';
puts("Entered strings are ");
puts(a);
puts(b);
printf("\nAfter combining ");
puts(c);
}

Related Links :

Program to Copy one string to another using pointer.



/*Program to Copy one string to another using pointer.*/

#include
#include

main()
{
char a[80],b[80],*pa,*pb;
int i=0;
clrscr();
printf("Given first string ");
scanf("%s",a);
pa=&a[0];
pb=&b[0];
while(*pa!='\0')
{
*pb=*pa;
pa++;
pb++;
}
*pb='\0';
printf("\nCopied string is");
puts(b);
}

Related Links :

Program to create a data file and count the vowels in the file.



/*To create a data file and count the vowels in the file.*/

#include

main()
{
FILE *fp;
char ch; int v=0; clrscr();
fb=fopen("text.dat","w");
printf("\nInput text to write into file \n");
while((ch=getc(stdin))!+EOF)
{
putc(ch,fp);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
v++;
}
}
printf("\nTotal vowels are %d",v);
fclose(fp);
}

Related Links :

Program to To input records of students then write them into file, read from file and print onto Screen Monitor .(Output)




To input students records, write into file, read from file and print onto monitor.


#include
#include
FILE *fp;

sturct student
{
int a,r;
char n[10];
float p;
}s;

main()
{
int response;
clrscr();
fp=fopen("file1.dat","w+");
while(1)
{
printf("\nInput data from keyboard to write to file\n");
printf("input name ");
scanf("%s",s.n);
printf("\nInput roll no.,age and percentage ");
scanf("%d%d%f",&s.r,&s.a,&s.p);
fprintf(fp,"%s%d%d%f",s.n,s.r,s.a,s.p);
printf("\nAnother record 1:yes, 0:no ");
scanf("%d",&response);
if(response==0)
break;
}

rewind(fp);
printf("\nDate storeed in file is\n");
printf("\nName\tRollNo.Age\tPer.\n");
while(!feof(fp))
{
scanf(fp,"%s%d%d%f",s.n,&s.r,&s.a,&s.p);
printf("\n%s\t%d\t%d\t%f",s.n,s.r,s.a,s.p);
}
fclose(fp);
}

Related Links :

Program to Count number of characters in existing file.



/*Count number of characters in existing file.*/

#include
#include
FILE *fp;
main()
{
char ch;
fp=fopen("myfile","r");
clrscr();
fseek(fp,0L,2);
ftell(fp);
printf("\nNumber of character in specified file are %d",ftell(fp));
fclose(fp);
}

Related Links :

program to search the elements through Binary search ( Binear search)


//program to search the elements through Binear search
#include
#include
class binary
{
private:
int data[25],item,loc,r;
public:
void getdata();
void search();
void sort();
};
void binary::getdata()
{
cout< cin>>r;
cout< for(int i=0;i {
cin>>data[i];
}
}
void binary::sort()
{
int k,ptr,temp;
cout< for(k=0;k {
ptr=0;
while(ptr<=r-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr];
data[ptr]=data[ptr+1];
data[ptr+1]=temp;
}
ptr++;
}
}
for(int i=0;i {
cout< }
}
void binary::search()
{
cout< cin>>item;
int beg=0,end=r;
int mid=((beg+end)/2);
while(beg<=end && data[mid]!=item)
{
if (item {
end=mid-1;
}
else
{
beg=mid+1;
}
mid=((beg+end)/2);
}
if (data[mid]==item)
{
loc=mid+1;
cout< }
else
{
loc=0;
cout< }
}
void main()
{
clrscr();
binary b;
b.getdata();
b.sort();
b.search();
getch();
}





****************Output*****************


Enter the range of an array:5


Enter the elements of an array:12 22 2 1 4


Sorting order of an arry is:1 2 4 12 22

Enter the element you want to search:4


3 Is the position of the 4

****************************************



Enter the range of an array:7


Enter the elements of an array:1 0 9 6 2 3 5



Sorting order of an arry is:0 1 2 3 5 6 9



Enter the element you want to search:13



13 is not present in an array.


Related Links :

Program to find & replace any character from the input text.


//Program to find & replace any character from the input text

#include
# include
char c1,c2,a[80];
void main()
{
clrscr();
find_rep();
getch();
}


void find_rep(void)
/* Function to find & replace any text */
{
char c1,c2;
char a[80];
int i,j,k;
printf("Enter a line of text below:-");
printf("Press Enter after line..");
printf("You Have Entred:- ");
gets(a);
printf("Enter the replaceable & replacing letter respectively:- ");
scanf("%c %c %c",&c1,' ',&c2); //i have given space & 2nd %c

for (j=0;j<80;j++)
{
if (a[j]==c1)
a[j]=c2;
}
puts(a);
printf("Here all %c are replaced by %c.", c1,c2);
return;
}

Related Links :

program to sort the elements through Bubble Sort Method in C++



//Write a program to sort the elements through Bubble Sort
#include
#include
class bubble
{
private:
int r,data[25],i,k,ptr;
public:
void getdata();
void sort();
void showdata();
};
void bubble::getdata()
{
cout< cin>>r;
cout< for(i=0;i {
cin>>data[i];
}
}
void bubble::sort()
{
int temp;
for(k=0;k {
ptr=0;
while(ptr<=r-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr];
data[ptr]=data[ptr+1];
data[ptr+1]=temp;

}
ptr++;
}

}
}
void bubble::showdata()
{
cout< for(int i=0;i {
cout< }
}
void main()
{
clrscr();
bubble b;
b.getdata();
b.sort();
b.showdata();
getch();

}





****************Output***********************

Enter the range of an array:5


Enter the elements of an array:5 4 3 2 1


Sorting order is :1 2 3 4 5

Related Links :

Program to show selection sort in array - C++


#include
#include

void selection_sort(int[], int);

void main()
{
int a[50],n,i;
cout< cin>>n;
cout< for(i=0;i {
cin>>a[i];
}
selection_sort(a,n);
}
void selection_sort(int a[],int n)
{
int i,j,temp,k;
cout< for(k=0;k cout< for(i=0;i {
k=i;
for(j=i+1;j,n;j++)
if(a[j] k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
cout< for(k=0;k cout<<"\t"< }
}

Related Links :

Program to Search An Element in 2-Dimensional Array Using Linear Search



void main()
{
int i,j,item,loc=0,loc1=0;
int a[2][2];
clrscr();
printf("\n\tThis Program is Used To seaech an element in 2Dimensional Array using Linear Search\n");
printf("\n\tEneter The Value Of Array:");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\tEneter The Value To Be Serched:");
scanf("%d",&item);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(item==a[i][j])
{
loc=i;
loc1=j;
break;
}
}
}
printf("\n\tThe Item is at %d Row And %d Coloumn.",loc,loc1);
printf("\n\n\t\tSearch Completed.");
getch();
}





OUTPUT

This Program is Used To search an element in 2Dimensional Array using Linear Search
Enter the value of array: 6 4
3 9

Enter the value to be searched ;3

the item is at 2 Row and 1 coloum.


Search completed.

Related Links :

program to sort an elements of an array through selection sort



//Write a program to sort an elements of an array through selection sort
#include
#include
class selection
{
private:
int data[25],r,i,k,loc;
public:
void getdata();
void sort();
void showdata();
};
void selection::getdata()
{
cout< cin>>r;
cout< for(i=0;i {
cin>>data[i];
}
}
void selection::sort()
{
int temp;
for(k=0;k<=r-2;k++)
{
for(int j=k+1;j<=r-1;j++)
{
if(data[k]>data[j])
{
temp=data[k];
data[k]=data[j];
data[j]=temp;
}
}
}
}
void selection::showdata()
{
cout< for(i=0;i {
cout< }
}
void main()
{
clrscr();
selection s;
s.getdata();
s.sort();
s.showdata();
getch();
}

Related Links :

program to insert & delete the elemnts from queue.



//Write a prgram to insert & delete the elemnts from queue
#include
#include
#include
class queue
{
private:
int i,r,q[25],front,rear;
public:
void enqueue(int);
void dequeue();
void getdata();
};
void queue::getdata()
{
clrscr();
cout< cin>>r;
cout< for(i=0;i {
cin>>q[i];
}
front=q[0];
rear=q[2];
}
void queue::dequeue()
{
int item;
if(front==0)
{
cout< exit(0);
}
else
{
item=q[1];
}
if(front==rear)
{
front=0;
rear=0;
}
else
{
front=front+1;
}
cout< for(i=0;i {
cout<<"\t"< }
}
void queue::enqueue(int item)
{
if(rear==r)
{
cout< exit(0);
}
else
{
if(rear==0 &&front==0)
{
front=1;
rear=1;
}
r=r+1;
q[r]=item;
}
cout< for(i=0;i {
cout<<"\t"< }
}
void main()
{
int ch,no;
clrscr();
char cho;
queue q;
q.getdata();
cout< cout<<"want to perform on queue:";
cout< cout< cin>>ch;
do
{
switch(ch)
{
case 1:
cout< cin>>no;
q.enqueue(no);
break;
case 2:
q.dequeue();
break;
}
cout< cin>>cho;
}
while(cho=='y'||cho=='Y');
}

Related Links :

Program to show Push and Pop Method in C++



#include
#include
#include

const int maxstk=10;
int top;
int stack [maxstk];
push (int item);
pop();
display();
void main()
{
int ch, item;

do
{
cout<<"enter 1 for push \n"<<"enter 2 for pop"<<"\n enter 3 for exit \n";
cout <<"enter your choice";
cin>>ch;
switch (ch)
{
case 1:
cout <<"enter item to push";
cin>>item;
push (item);
break;
case 2:
pop();
break;
case 3:
exit (0);
break;
default:
cout<<"input is wrong \n";
break;
}

}
while(1);
}
push(int item)

{
if(top==maxstk)
{
cout<<"overflow\n";
return 0;
}
else
top=top+1;
stack[top]=item;
display();
}

pop()
{
if(top==0)
{
cout<<"underflow\n";
return 0;
}
else
top=top-1;
display();
}
display()
{
cout<<"Stack elements\n";
for(int i=1;i<=top;i++)
{

cout< }
}

Related Links :

Program to search element using linear search from Array in C++


//Linear search
#include
#include
class linear
{
private:
int data[25],loc;
public:
int n,item;
void getdata();
void search();

};
void linear::getdata()
{
cout< cin>>n;
cout< for(int i=1;i<=n;i++)
{
cin>>data[i];
}
}
void linear::search()
{
cout< cin>>item;
int k=1,loc=0;
while(loc==0 && k<=n)
{
if (item==data[k])
{
loc=k;
}
else
{
k=k+1;
}
}

if (loc==0)
{
cout< }
else
{
cout< }

}
void main()
{
clrscr();
linear l;
l.getdata();
l.search();

getch();
}

Related Links :

Program converts the given infix expression in to postfix form



#include
#include
#include
#include

class expression
{
private:
char infix[100];
char stack[200];
int top;
int r;
char postfix[100];
public:
void convert();
int input_p(char);
int stack_p(char);
int rank(char);
};

int expression::input_p(char c)
{
if(c==’+’ || c==’-')
return 1;
else if(c==’*’ || c==’/')
return 3;
else if(c==’^')
return 6;
else if(isalpha(c)!=0)
return 7;
else if(c==’(')
return 9;
else if(c==’)')
return 0;
else
{
cout<<”Invalid expression ::input error\n”;
exit(0);
}
}

int expression::stack_p(char c)
{
if(c==’+’ || c==’-')
return 2;
else if(c==’*’ || c==’/')
return 4;
else if(c==’^')
return 5;
else if(isalpha(c)!=0)
return 8;
else if(c==’(')
return 0;
else
{
cout<<”Invalid expression ::stack error\n”;
exit(0);
}
}

int expression::rank(char c)
{
if(c==’+’ || c==’-')
return -1;
else if(c==’*’ || c==’/')
return -1;
else if(c==’^')
return -1;
else if(isalpha(c)!=0)
return 1;
else
{
cout<<”Invalid expression ::in rank\n”;
exit(0);
}
}

void expression::convert()
{
cout< <<”in to postfix form”;
cout<<”Enter an infix expression ::\n”;
cin>>infix;
int l=strlen(infix);

infix[l]=’)';
infix[l+1]=”;

//Convertion starts
top=1;
stack[top]=’(';

r=0;
int x=-1;

int i=0;
char next=infix[i];

while(next!=”)
{
//Pop all the elements to outputin stack which have higher precedence
while( input_p(next) < stack_p(stack[top]) )
{
if(top<1)
{
cout<<”invalid expression ::stack error\n”;
exit(0);
}

postfix[++x]=stack[top];
top–;

r=r+rank(postfix[x]);

if(r<1)
{
cout<<”Invalid expression ::r<1\n”;
exit(0);
}
}

if(input_p( next ) != stack_p( stack[top]))
stack[++top]=next;
else
top–;

i++;
next=infix[i];
}
postfix[++x]=”;

if(r!=1 || top!=0)
{
cout<<”Invalid expression ::error in rank or stack\n”;
exit(0);
}

cout<<”\n\nThe corresponding postfix expression is ::\n”;
cout<}
int main()
{
expression obj;
obj.convert();
return 0;
}

Related Links :

Program to show Heap Sort Example in C++



#include
#include
int arr[20],n;
class heap
{
private:
int loc,num,par;
public:
void del_root(int);
void create_heap();
void display();
void heap_sort();
void insert(int,int);
};
void heap::insert(int num,int loc)
{
int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}
arr[0]=num;
}
void heap::display()
{
int i;
for(i=0;i {
cout< }
}
void heap::create_heap()
{
int i;
for(i=0;i {
insert(arr[i],i);
}
}
void heap::heap_sort()
{
int last;
for(last=n-1;last>0;last--)
{
del_root(last);
}
}
void heap::del_root(int last)
{
int left,right,i,temp;
i=0;
temp=arr[i];
arr[i]=arr[last];
arr[last]=temp;
left=2*i+1;
right=2*i+2;
while(right {
if(arr[i]<=arr[left]&&arr[i]>=arr[right])
return;
if(arr[right]<=arr[left])
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}
if(left==last-1 && arr[i] {
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}

void main()
{
int i;
clrscr();
cout< cin>>n;
for(i=0;i {
cout< cin>>arr[i];
}
cout< heap h;
h.display();
h.create_heap();
cout< h.display();
h.heap_sort();
cout<<"Sorted list is:";
h.display();
getch();
}

Related Links :

program to sort the elements through Bubble Sort in C++


//Write a program to sort the elements through Bubble Sort
#include
#include
class bubble
{
private:
int r,data[25],i,k,ptr;
public:
void getdata();
void sort();
void showdata();
};
void bubble::getdata()
{
cout< cin>>r;
cout< for(i=0;i {
cin>>data[i];
}
}
void bubble::sort()
{
int temp;
for(k=0;k {
ptr=0;
while(ptr<=r-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr];
data[ptr]=data[ptr+1];
data[ptr+1]=temp;

}
ptr++;
}

}
}
void bubble::showdata()
{
cout< for(int i=0;i {
cout< }
}
void main()
{
clrscr();
bubble b;
b.getdata();
b.sort();
b.showdata();
getch();

}

Related Links :

Program to Sorting & searching Element from Array in C++ By Using Function



//Binear search
#include
#include
class binary
{
private:
int data[25],item,loc,r;
public:
void getdata();
void search();
void sort();
};
void binary::getdata()
{
cout< cin>>r;
cout< for(int i=0;i {
cin>>data[i];
}
}
void binary::sort()
{
int k,ptr,temp;
cout< for(k=0;k {
ptr=0;
while(ptr<=r-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr];
data[ptr]=data[ptr+1];
data[ptr+1]=temp;
}
ptr++;
}
}
for(int i=0;i {
cout< }
}
void binary::search()
{
cout< cin>>item;
int beg=0,end=r;
int mid=((beg+end)/2);
while(beg<=end && data[mid]!=item)
{
if (item {
end=mid-1;
}
else
{
beg=mid+1;
}
mid=((beg+end)/2);
}
if (data[mid]==item)
{
loc=mid+1;
cout< }
else
{
loc=0;
cout< }
}
void main()
{
clrscr();
binary b;
b.getdata();
b.sort();
b.search();
getch();
}

Related Links :

How C Programming Works Under Linux?

How is C under Linux any different than C under DOS or C under Windows? Well, it is same as well as different. It is same to the extent of using language elements like data types, control instructions and the overall syntax. The usage of standard library functions is also same even though the implementation of each might be different under different OS.

For example, a printf( ) would work under all OSs, but the way it is defined is likely to be different for different OSs. The programmer however doesn’t suffer because of this since he can continue to call printf( ) the same way no matter how it is implemented.

But there the similarity ends. If we are to build programs that utilize the features offered by the OS then things are bound to be different across OSs. For example, if we are to write a C program that would create a Window and display a message “hello” at the point where the user clicks the left mouse button. The architecture of this program would be very closely tied with the OS under which it is being built. This is because the mechanisms for creating a window, reporting a mouse click, handling a mouse click, displaying the message, closing the window, etc. are very closely tied with the OS for which the program is being built. In short the programming architecture (better known as programming model) for each OS is different. Hence naturally the program that achieves the same task under different OS would have to be different.

Related Links :

Event Driven programming in c Linux Based

In a GUI program events occur typically when we click on the window, type a character, close the window, repaint the window, etc. We have chosen the GTK library version 2.0 to create the GUI applications. Here, GTK stands for Gimp’s Tool Kit.

below is the first program that uses this toolkit to create a window on the screen.


/* mywindow.c */
# include
int main ( int argc, char *argv[ ] )
{
GtkWidget *p ;
gtk_init ( &argc, &argv ) ;
p = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ;
gtk_window_set_title ( p , "Sample Window" ) ;
g_signal_connect ( p, "destroy", gtk_main_quit, NULL ) ;
gtk_widget_set_size_request ( p, 300, 300 ) ;
gtk_widget_show ( p ) ;
gtk_main( ) ;
return 0 ;
}

Related Links :

How to handle Multiple Signals In Linux Based C Prorogram

How to handle Multiple Signals In
Linux Based C Prorogram



# include
# include
# include
void inthandler ( int signum )
{
printf ( "\nSIGINT Received\n" ) ;
}
void termhandler ( int signum )
{
printf ( "\nSIGTERM Received\n" ) ;
}
void conthandler ( int signum )
{
printf ( "\nSIGCONT Received\n" ) ;
}
int main( )
{
signal ( SIGINT, inthandler ) ;
signal ( SIGTERM, termhandler ) ;
signal ( SIGCONT, conthandler ) ;
while ( 1 )
printf ( "\rProgram Running" ) ;
return 0 ;
}

Related Links :

SIGINT, SIGKILL, SIGCONT, etc. in the file signal.h

A unique number is associated with each signal. To avoid remembering these numbers, they have been defined as macros like SIGINT, SIGKILL, SIGCONT, etc. in the file ‘signal.h’. Every process contains several ‘signal ID - function pointer’ pairs indicating for which signal which function should be called. If we do not decide to handle a signal then against that signal ID the address of the default signal handler function is present. It is precisely this default signal handler for SIGINT that got called when we pressed Ctrl + C when the above program was executed. INT in SIGINT stands for interrupt.

Let us know see how can we prevent the termination of our program even after hitting Ctrl + C. This is shown in the following program:



# include
void sighandler ( int signum )
{
printf ( "SIGINT received. Inside sighandler\n" ) ;
}
int main( )
{
signal ( SIGINT, ( void* ) sighandler ) ;
while ( 1 )
printf ( "Program Running\n" ) ;
return 0 ;
}

Related Links :

Data Conversion Functions

Data Conversion Functions

atof =Converts string to float
atoi = Converts string to int
atol = Converts string to long
ecvt = Converts double to string
fcvt = Converts double to string
gcvt = Converts double to string
itoa = Converts int to string
ltoa = Converts long to string
strtod = Converts string to double
strtol = Converts string to long integer
strtoul = Converts string to an unsigned long integer
ultoa = Converts unsigned long to string

Related Links :

Use of String Manipulation Functions In C

Use of String Manipulation Functions In C

strcat = Appends one string to another


strchr = Finds first occurrence of a given character in a string

strcmp = Compares two strings

strcmpi = Compares two strings without regard to case

strcpy = Copies one string to another


strdup = Duplicates a string


stricmp = Compares two strings without regard to case (identical to strcmpi)


strlen = Finds length of a string


strlwr = Converts a string to lowercase


strncat = Appends a portion of one string to another


strncmp = Compares a portion of one string with portion of another
string

strncpy = Copies a given number of characters of one string to another
strnicmp = Compares a portion of one string with a portion of another without regard to case
strrchr = Finds last occurrence of a given character in a string
strrev = Reverses a string
strset = Sets all characters in a string to a given character
strstr = Finds first occurrence of a given string in another string
strupr = Converts a string to uppercase

Related Links :

Searching and Sorting Functions In C Language

Searching and Sorting Functions In C Language


bsearch = Performs binary search

lfind = Performs linear search for a given value

qsort = Performs quick sort

Related Links :

Directory Control Functions In C

Directory Control Functions In C


chdir = Changes current working directory

getcwd = Gets current working directory

fnsplit = Splits a full path name into its components

findfirst = Searches a disk directory

findnext = Continues findfirst search

mkdir = Makes a new directory

rmdir = Removes a directory

Related Links :

Buffer Manipulation Functions In C

Buffer Manipulation Functions In C


memchr = Returns a pointer to the first occurrence, within a specified number of characters, of a given character in the buffer.

memcmp = Compares a specified number of characters from two buffers.

memcpy = Copies a specified number of characters from one buffer to another

memicmp = Compares a specified number of characters from two buffers without regard to the case of the characters

memmove = Copies a specified number of characters from one buffer to another

memset = Uses a given character to initialize a specified number of bytes in the buffer

Related Links :

Disk I/O Functions (Input-Out ) In C

Disk I/O Functions (Input-Out ) In C

absread=Reads absolute disk sectors
abswrite=Writes absolute disk sectors
biosdisk=Performs BIOS disk services
getdisk=Gets current drive number
setdisk=Sets current disk drive

Related Links :

Memory Allocation Functions In C

Memory Allocation Functions In C

calloc=Allocates a block of memory
farmalloc=Allocates memory from far heap
farfree=Frees a block from far heap
free=Frees a block allocated with malloc
malloc=Allocates a block of memory
realloc=Reallocates a block of memory

Related Links :

List of Graphics Functions In C

List of Graphics Functions In C


arc=Draws an arc
ellipse=Draws an ellipse
floodfill=Fills an area of the screen with the current color
getimage=Stores a screen image in memory
getlinestyle=Obtains the current line style
getpixel=Obtains the pixel’s value
lineto=Draws a line from the current graphic output position to the specified point
moveto=Moves the current graphic output position to a specified point
pieslice=Draws a pie-slice-shaped figure
putimage=Retrieves an image from memory and displays it
rectangle=Draws a rectangle
setcolor=Sets the current color
setlinestyle=Sets the current line style
putpixel=Plots a pixel at a specified point
setviewport=Limits graphic output and positions the logical origin within the limited area

Related Links :

Standard Library Functions Time Related Functions

Time Related Functions


clock=Returns the elapsed CPU time for a process
difftime=Computes the difference between two times
ftime=Gets current system time as structure
strdate=Returns the current system date as a string
strtime=Returns the current system time as a string
time=Gets current system time as long integer
setdate=Sets DOS date
getdate=Gets system date

Related Links :

List of DOS Interface Functions In C

List of DOS Interface Functions In C

FP_OFF=Returns offset portion of a far pointer

FP_SEG=Returns segment portion of a far pointer
getvect=Gets the current value of the specified interrupt vector
keep=Installs terminate-and-stay-resident (TSR) programs
int86=Issues interrupts
int86x=Issues interrupts with segment register values
intdos=Issues interrupt 21h using registers other than DX and AL
intdosx=Issues interrupt 21h using segment register values
MK_FP=Makes a far pointer

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!