Ds 3-7 Lab Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

3.

Develop a menu driven Program in C for the following operations on STACK of Integers

(Array Implementation of Stack with maximum size MAX)

a. Push an Element on to Stack

b. Pop an Element from Stack

c. Demonstrate how Stack can be used to check Palindrome

d. Demonstrate Overflow and Underflow situations on Stack

e. Display the status of Stack

f. Exit

Support the program with appropriate functions for each of the above operations

#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 5

int top = -1;


int stack[10];

void push(int item)


{
if (top == STACK_SIZE - 1)
{
printf("Overflow of stack\n");
return;
}
top++;
stack[top] = item;
}

void pop()
{
if (top == -1)
{
printf("Stack underflow\n");
return;
}

printf("Item deleted = %d\n", stack[top]);


top = top - 1;
}

void display()
{
int i;

if (top == -1)
{
printf("Stack is empty\n");
return;
}

printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d ", stack[i]);

printf("\n");
}

void palindrome(char str[])


{
int i;

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


{
stack[++top] = str[i];
}

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


{
if(str[i] == stack[top--])
continue;
printf("%s: Not a palindrome\n",str);
return;
}

printf("%s: Palindrome\n",str);
}

int main()
{
int item, choice;
char str[10];
for (;;)
{
printf("1: Insert 2: Delete 3: Display 4:Palindrome 5: Exit : ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter the item: ");
scanf("%d", &item);
push(item);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
printf("Enter the string: ");
scanf(" %[^\n]", str);
palindrome(str);
break;

default:
exit(0);
}
}
}

4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.

#include <stdio.h>
#include <string.h>

int F (char symbol)


{
switch(symbol)
{
case '#' : return -1;

case '+' :
case '-' : return 2;

case '*' :
case '/' :
case '%' : return 4;

case '^' :
case '$' : return 5;

case '(' : return 0;

default : return 8;
}
}

int G (char symbol)


{
switch(symbol)
{
case ')' : return 0;

case '+' :
case '-' : return 1;

case '*' :
case '/' :
case '%' : return 3;

case '^' :
case '$' : return 6;

case '(' : return 7;

default : return 9;
}
}
void infix_2_postfix(char infix[], char postfix[])
{
int i, j = 0, top = -1;
char s[20];

s[++top] = '#';
for (i = 0; infix[i] != '\0'; i++)
{
while ( F( s[top] ) > G( infix[i] ) )
postfix[j++] = s[top--];

if ( F( s[top] ) != G( infix[i] ) )
s[++top] = infix[i];
else
s[top--];
}

while (s[top] != '#')


{
postfix[j++] = s[top--];
}

postfix[j] = '\0';
}

void main()
{
char infix[20], postfix[20];

printf("Infix :");
scanf("%[^\n]", infix);

infix_2_postfix(infix, postfix);

printf("Postfix:");
printf("%s\n", postfix);
}

5. Develop a Program in C for the following Stack Applications


a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double compute(double operand1, char operator, double operand2) {


switch (operator) {
case '+': return operand1 + operand2;
case '-': return operand1 - operand2;
case '*': return operand1 * operand2;
case '/': return operand1 / operand2;
case '%': return fmod(operand1, operand2);
case '^': return pow(operand1, operand2);
default:
exit(0);
}
}

double evaluate(char postfix[]) {


int i, top = -1;
double stack[20], operand1, operand2;

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


if (postfix[i] >= '0' && postfix[i] <= '9') {
stack[++top] = postfix[i] - '0';
} else {
operand2 = stack[top--];
operand1 = stack[top--];
stack[++top] = compute(operand1, postfix[i], operand2);
}
}

return stack[top--];
}

int main() {
char postfix[20];
double result;

printf("Enter Postfix expression: ");


scanf("%s", postfix);

result = evaluate(postfix);

printf("Result = %lf\n", result);

return 0;
}

b. Solving Tower of Hanoi problem with n disks.


6. Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters
(Array Implementation of Queue with maximum size MAX)

a. Insert an Element on to Circular QUEUE

b. Delete an Element from Circular QUEUE

c. Demonstrate Overflow and Underflow situations on Circular QUEUE

d. Display the status of Circular QUEUE e. Exit Support the program with appropriate functions for
each of the above operations.

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int queue[10], front = 0, rear = -1, count = 0;

void insert_rear(int item)


{
if (count == MAX)
{
printf("Q overflow\n");
return;
}

rear = (rear + 1) %MAX;


queue[rear] = item;

count++;
}

void delete_front()
{
if (count == 0)
{
printf("Q underFlow\n");
return;
}

printf("ITem deleted = %d\n", queue[front]);


front = (front + 1) % MAX;

count--;
}

