Q) Given a stack S, write a C program to sort the stack (in the ascending order

)

It can be done using just the stack operations like this: void sort(stack) { type x; if (!isEmpty(stack)) { x = pop(stack); sort(stack); insert(x, stack); } } void insert(x, stack) { type y; if (!isEmpty(stack) && top(stack) < x) { y = pop(stack); insert(x, stack); push(y, stack); } else { push(x, stack); } } Sajid, I think sort should look like, void sort(stack) { type x; if (!isEmpty(stack)) { x = pop(stack); sort(stack); } else insert(x, stack); } That wouldn't work for: stack = 132 Because it omits to reinsert x: if (!isEmpty(stack)) { /* stack=132 */ x = pop(stack); /* stack=13 x=2*/ sort(stack); /*stack=31 x=2*/

)Given a linked list.. p = p->next.n + 1)th element from the beginning. } return q->data. 8. 12. exit(1). Write a C program to print nth ugly number using while loop. 4. .List is null .(getLastNth) \n" ).List is empty .Two pointers indicates the list->head at first .(getLastNth) \n" ). 1. so having two pointers: . if ( NULL == p ) { fprintf( stderr. "list is null. } while ( p ->next ) { q = q->next.n ) nodes. stack).Then secod pointer now indicates the last nth element.List with length 2 ( get 1.List with length 1 ( get 1 ) . your are asked to find out the nth last element in the linked-list. "Index is out boundary. counter++ ).. 2 ) .Now second pointer walks ( l . 10. exit(1). } Q. By convention.3. 2 ) . 1. Test Cases: . } p = list->head. for ( counter = 0.. *q.List with length 3 ( get 3. if ( NULL == list ) { fprintf( stderr.Any value larger than the size of list int getLastNth( list_t *list..} else insert(x. p = p->next. 1 is included. unsigned int n ) { int counter. 9.1. 2. 15 are the first 11 ugly numbers. p && counter < n . node_t *p. . 3 or 5. } Q) Ugly number is the number whose only prime factors are 2.Both pointers goes simultaneously until first pointer comes to the end of the list . 3. q = list->head. 6. head node is also assumed one walk for two pointers. 5.First pointer goes to nth node from the beginning . (n would be given as the argument to the function) Last nth element is ( l .

Pick up 1000. of bananas one can transfer to city B? Note : the el ephant cannot go without bananas to eat. 533. . go 333 km.* N is a number input given by the user. } i++. return. How Many Max.i). } else if(i==1){ ++count. drop 667. return. drop 600. printf(³Enter a number\t:´). go 200 km. } getch(). Now you have 2000 bananas at 200 km. } Q) There are 2 cities A and B.i=1. drop 334. Pick up 1000.temp. return. if(temp==1){ ++count. Pick up 1000.count = 0. between them is 1000 Km we have 3000 bananas in city A and a elephant can carry max 1000 bananas at any given time and it needs to eat a banana every 1 Km. while(!(temp%5)) temp /= 5. go 200 km.&n). while(count < n){ temp = i. dist. drop 800. while(!(temp%3))temp /= 3. while(!(temp%2)) temp /= 2. Pick up 1000. No. Now you have 1001 bananas at 533 km. } if(count == n){ printf(³%d´. //This program will give nth Ugly number int main(){ int n. go 200 km. go 333 km. drop 600. Pick up 1000. scanf(³%d´.

Pick up 1000. drop 533 banans. You now have 533 bananas in city B and one banana rotting away at 533 km. go 467 km. .

Sign up to vote on this title
UsefulNot useful