Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int p1[100],p2[100],p3[100],top1=0,top2=0,top3=0;
void pushp1(int a)
{
if(a!=0)
p1[top1++]=a;
}
int popp1(void)
{
if(top1==0)
return 0;
else
return p1[--top1];
}
void displayp1(void)
{
int i;
printf("\nTower 1 ->> ");
if(top1==0)
printf("Empty");
else
for(i=0;i<top1;i++)
{
printf("%d ",p1[i]);
}
printf("\n");
}
void pushp2(int a)
{
if(a!=0)
p2[top2++]=a;
}
int popp2(void)
{
if(top2==0)
return 0;
else
return p2[--top2];
}
void displayp2(void)
{
int i;
printf("Tower 2->> ");
if(top2==0)
printf("Empty");
else
for(i=0;i<top2;i++)
{
printf("%d ",p2[i]);
}
printf("\n");
}
void pushp3(int a)
{
if(a!=0)
p3[top3++]=a;
}
int popp3(void)
{
if(top3==0)
return 0;
else
return p3[--top3];
}
void displayp3(void)
{
int i;
printf("Tower 3 ->> ");
if(top3==0)
printf("Empty");
else
for(i=0;i<top3;i++)
{
printf("%d ",p3[i]);
}
printf("\n");
}
int main()
{
int n,i,x,a,b;
printf("\nEnter the no. of Disks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pushp1(n-i);
}
x=pow(2,n)-1;
displayp1();
displayp2();
displayp3();
for(i=1;i<=x;i++)
{
if(i%3==1)
{
a=popp1();
b=popp3();
if(a==0)
{
pushp1(b);
}
else if(b==0)
{
pushp3(a);
}
else if(a>b)
{
pushp1(a);
pushp1(b);
}
else
{
pushp3(b);
pushp3(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
else if(i%3==2)
{
a=popp1();
b=popp2();
if(a==0)
{
pushp1(b);
}
else if(b==0)
pushp2(a);
else if(a>b)
{
pushp1(a);
pushp1(b);
}
else
{
pushp2(b);
pushp2(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
else if(i%3==0)
{
a=popp2();
b=popp3();
if(a==0)
{
pushp2(b);
}
else if(b==0)
pushp3(a);
else if(a>b)
{
pushp2(a);
pushp2(b);
}
else
{
pushp3(b);
pushp3(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
}
printf("\nCompleted\nFinal Position\n");
displayp1();
displayp2();
displayp3();
return 0;
}
iii) A linked list, because it supports efficient insertion of the elements of the
second list into the proper place inside the first list while merging. The
insertion is done by re-linking existing cells and does not require creating a
copy of either of the lists.
iv). A queue, because the first call added to the data structure should be the
first one to be processed.
ANS.3)
i) g(n) is O(f(n)).
ANS 4) #include<stdio.h>
#define size 50
int f=-1,r=-1;
typedef struct{
int item;
int prty;
} pqueue;
pqueue pq[size];
{ int i; if(Qfull())
printf("\nOverflow\n");
f++;
r++;
pq[r].item = elem;
pq[r].prty = pre;
else
i=r;
++r;
while(pq[i].prty>=pre&&i>=0)
{
pq[i+1]=pq[i];
i--;
pq[i+1].item=elem;
pq[i+1].prty=pre;
pqueue pqdelete()
pqueue P;
if(Qempty())
printf("\nempty queue\n");
P.item=-1;
P.prty=-1; return(P);
else
P=pq[f];
f=f+1;
return(P);
int Qfull()
{
if(r==size-1) return 1;
return 0;
int Qempty()
1; return 0;
void display()
{ int
i;
else
printf("Front->"); for(i=f;i<=r;i++)
printf("[%d,%d] ",pq[i].item,pq[i].prty);
printf("<-Rear");
void main()
{
int ch;
pqueue P;
while(1)
printf("\n1.insertion\n");
printf("\n2.deletion\n");
printf("\n3.display\n");
scanf("%d",&ch); switch(ch)
scanf("%d%d",&P.item,&P.prty); pqinsert(P.item,P.prty);
break;
display();
break;
case 4: gethighestpriority(P.item,P.prty);
break;
break;
}
void gethighestpriority()
Empty Queue\n");
else
max=pq[f].prty;
for(i=f;i<=r;i++)
if(pq[i].prty>max)
max=pq[i].prty;
j = i;
ANS 5)
#include <stdio.h>
// A petrol pump has petrol
and distance to next petrol
pump
struct petrolPump
int petrol;
int distance;
};
// otherwise returns -1
int printTour(struct
petrolPump arr[], int n)
int start = 0;
int end = 1;
int curr_petrol =
arr[start].petrol -
arr[start].distance;
/* Run a loop while all
petrol pumps are not visited.
// If curremt amount of
petrol in truck becomes less
than 0, then
// Remove starting
petrol pump. Change start
curr_petrol -=
arr[start].petrol -
arr[start].distance;
// If 0 is being
considered as start again,
then there is no
// possible solution
if (start == 0)
return -1;
curr_petrol +=
arr[end].petrol -
arr[end].distance;
return start;
int main()
int n =
sizeof(arr)/sizeof(arr[0]);
return 0;