(DS-3)

 3A.Write a program to implement the concept of Stack with Push, Pop, Display and Exit operations.

#include<stdio.h>

#define size 3

void push();

void pop();

void peek();

void print();

int stack[size],TOP=-1,val;

int main()

{

int ch;

printf("------Menu Driven--------");

do

{

printf("\n 1 -> PUSH \n 2 -> PRINT\n 3 -> POP\n 4 -> PEEK \n 5 -> EXIT");

printf("\nEnter a choice:");

scanf("%d",&ch);

switch(ch)

{

case 1: push();

break;

case 2: print();

break;

case 3: pop();

break;

case 4: peek();

break;

case 5: 

break;

}

}while(ch!=5);

return 0;

}

void push()

{

printf("\nEnter an element or -1 to Exit:");

scanf("%d",&val);

do

{

if(TOP==size-1)

{

printf("\nStack is full");

break;

}

else

{

stack[++TOP]=val;

printf("\nInserted successfully");

}

printf("\nEnter an element or -1 to Exit:");

scanf("%d",&val);

}while(val!=-1);

}

void pop()

{

printf("\nEnter 1 to delete or -1 to Exit:");

scanf("%d",&val);

do

{

if(TOP==-1)

{

printf("\nstack is empty");

break;

}

else

{

printf("\nDeleted element is %d",stack[TOP]);

TOP--;

}

printf("\nEnter 1 to delete or -1 to Exit:");

scanf("%d",&val);

}

while(val!=-1);

}

void peek()

{

if(TOP==-1)

{

printf("\nstack is empty");

}

else

{

printf("\nThe Topmost element is %d",stack[TOP]);

}

}

void print()

{

int i;

printf("\nThe stack is:");

for(i=0 ; i<=TOP ; i++)

{

printf("\t %d",stack[i]);

}

}

3B.Write a program to implement Tower of Hanoi problem.

#include <stdio.h>

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

int main()

{

    int n = 4; 

    towerOfHanoi(n, 'A', 'C', 'B');  

    return 0;

}



Comments

Popular posts from this blog

python(BI)

Prac_8(AMP)

LSA10