Sie sind auf Seite 1von 14

ANS 1)

#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;
}

ANS 2)i) For storing the


stations of a public
transportation line and for
adding new stations from
both the ends, we can use
double ended queues as the
name suggests, we can insert
or delete or traverse from
both the ends of a queue.

ii)Since a phone book


consists of names, phone
numbers and addresses, we
require structures as it can
have different data elements
of different data types but
since we need to add and
remove entries, we should
use linked lists so that we
can add or remove the
entries we want to.

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)).

As g(n) is always less than


f(n) for c=1 and for any
natural no. n.

ii) g(n) Is (f(n))

As g(n) is always greater than


f(n) for c=1 for any natural
no. n

iii) f(n) is O(g(n))

As for smaller c and n, f(n) is


always less than g(n)

iv) f(n) is O(g(n)) for c=1 and


any natural no. n

ANS 4) #include<stdio.h>
#define size 50

int f=-1,r=-1;

typedef struct{

int item;

int prty;

} pqueue;

pqueue pq[size];

void pqinsert(int elem,int pre)

{ int i; if(Qfull())

printf("\nOverflow\n");

if(f ==-1 && r == -1)

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()

{ if(f > r) return

1; return 0;

void display()

{ int

i;

if(Qempty()) printf(" \n Empty Queue\n");

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");

printf("\n4.show highest priority");

printf("\nenter your choice(1-4):\n");

scanf("%d",&ch); switch(ch)

case 1: printf("\n\nRead the element to be Inserted and its priority?");

scanf("%d%d",&P.item,&P.prty); pqinsert(P.item,P.prty);

break; case 2: P=pqdelete(); if(P.item!=-1)

printf("\n\nDeleted Element is %d \n",P.item);

break;

case 3: printf("\n\nStatus of Queue\n\n");

display();

break;

case 4: gethighestpriority(P.item,P.prty);

break;

default: printf("\nwrong choice\n");

break;

}
void gethighestpriority()

{ int i,max,j; if(Qempty()) printf(" \n

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;

}printf("the highest priority element is %d",pq[j].item);

ANS 5)

// C program to find circular


tour for a truck

#include <stdio.h>
// A petrol pump has petrol
and distance to next petrol
pump

struct petrolPump

int petrol;

int distance;

};

// The function returns


starting point if there is a
possible solution,

// otherwise returns -1

int printTour(struct
petrolPump arr[], int n)

// Consider first petrol


pump as a starting point

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.

And we have reached first


petrol pump again with 0 or
more petrol */

while (end != start ||


curr_petrol < 0)

// If curremt amount of
petrol in truck becomes less
than 0, then

// remove the starting


petrol pump from tour

while (curr_petrol < 0


&& start != end)

// Remove starting
petrol pump. Change start

curr_petrol -=
arr[start].petrol -
arr[start].distance;

start = (start + 1)%n;

// If 0 is being
considered as start again,
then there is no

// possible solution

if (start == 0)
return -1;

// Add a petrol pump to


current tour

curr_petrol +=
arr[end].petrol -
arr[end].distance;

end = (end + 1)%n;

// Return starting point

return start;

// Driver program to test


above functions

int main()

struct petrolPump arr[] =


{{6, 4}, {3, 6}, {7, 3}};

int n =
sizeof(arr)/sizeof(arr[0]);

int start = printTour(arr, n);


(start == -1)? printf("No
solution"): printf("Start =
%d", start);

return 0;

Das könnte Ihnen auch gefallen