Professional Documents
Culture Documents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}
int precedence(char ch) {
switch(ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return 0;
}
void infixToPostfix(char* infix) {
char postfix[100];
int top = -1;
for (int i = 0; infix[i]; ++i) {
if (isOperator(infix[i])) {
while (top >= 0 && precedence(postfix[top]) >= precedence(infix[i])) {
printf("%c", postfix[top--]);
}
postfix[++top] = infix[i];
} else {
printf("%c", infix[i]);
}
}
while (top >= 0) {
printf("%c", postfix[top--]);
}
}
int main() {
char infix[100];
printf("Enter infix expression: ");
scanf("%s", infix);
printf("Postfix expression: ");
infixToPostfix(infix);
return 0;
}
LINEAR QUEUE USING ARRAY:
#include <stdio.h>
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void dequeue() {
if (front == -1) {
printf("Queue is empty. Cannot dequeue\n");
} else {
printf("Dequeued: %d\n", queue[front]);
}
front++;
if (front > rear) {
front = rear = -1;
}
}
}
int main() {
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}
CIRCULAR QUEUE USING ARRAY:
#include <stdio.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1, rear = -1;
void dequeue() {
if (front == -1) {
printf("Queue is empty. Cannot dequeue element.\n");
} else {
printf("Dequeued: %d\n", queue[front]);
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}
}
void display() {
int i;
if (front == -1) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (i = front; i != rear; i = (i + 1) % MAX_SIZE) {
printf("%d ", queue[i]);
}
printf("%d\n", queue[i]);
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
display();
dequeue();
display();
enqueue(4);
enqueue(5);
enqueue(6); // This will overflow the queue
display();
dequeue();
dequeue();
dequeue();
dequeue(); // This will underflow the queue
return 0;
}
SINGLY LINKED LIST:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
int main() {
struct Node* root = NULL;
int values[] = {5, 3, 7, 1, 4, 6, 8};
for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
root = insert(root, values[i]);
}
return 0;
}
AVL 11
#include <stdio.h>
#include <stdlib.h>
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Return new root
return x;
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Driver program
int main() {
struct Node* root = NULL;
return 0;
}
CIRCULAR SINGLY LINKED LIST:
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node at the beginning of the circular linked list
struct Node* insertNode(struct Node* last, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
if (last == NULL) {
new_node->next = new_node; // For the first node, make it point to itself
last = new_node;
} else {
new_node->next = last->next; // New node points to the first node
last->next = new_node; // Last node's next points to the new node
last = new_node; // Update last to the new node
}
return last; // Return the updated last pointer
}
// Main function
int main() {
struct Node* last = NULL;
return 0;
}
CIRCULAR DOUBLY LLINKED LIST
CODE:1
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node at the beginning of the circular doubly linked list
struct Node* insertNode(struct Node* last, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
if (last == NULL) {
new_node->next = new_node;
new_node->prev = new_node;
last = new_node;
} else {
new_node->next = last->next;
last->next->prev = new_node;
last->next = new_node;
new_node->prev = last;
}
return last; // Return the updated last pointer
}
// Main function
int main() {
struct Node* last = NULL;
return 0;
}
CODE 2:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
int main() {
struct Node* last = NULL;
last = insertNode(last, 1);
last = insertNode(last, 2);
last = insertNode(last, 3);
printf("Circular Doubly Linked List: ");
displayList(last);
return 0;
}