You are on page 1of 16

INFIX TO POSTFIX

#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>

#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1;
int rear = -1;

void enqueue(int value) {


if (rear == MAX_SIZE - 1) {
printf("Queue is full. Cannot enqueue %d\n", value);
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("Enqueued: %d\n", value);
}
}

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() {

printf("enter the element to enter");


enqueue(1);
enqueue(2);
enqueue(3);

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 enqueue(int item) {


if ((rear + 1) % MAX_SIZE == front) {
printf("Queue is full. Cannot enqueue element.\n");
} else {
if (front == -1) {
front = 0;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = item;
printf("Enqueued: %d\n", item);
}
}

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>

// Define a structure for a node in the linked list


struct Node {
int data; // Data of the node
struct Node* next; // Pointer to the next node
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new node at the end of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to print the linked list


void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function to test the linked list implementation


int main() {
struct Node* head = NULL; // Initialize an empty linked list
insertNode(&head, 1); // Insert nodes
insertNode(&head, 2);
insertNode(&head, 3);
printf("Linked List: ");
printList(head); // Print the linked list
return 0;
}
BINARY SEARCH TREE 10

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

struct Node {
int data;
struct Node *left, *right;
};

struct Node* createNode(int value) {


struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int value) {


if (!root) return createNode(value);
if (value < root->data) root->left = insert(root->left, value);
else if (value > root->data) root->right = insert(root->right, value);
return root;
}

void inorder(struct Node* root) {


if (root) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct Node* root) {


if (root) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

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]);
}

printf("Inorder: "); inorder(root); printf("\n");


printf("Preorder: "); preorder(root); printf("\n");
printf("Postorder: "); postorder(root); printf("\n");

return 0;
}
AVL 11

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

// AVL Node structure


struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};

// Function to get the height of a node


int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
}

// Function to get the maximum of two integers


int max(int a, int b) {
return (a > b) ? a : b;
}

// Function to create a new AVL node


struct Node* newNode(int key) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // New node is initially at height 1
return node;
}

// Function to right rotate subtree rooted with y


struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;

// 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;
}

// Function to left rotate subtree rooted with x


struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* T2 = y->left;

// 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;

// Return new root


return y;
}

// Get balance factor of a node


int getBalance(struct Node* N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

// Function to insert a key into AVL tree


struct Node* insert(struct Node* node, int key) {
// Perform the normal BST insertion
if (node == NULL)
return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Duplicate keys not allowed
return node;

// Update height of current node


node->height = 1 + max(height(node->left), height(node->right));

// Get the balance factor of this node


int balance = getBalance(node);

// Left Left Case


if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case


if (balance < -1 && key > node->right->key)
return leftRotate(node);

// Left Right Case


if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case


if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

// Return the (unchanged) node pointer


return node;
}

// Function to print inorder traversal of AVL tree


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

// Driver program
int main() {
struct Node* root = NULL;

root = insert(root, 10);


root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 15);
root = insert(root, 5);

printf("Inorder traversal of the constructed AVL tree: ");


inorder(root);

return 0;
}
CIRCULAR SINGLY LINKED LIST:
#include <stdio.h>
#include <stdlib.h>

// Node structure for circular singly linked list


struct Node {
int data;
struct Node* next;
};

// 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
}

// Function to display the circular linked list


void displayList(struct Node* last) {
if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = last->next; // Start from the first node
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != last->next); // Stop when we reach the first node again
printf("\n");
}

// Main function
int main() {
struct Node* last = NULL;

// Insert nodes into the circular linked list


last = insertNode(last, 1);
last = insertNode(last, 2);
last = insertNode(last, 3);
// Display the circular linked list
printf("Circular Linked List: ");
displayList(last);

return 0;
}
CIRCULAR DOUBLY LLINKED LIST
CODE:1
#include <stdio.h>
#include <stdlib.h>

// Node structure for circular doubly linked list


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// 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
}

// Function to display the circular doubly linked list in forward direction


void displayForward(struct Node* last) {
if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = last->next;
do {
printf("%d <-> ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("\n");
}

// Function to display the circular doubly linked list in reverse direction


void displayBackward(struct Node* last) {
if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = last;
do {
printf("%d <-> ", temp->data);
temp = temp->prev;
} while (temp != last);
printf("\n");
}

// Main function
int main() {
struct Node* last = NULL;

// Insert nodes into the circular doubly linked list


last = insertNode(last, 1);
last = insertNode(last, 2);
last = insertNode(last, 3);

// Display the circular doubly linked list in forward direction


printf("Circular Doubly Linked List (Forward): ");
displayForward(last);

// Display the circular doubly linked list in reverse direction


printf("Circular Doubly Linked List (Backward): ");
displayBackward(last);

return 0;
}

CODE 2:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
struct Node* prev;
};

struct Node* insertNode(struct Node* last, int data) {


struct Node* new_node = malloc(sizeof(struct Node));
new_node->data = data;
if (last == NULL) {
new_node->next = new_node->prev = new_node;
} else {
new_node->next = last->next;
new_node->prev = last;
last->next->prev = new_node;
last->next = new_node;
}
return new_node;
}

void displayList(struct Node* last) {


if (last == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = last->next;
do {
printf("%d <-> ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("\n");
}

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;
}

You might also like