Professional Documents
Culture Documents
Data Structures
Definition:
Data structure is a representation of logical relationship existing between individual
elements of data.
Or
A data structure is a way of organizing all the data items that considers not only the
elements stored but also their relationship to each other.
Data structures
Non-Primitive
Primitive Data Data Structures
Structures
Stacks Trees
Queues
Graphs
1 /* Write a program to create a sequential file with at least five records each record
having the structure shown below
USN Name Marks1 Marks2 Marks3
Non-Zero 25 characters Positive Positive Positive
positive integer integer integer
integer
Write necessary functions 1.To display all the records in the file. 2. To search for a
specific record based on USN in case the required record is not found suitable
message should be displayed both the options in this case must be demonstrated. */
#include<stdio.h>
struct file
{
int usn;
char name[25];
int m[3];
};
typedef struct file S;
main()
{
int ch;
while(1)
{
clrscr();
printf(" 1 Create\n 2 Display\n 3 Search\n 4 Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: create(); break;
case 2: display(); break;
case 3: search(); break;
case 4: exit(0);
}
getch();
}
}
create()
{
FILE *fp;
int i,j,n;
S s[10];
fp=fopen("stud.c","w");
if(fp==NULL)
printf("Error..............\n");
else
{
printf("Enter the number of records\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d student record\n",i+1);
scanf("%d%s",&s[i].usn,s[i].name);
printf("Enter the 3 marks\n");
for(j=0;j<3;j++)
scanf("%d",&s[i].m[j]);
}
}
for(i=0;i<n;i++)
{
fprintf(fp,"\t%d\t%s\t\t",s[i].usn,s[i].name);
for(j=0;j<3;j++)
fprintf(fp,"%d\t",s[i].m[j]);
fprintf(fp,"%c",'\n');
}
fclose(fp);
}
/*********************Create function***************************/
display()
{
FILE *fp;
char ch;
fp=fopen("stud.c","r");
printf("The Contents of The file is\n");
printf("\tUSN\t NAME\t\tM1\tM2\tM3\n");
printf("______________________________________________\n");
while((ch=getc(fp))!=EOF)
{
printf("%c",ch);
}
printf("\n\n__________________________________________\n");
printf("End OF File..............\n");
fclose(fp);
}
/**********************display function()**************************/
search()
{
FILE *fp;
char name[25];
int id,un;
int m1,m2,m3;
fp=fopen("stud.c","r");
if(fp==NULL)
printf("Error......................\n");
else
{
printf("Enter the University number of stud\n");
scanf("%d",&id);
for(;;)
{
fscanf(fp,"%d%s%d%d%d",&un,name,&m1,&m2,&m3);
if(feof(fp))
{ printf("Sorry NOT FOund\n");
break;
}
if(un==id)
{ printf("RECORD is Found\n");
printf("\tUSN is %d\n\tNAME is
%s\n\tM1=%d\n\tM2=%d\n\tM3=%d\n\t",un,name,m1,m2,m3);
return;
}
}
}
fclose(fp);
}
/*** End of search()***********/
/*************************End of program **************/
pop()
{
if(top==0)
printf("Stack Under flow\n");
else
printf("The Poped element is %d\n",stack[--top]);
display()
{ int i;
if(top==0)
printf("Stack Empty\n");
else
{
printf("The Elements in Stack are\n");
for(i=top-1;i>=0;i--)
printf("%d\n",stack[i]);
}
}
/* End of Display() ****** */
3 /* Write a C program to convert and print a given valid parenthesized Infix arithmetic
Step2: a) If the scanned symbol is left parentheses, push it into the stack.
b) If the scanned symbol is right parentheses, then go on popping all the items
from the stack and place them in the postfix expression till we get the
expression.
from the
stack and place them in the postfix expression, as long as precedence of the
operator, which is on the top of the stack, is greater than or equal to the
precedence of the scanned symbol, otherwise push the scanned symbol onto
the stack.
Step3: Repeat the above steps as long as symbols present in infix expression.
#include<stdio.h>
#include<string.h>
char infix[20],pfix[20],stack[20];
int pos,i,len,top=-1;
void main()
{
clrscr();
printf("Enter the valid infix expression\n");
scanf("%s",infix);
convert();
printf("The postfix Expression is :%s",pfix);
getch();
}
/* End of main function */
convert()
{
char temp,sym;
len=strlen(infix);
push('#');
while(i<len)
{
sym=infix[i];
switch(sym)
{
case '(': push(sym); break;
case ')': temp=pop();
while(temp!='(')
{ pfix[pos++]=temp;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^': while(preced( stack[top])>=preced(sym))
{
temp=pop();
pfix[pos++]=temp;
}
push(sym);//condition fails push symbol into stack
break;
default: pfix[pos++]=sym;
}
i++;
}
//any items left in the stack pop and add them to postfix expression
while(top>0)
{
pfix[pos++]=pop();
}
preced(char c)
{
int p;
switch(c)
{ case '^': p=3; break;
case '/':
case '*': p=2; break;
case '+':
case '-': p=1; break;
case '(':
case ')': p=0; break;
case '#': p=-1; break;
}
return p;
}
/* End of precedence function it returns precedence of operators*/
push(char c)
{
stack [++top]=c;
}
/* End of Push */
pop()
{
char r;
r=stack [top--];
return r;
}
/* End of POP */
Step3: a) If the scanned symbol is an operand, then push its value onto the stack.
b) If the scanned symbol is an operator, then pop out two values from the stack
and store
If the operator is +
Case +: result=op1+op2;
Case -: result=op1-op2;
Case *: result=op1*op2;
Step6: Repeat the above steps as long as symbols present in the Postfix Expression
#include<stdio.h>
char pfix[20];
int stack[20],top=0;
int value[20];
void main()
{
int i=0,r;
clrscr();
scanf("%s",pfix);
while(pfix[i]!='\0')
{
if(isalpha(pfix[i])) //isalpha() checks of the charcter
{
printf("Enter the value of %c\n",pfix[i]);
scanf("%d",&value[i]);
}
i++;
}
getch();
}
evaluvate()
{
int i=0;
int p1,p2,t;
char c;
while(pfix[i]!='\0')
{
c=pfix[i];
if(isalpha(c))
push(value[i]);
else
{
p2=pop();
p1=pop();
switch(c)
{
case '+': push(p1+p2);break;
case '-': push(p1-p2); break;
case '/': push(p1/p2); break;
case '*': push(p1*p2); break;
case '^': push(pow(p1,p2)); break;
}
}
i++;
}
t=pop();
return t;
}
/* End of Evaluate Function */
push(int n)
{
stack[++top]=n;
#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX];
int r=0,f=0;
void main()
{
int ch;
while(1)
{ clrscr();
printf("1 Insert\n2 Delete\n3 Display\n4 Exit\n");
printf("Enter Your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: del(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
getch();
}
}
/* ****** ** End of main function ******** */
insert()
{
if(r==MAX)
printf("Queue Overflow\n");
else
{
printf("Enter the element to be Insert\n");
scanf("%d",&queue[r++]);
printf("Operation Success\n");
}
}
del()
{
if(r==f)
printf("Queue Under flow\n");
else
printf("The Deleted element is %d\n",queue[f++]);
}
/* *** End del() **** */
display()
{
int i;
if(r==f)
printf("Queue Empty\n");
else
{
printf("The Elements in Queue are\n");
for(i=f;i<r;i++)
printf("%d\n",queue[i]);
}
}
#include<stdio.h>
#define SIZE 5
int queue[SIZE];
void insert(),deletee(),display();
main()
{ int f=0,r=-1,s=0,n;
int ch;
while(1)
{ clrscr();
printf("MENU\n 1 INSERT\n 2 DELETE\n 3 DISPLAY\n 4 EXIT\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element\n");
scanf("%d",&n);
insert(n,&r,&s); break;
case 2: deletee(&f,&s); break;
case 3: display(f,s); break;
case 4: exit(0); break;
default: printf("Invalid choice\n");
}
getch();
}
}
queue[*r]=n;
*s=*s+1;
printf("Insert success\n");
}
}
/* End of Insert() */
}
}
{
insert(q3,n,r3);
r3++;
}
break;
default: printf("Invalid priority element discarded re-enter\n");
}
break;
case 2: if(f1==r1)
{ printf("Q1 is empty\n");
if(f2==r2)
{ printf("Q2 is empty\n");
if(f3==r3)
printf("Q3 is Empty\n");
else
{ printf(" The deleted element is
d\n",deletee(q3,f3));
f3++;
} }
else
{
printf("The deleted element is
\n",deletee(q2,f2));
f2++;
} }
else
{
printf("The deleted element is %d\n",deletee(q1,f1));
f1++;
}
break;
case 3: if(r1==f1)
printf("Q1 is empty\n");
else
{ printf("Elements in Q1 are \n");
display(q1,f1,r1);
}
if(r2==f2)
printf("Q2 is empty\n");
else
}
/* ************ End of Deletee() function ********** */
printf("\n");
OR
#include<stdio.h>
#include<conio.h>
#define MAX 5
struct pq
{
int queue[MAX];
int f,r;
}Q[3];
void main()
{
int ch,i;
for(i=0;i<3;i++)
{
Q[i].f=0;
Q[i].r=0;
}
while(1)
{ clrscr();
printf("1 Insert\n2 Delete\n3 Display\n4 Exit\n");
printf("Enter Your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: del(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
getch();
}
}
/* ****** ** End of main function ******** */
insert()
{
int qn,n;
printf("Enter the Q number and Element to be insert\n");
scanf("%d%d",&qn,&n);
if(Q[qn-1].r==MAX)
printf(" %d Queue Overflow\n",qn);
else
{
Q[qn-1].queue[Q[qn-1].r++]=n;
printf("Operation success\n");
}
}
del()
{ if(Q[0].f==Q[0].r)
{
printf("%d Queue Underflow\n",1);
if(Q[1].f==Q[1].r)
{
printf("%d Queue Underflow\n",2);
if(Q[2].f==Q[2].r)
printf("%d Queue Underflow\n",3);
else
printf("The Deleted item is %d\n",Q[2].queue[Q[2].f++]);
}
else
printf("The Deleted item is %d\n",Q[1].queue[Q[1].f++]);
}
else
printf("The Deleted item is %d\n",Q[0].queue[Q[0].f++]);
}
display()
{ int i,j;
for(i=0;i<3;i++)
{
if(Q[i].f==Q[i].r)
printf(" Queue%d Empty\n",i+1);
else
{
printf("The elements In %d Queue are\n",i+1);
for( j=Q[i].f;j<Q[i].r;j++)
printf("%d\t",Q[i].queue[j]);
}
printf("\n");
}
}
/* End of Display() ****** */
int ch;
head=(N *)malloc(sizeof(N));
head=NULL;
while(1)
{ clrscr();
printf("\n\t1 InsertFront\n\t2 InsertBack\n\t3 InsertAtpos\n\t4 Delete\n\t5
Display\n\t6 SearchRec\n\t7 Exit\n");
case 3: insertatspos();
break;
case 4: deletee();
break;
insertfront()
{
N *temp;
temp=(N*)malloc(sizeof(N));
getdata();
if(head==NULL)
{ temp->rno=m ;
strcpy(temp->name,n);
temp->sem=s;
temp->next=NULL;
head=temp;
}
else
{
temp->rno=m;
strcpy(temp->name,n);
temp->sem=s;
temp->next=head;
head=temp;
}
insertback()
{
N *temp,*p;
p=head;
temp=(N *)malloc(sizeof(N));
getdata();
if(head==NULL)
printf("List is Empty\n");
else
{
while(p->next!=NULL)
{
p=p->next;
}
temp->rno=m;
strcpy(temp->name,n);
temp->sem=s;
temp->next=NULL;
p->next=temp;
}
}
/* **********End of insertBack function************* */
insertatspos()
{
int c=1,pos;
N *temp,*p;
p=head;
temp=(N*)malloc(sizeof(N));
getdata();
printf("Enter the position\n");
scanf("%d",&pos);
if(head==NULL)
printf("List is Empty\n");
else
{ while(c<pos)
{
p=p->next;
if(p==NULL)
{
printf("Number of nodes Less than the postion\n");
break;
}
else
c++;
}
temp->rno=m;
strcpy(temp->name,n);
temp->sem=s;
temp->next=p->next;
p->next=temp;
}
}
/* ********** End of insertat specified position ***** */
display()
{
N *temp;
temp=head;
printf("The list is \n");
printf("____________________________________________________________\n");
printf("SID\t\tNAME\t\tSEM\n");
printf("____________________________________________________________\n");
while(temp!=NULL)
{
printf("%3d\t%12s\t\t%2d\n",temp->rno,temp->name,temp->sem);
temp=temp->next;
}
printf("_________________________________________________________NULL");
}
/* *********** End of Display() ************** */
search()
{
N *temp;
int f=0,id;
printf("Enter the student id\n");
scanf("%d",&id);
if(head==NULL)
{
printf("Not Found\n");
return;
}
temp=head;
while(temp!=NULL)
{
if(temp->rno==id)
{
printf("RECORD FOUND IN THE LIST\n");
printf("\nRNO %d\nNAME %s\nSEM %d",temp->rno,temp->name,temp->sem);
f=1;
break;
}
temp=temp->next;
}
if(f==0)
printf("Record Not Found\n");
}
deletee()
{
N *t1,*t2;
int id;
t1=head;
printf("Enter the student id to delete?");
scanf("%d",&id);
if(head==NULL)
printf("List is Empty\n");
else
if(t1->rno==id)
{ t1=t1->next;
head=t1;
}
else
{
while(t1->rno!=id)
{
if(t1->next==NULL)
{printf("NOT Found\n");
return;
}
t2=t1;
t1=t1->next;
}
t2->next=t1->next;
}
printf("The %d is deleted from list\n",id);
}
/* End of Deletee */
/* End of Program */
void push()
{
S *temp;
temp=(S*)malloc(sizeof(S));
printf("Enter the element\n");
scanf("%d",&temp->data);
if(top==NULL)
{
temp->next=NULL;
top=temp;
}
else
{
temp->next=top;
top=temp;
}
printf("Element pushed successfully\n");
}
/* *** End of push function ************ */
void pop()
{ S *temp;
temp=top;
if(top==NULL)
printf("Stack Underflow\n");
else
{
top=top->next;
printf("Element deleted is %d",temp->data);
free(temp);
}
} /*** End of delete function **** */
void display()
{ S *temp;
temp=top;
if(top==NULL)
printf("Stack Empty\n");
else
{
printf("Elements in stack are\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}
/* *********** end of display ******* */
10/* Program using Dynamic Variables and pointers to construct a queue of Integers
using SLL and to perform the following operations.
1. Insert
2. Delete
3. Display
Also check for appropriate conditions like overflow, underflow, Empty.*/
#include<stdio.h>
struct queue
{
int data;
struct queue *next;
};
typedef struct queue Q;
Q *front;
void insert();
void deletee();
void display();
void main()
{
int ch;
front=(Q*)malloc(sizeof(Q));
front=NULL;
while(1)
{ clrscr();
printf("1 Insert\n2 Delete\n3 Display\n4 Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: deletee(); break;
case 3: display(); break;
case 4: exit(0); break;
default: printf("invalid choice\n");
}
getch();
}
}
/* end of main function */
void insert()
{
Q *temp,*t;
temp=(Q*)malloc(sizeof(Q));
printf("Enter the element to be insert\n");
scanf("%d",&temp->data);
if(front==NULL)
{
temp->next=NULL;
front=temp;
}
else
{ t=front;
while(t->next!=NULL)
{
t=t->next;
}
temp->next=NULL;
t->next=temp;
}
} /* ************************ End of insertQ *********/
void deletee()
{
Q *temp;
if(front==NULL)
printf("Queue underflow\n");
else
{ temp=front;
front=front->next;
printf("Deleted element is %d\n",temp->data);
free(temp);
}
}
/* **************** End of Delete function ************/
void display()
{ Q *temp;
temp=front;
if(front==NULL)
printf("Queue Empty\n");
else
{
printf("The elements in Queue are\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
11./* Write a program to support the following operations on doubly linked list where
each node consists of integers.
a Create a doubly linked list by adding each node at the front.
b Insert a new node to the left if the node whose key value is read as an input
c Delete the node of a given data, if it is found, otherwise display appropriate
message.
d Display the contents of the list. */
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *next;
struct list *prev;
};
typedef struct list NODE;
NODE *head;
main()
{
int ch;
head=(NODE*)malloc(sizeof(NODE));
head=NULL;
while(1)
{
clrscr();
printf("1 InsertB\n2 Display\n3 InsertAtPosition\n4 Delete\n5 Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: display(); break;
case 3: insertp(); break;
case 4: deletee(); break;
case 5: exit(0);
}
getch();
}
}
/* End of main function ********* */
insert()
{
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the data\n");
scanf("%d",&temp->data);
if(head==NULL)
{
temp->next=NULL; /* if list emty temp is the first and last node*/
temp->prev=NULL;
head=temp;
}
else
{
temp->next=head;
temp->prev=NULL; /* if list is not empty make temp as first node*/
head->prev=temp;
head=temp;
}
}
insertp()
{
NODE *temp,*t;
int p;
t=head;
temp=(NODE *)malloc(sizeof(NODE));
printf("Enter the data and position\n");
scanf("%d%d",&temp->data,&p);
while(p>1)
{
if(t->next==NULL)
{
printf("Number of nodes less than position\n");
return;
}
p--;
t=t->next;
}
temp->next=t->next;
temp->prev=t;
t->next=temp;
temp->next->prev=temp;
}
/* End of insert at position *****/
deletee()
{
NODE *temp,*t,*d;
int n;
temp=head;
printf("Enter the data to be delete?\n");
scanf("%d",&n);
if(temp->data==n)
{ d=temp;
temp=temp->next; /* if n is first node delete it*/
temp->prev=NULL;
head=temp;
free(d);
return;
}
#include<stdio.h>
void main()
{
int a[20],n,i,j,temp;
int low,high,k,R;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elemets\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(a[i]<=a[j])
{
temp=a[i]; /* if elements are unsorted it sorts the elements*/
a[i]=a[j]; /* because binary search works for only sorted array*/
a[j]=temp;
}
}
}
printf("Enter the element to be search\n");
scanf("%d",&k);
low=0;
high=n-1;
R=bsearch(a,low,high,k);
if(R>=0)
printf("Search Success..... elemet found at %d",R+1);
else
printf("Element not Found\n");
getch();
}
/* End of main() *********** */
b)
/* Program to solve Tower of hanoi problem */
#include<stdio.h>
main()
{ int n;
clrscr();
printf("Enter the number of discks\n");
scanf("%d",&n);
tower(n,'S','D','T');
getch();
}
/* End of main()****/
13. /* Write a C program to sort a list of N strings using insertion sort algorithm. The
necessary data must be read from a sequential file.*/
#include<stdio.h>
main()
{
int i=0,j,k,m;
char fname[10],name[200][30],s[30];
FILE *fp;
clrscr();
printf("Enter the file name\n");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
{ printf("Can't open........................\n");
exit(0);
}
printf("The names in %s file are\n",fname);
for(;;)
{
fscanf(fp,"%s",s);
if(feof(fp)) /* reads the names from the file */
break;
strcpy(name[i++],s); /*store the name taken from the file int array*/
printf("%s ",s);
}
m=i;
printf("\nThe sorted order is \n");
for(j=0;j<m;j++)
{ strcpy(s,name[j]);
for(k=j-1; (k>=0&&strcmp(name[k],s)>0);k--)
{
strcpy(name[k+1],name[k]);
}
strcpy(name[k+1],s);
}
for(i=0;i<m;i++)
printf("%s\n",name[i]);
getch();
}
#include<stdio.h>
#define max 20
void input(int *,int *,int *);
int lsearch(int *,int,int);
void bsort(int *,int);
int bsearch(int *,int,int);
void main()
{
int i,a[max],key,pos1=0,pos2=0,n;
clrscr();
for(;;)
{
clrscr();
printf("\n List of searching Techniques");
printf("\n 1. Linear Search");
printf("\n 2. Binary Search");
printf("\n Exit");
printf("\n Enter your choice");
fflush(stdin);
switch(getchar())
{
case '1':
input(a,&n,&key);
pos1= lsearch(a,n,key);
break;
case '2':
input(a,&n,&key);
bsort(a,n);
printf("\n Sorted array is:\n\n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
pos2= bsearch(a,n,key);
break;
case '3':
exit(1);
default: printf("\n Envalid Choice");
}
if(pos1)
if(a[i]==key)
return(i+1);
return(0);
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct bst
{
int info;
struct bst *right, *left;
};
typedef struct bst * NODEPTR;
void main()
{
int ch,item;
NODEPTR root=NULL;
for(;;)
{
clrscr();
printf("\nOperations on BST");
printf("\n-----------------");
printf("\n1. Create BST");
printf("\n2. Preorder traversal");
printf("\n3. Postorder traversal");
printf("\n4. Inorder traversal");
printf("\n5. Exit");
printf("\n\nEnter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
root=create(root,item);
break;
case 2: printf("\nPreorder Traversal: ");
preorder(root);
break;
case 3: printf("\nPostorder Traversal: ");
postorder(root);
break;
IF THE FORGOING PROCESS IS REPEATED WITH SUBARRAY A[0] AND A[J-1] AND
A[J+1] THROUGH A[N-1]
AND ANY SUBARRAY CREATED BY THE PROCESS IN SUCCESSIVE ITERATIONS
THE FINAL RESULT IS A SORTED FILE.
PARTITION(A,LB,UB)
#include<stdio.h>
#include<conio.h>
quick(x,lb,j-1);
/* recursively sort the subarray between position lb aND J-1*/
quick(x,j+1,ub);
/* recursively sort the subarray between position J+1 aND ub*/
}
}
x[down]=x[up];
x[up]=temp;
}
}
x[lb]=x[up];
x[up]=a;
return(up);
}
void main()
{
int n,i;
int x[100];
clrscr();
printf("\n Enter the number element");
scanf("%d",&n);
printf("\n Enter array element:");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
quick(x,0,n-1);
printf("\n sorted array is:\n\n");
for(i=0;i<n;i++)
printf("%d",x[i]);
getch();
}
HEAP SORT
Heap sort is simply an implementation of the general selection sort using the input array
'A' as a heap representing a descending priority queue.
ALGORITHM for HEAP SORT (Ascending Order)
procedure pq_insert(A,k,ele)
procedure pq_maxdel(A,k+1)
procedure adjust(root,k)
procedure largeson(p,m)
#include<stdio.h>
#include<math.h>
int a[31];
void main()
{
register int i;
int n,y;
while(1)
{
clrscr();
printf("\nEnter the number of elements in the Array\t:\t");
scanf("%d",&n);
printf("\n\nEnter the elements in the Array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
clrscr();
printf("\nArray Before Sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=1;i<n;i++)
pqins(i,a[i]);
printf("\n\nInitial Heap\n");
display(n,10);
getch();
for(i=n-1,y=30;i>0;i--,y+=20)
{
a[i]=pqmaxdel(i+1);
if(y>=50)
{
clrscr();
y=10;
}
printf("\nHeap After a[%d]=pqmaxdel(%d)",i,i+1);
display(n,y);
getch();
}
printf("\n\nArray After Sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\n\nDo You Want to Continue?(Y/N) :\t");
fflush(stdin);
if(toupper(getchar())!='Y')
break;
}
}
int pqmaxdel(int k)
{
int p;
p=a[0];
adjust(0,k-1);
return(p);
}
void adjust(int r,int k)
{
int f,s,key;
f=r;
key=a[k];
s=largeson(f,k-1);
while(s>=0&&a[s]>key)
{
a[f]=a[s];
f=s;
s=largeson(f,k-1);
}
a[f]=key;
}
int largeson(int p,int m)
{
int s;
s=2*p+1;
if(s+1<=m&&a[s]<a[s+1])
s=s+1;
if(s>m)
return -1;
return s;
}
void display(int n,int y)
{
int i,j,x,k,c=0;
for(i=0,k=40 ; ;i++,k/=2,y+=3)
for(j=0,x=k ;j<pow(2,i) ;j++,x+=80/pow(2,i))
{
gotoxy(x,y);
printf("%d",a[c++]);
if(c==n)
return;
}