0% found this document useful (0 votes)
9 views

DDS PRACTCAL

The document contains multiple C programs demonstrating various data structures and algorithms including stack operations, infix to postfix conversion, postfix expression evaluation, Tower of Hanoi, and queue operations. Each program includes user interaction for performing operations and displays results accordingly. The code is structured with functions for specific tasks, ensuring modularity and clarity.

Uploaded by

Jahnvi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DDS PRACTCAL

The document contains multiple C programs demonstrating various data structures and algorithms including stack operations, infix to postfix conversion, postfix expression evaluation, Tower of Hanoi, and queue operations. Each program includes user interaction for performing operations and displays results accordingly. The code is structured with functions for specific tasks, ensuring modularity and clarity.

Uploaded by

Jahnvi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DDS PRACTCAL

1. #include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

void main()

top=-1;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("\n\t EXIT POINT ");

break;
}

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

else

printf(" Enter a value to be pushed:");

scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t Stack is under flow");

else

printf("\n\t The popped elements is %d",stack[top]);

top--;

void display()

if(top>=0)

{
printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

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

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

OUTPUT:

2. #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define MAX_EXPR_SIZE 100


// Function to return precedence of operators

int precedence(char operator)

switch (operator) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

default:

return -1;

// Function to check if the scanned character

// is an operator

int isOperator(char ch)

return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^');

// Main function to convert infix expression

// to postfix expression

char* infixToPostfix(char* infix)

int i, j;

int len = strlen(infix);

char* postfix = (char*)malloc(sizeof(char) * (len + 2));

if (!postfix) {

return "Memory Allocation Error";

char stack[MAX_EXPR_SIZE];

int top = -1;

for (i = 0, j = 0; i < len; i++) {

if (infix[i] == ' ' || infix[i] == '\t')

continue;

// If the scanned character is operand

// add it to the postfix expression


if (isalnum(infix[i])) {

postfix[j++] = infix[i];

// if the scanned character is '('

// push it in the stack

else if (infix[i] == '(') {

stack[++top] = infix[i];

// if the scanned character is ')'

// pop the stack and add it to the

// output string until empty or '(' found

else if (infix[i] == ')') {

while (top > -1 && stack[top] != '(')

postfix[j++] = stack[top--];

if (top > -1 && stack[top] != '(')

return "Invalid Expression";

else

top--;

// If the scanned character is an operator

// push it in the stack

else if (isOperator(infix[i])) {

while (top > -1 && precedence(stack[top]) >= precedence(infix[i]))

postfix[j++] = stack[top--];

stack[++top] = infix[i];

// Pop all remaining elements from the stack

while (top > -1) {

if (stack[top] == '(') {

return "Invalid Expression";

postfix[j++] = stack[top--];

postfix[j] = '\0';

return postfix;

// Driver code

int main()
{

char infix[MAX_EXPR_SIZE] = "a+b*(c^d-e)^(f+g*h)-i";

// Function call

char* postfix = infixToPostfix(infix);

if (strcmp(postfix, "Invalid Expression") == 0 || strcmp(postfix, "Memory Allocation Error") ==


0) {

printf("%s\n", postfix);

} else {

printf("%s\n", postfix);

free(postfix);

return 0;

OUTPUT:

3. #include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#define MAXSTACK 100 /* for max size of stack */

#define POSTFIXSIZE 100 /* define max number of characters in postfix expression */


/* declare stack and its top pointer to be used during postfix expression evaluation */

int stack[MAXSTACK];

int top = -1; /* because array index in C begins at 0 */

/* define push operation */

void push(int item)

if (top >= MAXSTACK - 1) {

printf("stack overflow\n");

exit(1);

else {

stack[++top] = item;

/* define pop operation */

int pop()

if (top < 0) {

printf("stack underflow\n");

exit(1);

else {

return stack[top--];

/* define function that is used to input postfix expression and to evaluate it */

void EvalPostfix(char postfix[])

int i;

char ch;

int val;

int A, B;

/* evaluate postfix expression */

for (i = 0; postfix[i] != '\0'; i++) {

ch = postfix[i];

if (isdigit(ch)) {

/* we saw an operand, push the digit onto stack

ch - '0' is used for getting digit rather than ASCII code of digit */

push(ch - '0');
}

else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {

/* we saw an operator

pop top element A and next-to-top element B

from stack and compute B operator A */

A = pop();

B = pop();

switch (ch) /* ch is an operator */

case '*':

val = B * A;

break;

case '/':

val = B / A;

break;

case '+':

val = B + A;

break;

case '-':

val = B - A;

break;

/* push the value obtained above onto the stack */

push(val);

printf("Result of expression evaluation: %d\n", pop());

int main()

char postfix[POSTFIXSIZE];

printf("ASSUMPTION: There are only four operators (*, /, +, -) in an expression and operand is
single digit only.\n");

printf("Enter postfix expression, end with ')' : ");

/* take input of postfix expression from user */

int i = 0;

char ch;
while (i < POSTFIXSIZE - 1 && (ch = getchar()) != ')') {

postfix[i++] = ch;

postfix[i] = '\0'; // Null-terminate the string

/* call function to evaluate postfix expression */

EvalPostfix(postfix);

return 0;

OUTPUT:

4. #include <stdio.h>

void towerOfHanoi(int n, char A, char B, char C)

if (n == 1)

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

return;
}

towerOfHanoi(n - 1, A, C, B);

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

towerOfHanoi(n - 1, B, A, C);

int main()

int n = 4; // Number of disks

towerOfHanoi(n, 'A', 'B', 'C'); // A, B, and C are names of rods

return 0;

OUTPUT:

5. #include <stdio.h>

#include <stdlib.h>

#define SIZE 100

void enqueue();

void dequeue();

void show();

int inp_arr[SIZE];

int Rear = -1;


int Front = -1;

void main()

int ch;

while (1)

printf("1. Enqueue Operation\n");

printf("2. Dequeue Operation\n");

printf("3. Display the Queue\n");

printf("4. Exit\n");

printf("Enter your choice of operation: ");

scanf("%d", &ch);

switch (ch)

case 1:

enqueue();

break;

case 2:

dequeue();

break;

case 3:

show();

break;

case 4:

exit(0);

default:

printf("Incorrect choice \n");

void enqueue()

int insert_item;

if (Rear == SIZE - 1)

printf("Overflow \n");

else

if (Front == -1)
Front = 0;

printf("Element to be inserted in the queue: ");

scanf("%d", &insert_item);

Rear = Rear + 1;

inp_arr[Rear] = insert_item;

void dequeue()

if (Front == -1 || Front > Rear)

printf("Underflow \n");

return;

else

printf("Element deleted from the Queue: %d\n", inp_arr[Front]);

Front = Front + 1;

void show()

if (Front == -1)

printf("Empty Queue \n");

else

printf("Queue: \n");

for (int i = Front; i <= Rear; i++)

printf("%d ", inp_arr[i]);

printf("\n");

Output:

You might also like