void display()
{
int i, temp;
if (count == 0)
{
printf("Q is empty\n");
return;
}

printf("Queue : ");
temp = front;
for (i = 1; i <= count; i++)
{
printf("%d ", queue[temp]);
temp = (temp + 1) % MAX;
}

printf("\n");
}

void main()
{
int item, choice;

for(;;)
{
printf("1: insert 2:Delete 3:Display 4:Exit : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the item : ");
scanf("%d", &item);
insert_rear(item);
break;

case 2:
delete_front();
break;

case 3:
display();
break;

default:
exit(0);
}
}
}
7. Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of
Student Data with the fields: USN, Name, Programme, Sem, PhNo a. Create a SLL of N Students Data
by using front insertion. b. Display the status of SLL and count the number of nodes in it c. Perform
Insertion / Deletion at End of SLL d. Perform Insertion / Deletion at Front of SLL(Demonstration of
stack) e. Exit

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
char usn[25];
char name[25];
char programme[25];
char phone[12];
int sem;
struct node*link;
};

typedef struct node *NODE;

NODE getnode()
{
NODE x;

x = (NODE) malloc (sizeof(struct node));

if(x == NULL)
{
printf("not enough memory");
exit(0);
}

return x;
}

NODE insert_front(char usn[], char name[], char programme[],char phone[], int sem, NODE first)
{
NODE temp;

temp = getnode();

strcpy(temp ->usn, usn);


strcpy(temp ->name,name);
strcpy(temp ->programme, programme);
strcpy(temp ->phone, phone);
temp -> sem = sem;

temp -> link = first;

return temp;
}

NODE insert_rear(char usn[], char name[], char programme[], char phone[], int sem, NODE first)
{
NODE temp, cur;

temp = getnode();

strcpy(temp ->usn, usn);


strcpy(temp ->name,name);
strcpy(temp ->programme, programme);
strcpy(temp ->phone, phone);
temp -> sem = sem;

temp -> link = NULL;

if (first == NULL) return temp;

cur = first;
while(cur -> link != NULL)
{
cur=cur -> link;
}

cur -> link = temp;

return first;

NODE delete_rear(NODE first)


{
NODE prev,cur;

if(first == NULL)
{
printf("Student list is empty\n");

return NULL;
}

if(first -> link == NULL)


{
printf("The student with usn %s is deleted\n " ,first ->usn);
free(first);

return NULL;
}

cur = first;
while(cur -> link != NULL)
{
prev = cur;
cur = cur -> link;
}

prev -> link = NULL;

printf("The student with usn %s is deleted\n", cur -> usn);


free(cur);

return first;
}

NODE delete_front(NODE first)


{
NODE prev, cur;

if( first == NULL )


{
printf("students list is empty\n");

return NULL;
}

if(first -> link == NULL)


{
printf("The student with usn %s is deleted\n", first -> usn);
free(first);
return NULL;
}

cur = first -> link;

printf(" The student with usn %s is deleted\n", first -> usn);


free(first);

return cur;
}

void display(NODE first)


{
NODE cur;

printf("Student list\n");

printf("usn name programme phone sem");

if (first == NULL)
{
printf( "empty");

return;
}

cur = first;

while(cur != NULL)
{
printf("\n%s", cur -> usn);
printf("%10s", cur -> name);
printf("%10s", cur -> programme);
printf("%10s", cur -> phone);
printf("%10d", cur -> sem);

cur = cur -> link;

printf("\n");

}
void main()
{
char usn[25], name[25],programme[25], phone[12];
int choice, sem;
NODE first;

for(;;)
{
printf("\n\n 1:Front insert 2:rear insert\n");
printf("1:Front delete 2:rear delete\n");
printf("5: Display 6:exit\n");
printf("Enter your choice");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("USN: ");
scanf("%s",usn);
printf("Name: ");
scanf("%s", name);
printf("programme: ");
scanf("%s", programme);
printf("phone: ");
scanf("%s", phone);
printf("sem: ");
scanf("%d", &sem);

first = insert_front(usn, name, programme, phone, sem, first);


break;

case 2:
printf("USN : "); scanf("%s", usn);
printf("Name : "); scanf("%s", name);
printf("Progrm : "); scanf("%s", programme);
printf("Phone : "); scanf("%s", phone);
printf("Sem : "); scanf("%d", &sem);

first = insert_rear(usn, name, programme, phone, sem, first);


break;

case 3:
first = delete_front(first);
break;

case 4:

first = delete_rear(first);
break;

case 5:
display(first);
break;

default:
exit(0);

}
}
}

You might also like