infix to prefix :
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
#include
<string.h> int
prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPrefix(char*
exp) { int len =
strlen(exp);
char result[len + 1]; // postfix (built in reverse
scan) char stack[len]; // operator stack
char final[len + 1]; // final
prefix int j = 0;
int top = -1;
for (int i = 1; i < len + 1; i+
+) { char c = exp[len - i];
if (isalnum(c))
{ result[j++] =
c;
}
else if (c == ')') {
stack[++top] = '(';
}
else if (c == '(') {
while (top != -1 && stack[top] != ')')
{ result[j++] = stack[top--];
}
top--;
}
else {
while (top != -1 && (prec(c) < prec(stack[top]) || (prec(c) == prec(stack[top]) && c !=
'^'))) { result[j++] = stack[top--];
}
stack[++top] = c;
}
}
while (top != -1) {
result[j++] =
stack[top--];
}
result[j] = '\0';
for (int k = 0; k < j; k++)
{ final[k] = result[j - k -
1];
}
final[j] = '\0';
printf("Prefix Expression: %s\n", final);
}
int main() {
char exp[100];
printf("Enter infix expression: ");
scanf("%99s", exp); // no spaces
allowed infixToPrefix(exp);
return 0;
}
2. infix to postfix :
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
#include
<string.h> int
prec(char c){
if (c=='^'){
return 3;
}else if (c== '*' || c=='/')
{ return 2 ;
}else if (c=='+' || c=='-')
{ return 1;
}else{
return -1;
}
}
void InfToPost(char*exp)
{ int length =
strlen(exp); char
result[length +1];
char stack[length];
int top = -1;
int j=0;
for(int i = 0 ; i<length;i+
+){ char c = exp[i];
if(isalnum(c)){
result[j++]=c;
}else if (c=='('){ stack[+
+top]=c;
}else if(c==')'){
while(top != -1 && stack[top]!
='('){ result[j+
+]=stack[top--];
}
top --;
}
else{
while (top != -1 && (prec(c) <= prec(stack[top]) && c != '^'))
{ result[j++] = stack[top--];
}
stack[++top]=c;
}
}
while(top!=-1){ result[j+
+]=stack[top--];
}
result[j]='\0';
printf("the final postfix expression is %s \n",result);
}
int main(){
char exp[100];
printf("enter the infix exp
:"); scanf("%99s", exp);
InfToPost(exp);
return 0;
}
3. tower of Hanoi:
#include
<stdio.h>
#include
<stdlib.h>
#define MAX_DISKS 64
typedef struct Stack {
int items[MAX_DISKS];
int top;
char name;
} Stack;
void push(Stack *s, int
item) { s->items[++(s-
>top)] = item;
}
int pop(Stack *s) {
if (s->top == -1) return 0;
return s->items[(s->top)--];
}
void moveDisk(Stack *src, Stack
*dest) { int pole1Top = pop(src);
int pole2Top = pop(dest);
if (pole1Top == 0) {
push(src, pole2Top);
printf("\n Move disk %d from rod %c to rod %c", pole2Top, dest->name, src->name);
} else if (pole2Top == 0)
{ push(dest, pole1Top);
printf("\n Move disk %d from rod %c to rod %c", pole1Top, src->name, dest->name);
} else if (pole1Top >
pole2Top) { push(src,
pole1Top); push(src,
pole2Top);
printf("\n Move disk %d from rod %c to rod %c", pole2Top, dest->name, src->name);
} else {
push(dest,
pole2Top);
push(dest,
pole1Top);
printf("\n Move disk %d from rod %c to rod %c", pole1Top, src->name, dest->name);
}
}
void towerOfHanoiIterative(int n, Stack *src, Stack *aux, Stack
*dest) { int moves = (1 << n) - 1;
Stack *s = src, *d = dest, *a = aux;
if (n % 2 == 0) {
Stack *temp =
d; d = a;
a = temp;
}
for (int i = n; i >= 1;
i--) push(s, i);
for (int i = 1; i <= moves; i+
+) { if (i % 3 == 1)
moveDisk(s, d);
else if (i % 3 == 2)
moveDisk(s, a);
else if (i % 3 == 0)
moveDisk(a, d);
}
}
int main() {
int n;
printf("Enter number of disks:
"); scanf("%d", &n);
Stack src = {.top = -1, .name = 'A'};
Stack aux = {.top = -1, .name =
'B'}; Stack dest = {.top = -1,
.name = 'C'};
towerOfHanoiIterative(n, &src, &aux, &dest);
return 0;
}
4. prefix eval :
#include
<stdio.h>
#include
<ctype.h>
#include
<string.h>
#include <math.h>
#define MAX 100
int
stack[MAX];
int top = -1;
void push(int val)
{ stack[++top] = val;
}
int pop() {
return stack[top--];
}
int evaluatePrefix(char*
exp) { int length =
strlen(exp);
int i = 0;
char ch;
for(int i = 0; i < length; i+
+) { ch = exp[length - 1 -
i];
if (isdigit(ch)) {
push(ch - '0');
} else {
int b =
pop(); int a
= pop();
switch (ch)
{
case '+': push(a + b);
break; case '-': push(a -
b); break; case '*':
push(a * b); break; case
'/': push(a / b); break;
case '^': push((int)pow(a, b)); break;
}
}
}
return pop();
}
int main() {
char postfix[MAX];
printf("Enter Postfix Expression:
"); scanf("%s", postfix);
int result =
evaluatePrefix(postfix);
printf("Result: %d\n", result);
return 0;
}
5. postfix eval :
#include
<stdio.h>
#include
<ctype.h>
#include <math.h>
#include
<string.h>
#define MAX 100
int
stack[MAX];
int top = -1;
void push(int val)
{ stack[++top] = val;
}
int pop() {
return stack[top--];
}
int evaluatePostfix(char*
exp) { int length =
strlen(exp);
int i = 0;
char ch;
for(int i=0; i<length; i+
+) { ch = exp[i];
if (isdigit(ch)) {
push(ch - '0'); // convert char to int
} else {
int b =
pop(); int a
= pop();
switch (ch) {
case '+': push(a + b);
break; case '-': push(a -
b); break; case '*':
push(a * b); break; case
'/': push(a / b); break;
case '^': push((int)pow(a, b)); break;
}
}
}
return pop();
}
int main() {
char
exp[MAX];
printf("Enter Postfix Expression:
"); scanf("%s", exp);
int result =
evaluatePostfix(exp);
printf("Result: %d\n",
result); return 0;
}
6. circular queue :
#include <stdio.h>
#define MAX 5 // keep small for testing
char circularQueue[MAX];
int front = -1, rear = -
1;
int enqueue(char c) {
// Correct overflow condition
if ((front == 0 && rear == MAX-1) || (rear + 1) % MAX == front) {
printf("Overflow\n");
return 0;
}
else if (front == -1) { // first
element front = rear = 0;
}
else if (rear == MAX-1 && front !=
0) { rear = 0; // wrap around
}
else {
rear = (rear + 1) % MAX;
}
circularQueue[rear] = c;
return 1;
}
void dequeue() {
if (front == -1) {
printf("Underflow\
n"); return;
}
printf("Dequeued: %c\n",
circularQueue[front]); if (front == rear) {
// only one element
front = rear = -1;
}
else {
front = (front + 1) % MAX;
}
}
void display() {
if (front == -1) {
printf("Queue is empty\
n"); return;
}
printf("Queue elements:
"); if (rear >= front) {
for (int i = front; i <= rear; i+
+) printf("%c ",
circularQueue[i]);
} else {
for (int i = front; i < MAX; i++)
printf("%c ",
circularQueue[i]);
for (int i = 0; i <= rear; i++)
printf("%c ",
circularQueue[i]);
}
printf("\n");
}
int main() {
int
choice;
char val;
while (1)
{
printf("\nChoose the operation:\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter character to enqueue: ");
scanf(" %c", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\
n"); return 0;
default:
printf("Invalid choice\n");
}
}
}
7. queue :
#include
<stdio.h>
#define MAX 100
char queue[MAX];
int front = -1;
int rear = -1;
void enqueue(char c) {
if (rear == MAX - 1) {
printf("Overflow\
n");
} else {
queue[++rear] = c;
}
}
void dequeue() {
if (front == rear) {
printf("Underflow\n");
} else {
front++;
}
}
int IsFull() {
if (rear == MAX - 1)
{ return 1;
} else {
return 0;
}
}
int IsEmpty() {
if (front == rear)
{ return 1;
} else {
return 0;
}
}
void display() {
if (IsEmpty()) {
printf("Queue is empty\n");
return;
}
for (int i = front + 1; i <= rear; i+
+) { printf("%c ", queue[i]);
}
printf("\n");
}
void clear() {
front = rear = -1;
}
void peek() {
if (IsEmpty()) {
printf("Queue is empty\n");
} else {
printf("Front element is: %c\n", queue[front + 1]);
}
}
int main() {
int
choice; do
{
printf("\nEnter the option:\
n" "1. Enqueue\n"
"2. Dequeue\n"
"3. IsFull\n"
"4. IsEmpty\n"
"5. Display\n"
"6. Clear\n"
"7. Peek\n"
"8. Exit\n"
"Choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
char c;
printf("Enter a character to enqueue: ");
scanf(" %c", &c);
enqueue(c);
break;
}
case 2:
dequeue();
break;
case 3:
if (IsFull()) {
printf("Queue is full\n");
} else {
printf("Queue is not full\n");
}
break
; case 4:
if (IsEmpty()) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
break
; case 5:
display();
break;
case 6:
clear();
printf("Queue cleared\
n"); break;
case 7:
peek()
;
break;
case 8:
printf("Exiting...\
n"); break;
default:
printf("Invalid option\n");
}
} while (choice != 8);
return 0;
}
8. stack :
#include
<stdio.h> #define
MAX 100 char
stack[MAX]; int
top=-1;
void push(char
c){
if(top==MAX-1){
printf("full");
}else{
stack[++top]=c;
}
}
char pop(){
if(top==-1)
{
printf("empty");
}else{
return stack[top--];
}
}
char peek(){
if (top==-1){
printf("empty");
}else{
return stack[top];
}
}
int isFull(){
return top==MAX-1;
}
int isEmpty(){
return top==-
1;
}
int main(){
int choice;
printf("1. Push\n2. Pop\n3. Peek\n4. Check Full\n5. Check
Empty\n"); scanf("%d", &choice);
switch(choice
){ case 1:
{
char c;
printf("Enter a character to push:
"); scanf(" %c", &c);
push(c);
break;
}
case 2:{
char c = pop();
if(c != '\0'){
printf("Popped: %c\n", c);
}
break;
}
case 3:{
char c = peek();
if(c != '\0'){
printf("Top element: %c\n", c);
}
break;
}
case 4:{
if(isFull()){
printf("Stack is full\n");
}else{
printf("Stack is not full\n");
}
break;
}
case 5:{
if(isEmpty()){
printf("Stack is empty\n");
}else{
printf("Stack is not empty\n");
}
break;
}
default:{
printf("Invalid choice\n");
}
}
return 0;
}
9. linked list :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
struct node* next;
};
struct node* createNode (int data)
{
struct node* newNode = (struct node*)malloc(sizeof(struct
node)); newNode->data = data;
newNode->next =
NULL; return
newNode;
};
void intsertFirst(struct node** head, int
data){ struct node* newNode =
createNode(data); newNode->next = *head;
*head = newNode;
};
void intsertAtEnd(struct node** head, int
data){ struct node* newNode =
createNode(data); if(*head == NULL){
*head = newNode;
return;
}
struct node* temp =
*head; while(temp->next
!= NULL){
temp = temp->next;
}
temp->next = newNode;
};
void intsertAtPosition(struct node** head, int data, int
position){ struct node* newNode = createNode(data);
if(position == 0){
intsertFirst(head, data);
return;
}
struct node* temp = *head;
while(position-1 >1 && temp!=
NULL){ temp= temp->next;
position--;
}
if(temp == NULL){
printf("Position out of range \
n"); free(newNode);
return;
}
newNode->next = temp-
>next; temp-
>next=newNode;
};
void deleteAtFirst(struct node**
head){ if(*head == NULL){
printf("List is Empty \
n"); return;
}
struct node* temp = *head;
*head = temp-
>next;
free(temp);
};
void deleteAtLast(struct node**
head){ if(*head == NULL){
printf("List is Empty \
n"); return;
}
struct node* temp =
*head; if(temp->next
==NULL){ free(temp);
*head = NULL;
}
while(temp->next->next != NULL)
{ temp = temp->next;
}
free(temp-
>next); temp-
>next= NULL;
};
void deleteAtPosition(struct node** head, int position)
{ if(*head == NULL){
printf("List is Empty \
n"); return;
}
if(position == 0){
deleteAtFirst(head);
return;
}
struct node* temp = *head;
while(position-1 >1 && temp!=
NULL){ temp= temp->next;
position--;
}
if(temp == NULL || temp->next ==
NULL){ printf("Position out of
range \n"); return;
}
struct node* next = temp->next-
>next; free(temp->next);
temp->next= next;
}
void display(struct node*
head){ struct node* temp =
head; while(temp != NULL){
printf("%d -> ", temp-
>data); temp = temp->next;
}
};
bool search(struct node* head, int
element){ struct node* temp = head;
while(temp != NULL){
if(temp->data ==
element){ return true;
}
temp = temp->next;
}
return false;
};
void findDuplicate(struct node* head){
int freq[100]= {0}; // assuming that list containing node values from 0
to 99 struct node* temp = head;
while(temp != NULL){
if(freq[temp->data!=0])
{ printf("Found
duplicate\n");
printf(temp->data);
return;
}else{
freq[temp-
>data]=1; temp=
temp->next;
}
}
printf("List don't have any duplicate element");
}
int main (){
struct node* head = NULL;
deleteAtFirst(&head);
deleteAtLast(&head);
deleteAtPosition(&head, 2);
intsertFirst(&head, 10);
intsertFirst(&head, 20);
intsertAtEnd(&head, 30);
intsertAtPosition(&head, 44, 3);
intsertFirst(&head, 10);
intsertFirst(&head, 20);
intsertAtEnd(&head, 30);
intsertAtPosition(&head, 44, 3);
deleteAtFirst(&head);
deleteAtLast(&head);
deleteAtPosition(&head, 2);
bool isPresnet = search(head, 44);
printf("\n");
// if(isPresnet){
// printf("Element found!");
// }else{
// printf("Element is not found in the list!");
// }
findDuplicate(head
); printf("\n");
display(head);
return 0;
}