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

Single Linked List

The document discusses different data structures for representing queues. It describes implementing queues using arrays as well as linked lists. Functions for initialization, checking empty/full status, enqueue, dequeue and peek operations are demonstrated for both array-based and linked list-based queues.

Uploaded by

armanmund27
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)
10 views

Single Linked List

The document discusses different data structures for representing queues. It describes implementing queues using arrays as well as linked lists. Functions for initialization, checking empty/full status, enqueue, dequeue and peek operations are demonstrated for both array-based and linked list-based queues.

Uploaded by

armanmund27
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/ 45

Single Linked List

#include<stdio.h>

#include<stdlib.h>

struct node{

int key;

struct node* next;

};

struct node* newnode(int key){

struct node* temp = (struct node*)malloc(sizeof(struct node));

temp->key = key;

temp->next = NULL;

return temp;

void insert_at_beginning(struct node** head,int key){

struct node* temp = newnode(key);

temp->next= *head;

*head = temp;

void insert_at_end(struct node** head,int key){

struct node* temp = newnode(key);

if(*head = NULL){

*head = temp;

return 0;

struct node* last = *head;

while(last->next != NULL){

last = last->next;

last->next = temp;

}
void insert_after(struct node* prev,int key){

if(prev == NULL){

printf("Not found.");

return;

struct node* temp = newnode(key);

temp->next = prev->next;

prev->next = temp;

struct node* find_key(struct node* head,int key){

struct node* temp=head;

while(temp!=NULL){

if(temp->key == key){

return temp;

temp= temp->next;

return NULL;

void delete_key(struct node** head,int key){

struct node* temp = *head;

struct node* prev = NULL;

if(temp != NULL && temp->key == key){

*head = temp->next;

free(temp);

void print_list(struct node* head){

struct node* temp= head;

while(temp!= NULL){

printf("%d\t",temp->key);
temp= temp->next;

int main(){

struct node* head = NULL;

int key[] = {124,544,87,256,785,12,356,65};

int n = sizeof(key)/sizeof(key[0]);

for(int i=0;i<n;i++){

insert_at_end(&head,key[i]);

printf("The initial list:\n");

print_list(head);

insert_at_beginning(&head,111);

insert_at_end(&head,999);

struct node* prev = find_key(head,87);

insert_after(prev,555);

delete_key(&head,87);

delete_key(&head,356);

delete_key(&head,124);

printf("The updated list:\n");

print_list(head);

Double linked list


#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node* prev;

struct node* next;

};
struct node* createnode(int data){

struct node* newnode = (struct node*)malloc(sizeof(struct node));

newnode->data = data;

newnode->prev = NULL;

newnode->next = NULL;

return newnode;

void insert_at_beginning(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

else{

newnode->next = *head;

(*head)->prev = newnode;

*head= newnode;

void insert_at_end(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

else{

struct node* current = *head;

while(current->next != NULL){

current = current->next;

current->next = newnode;

newnode->prev = current;

}
}

int search(struct node* head,int key){

struct node* current = head;

while(current!= NULL){

if(current->data == key){

return 1;

current = current->next;

return 0;

void delete_node(struct node** head,int key){

struct node* current = *head;

while(current!= NULL && current->data != key){

current = current->next;

if(current == NULL){

printf("Element not found.\n");

if(current->prev != NULL){

current->prev->next = current->next;

else{

*head = current->next;

if(current->next != NULL){

current->next->prev = current->prev;

free(current);

void print_list(struct node* head){


struct node* current = head;

printf("Linked list\n");

while(current!=NULL){

printf("%d\t",current->data);

current= current->next;

void print_opp(struct node* tail){

printf("Traversal from back:\n");

while(tail->next != NULL){

tail= tail->next;

while(tail!=NULL){

printf("%d\t",tail->data);

tail = tail->prev;

printf("\n");

int main(){

struct node* head = NULL;

insert_at_end(&head,6);

insert_at_end(&head,8);

insert_at_end(&head,55);

insert_at_end(&head,22);

insert_at_beginning(&head,2);

insert_at_end(&head,5);

if(search(head,8)){

printf("Element 8 is present.\n");

else{

printf("Element 8 is not present.\n");


}

delete_node(&head,55);

print_list(head);

print_opp(head);

return 0;

Circular linked list


#include<stdio.h>

#include<stdlib.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 insert_at_beginning(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

newnode->next = newnode;

else{

struct node* last = (*head)->next;

while(last->next != *head){

last = last->next;

}
last->next = newnode;

newnode->next = *head;

*head = newnode;

void insert_at_end(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

newnode->next = newnode;

else{

struct node* last = (*head)->next;

while(last->next != *head){

last = last->next;

last->next = newnode;

newnode->next= *head;

int search(struct node* head,int key){

struct node* current = head;

if(current == NULL){

return 0;

do{

if(current->data == key)

return 1;

current = current->next;

while(current!= head);
return 0;

void delete_node(struct node** head,int key){

if(*head == NULL){

return;

struct node* current = *head;

struct node* prev = NULL;

while(current->data != key){

if(current->next == *head){

printf("Element not found.\n");

return;

prev = current;

current = current->next;

if(current->next == *head && prev == NULL){

*head = NULL;

free(current);

return;

if(current == *head){

prev = (*head)->next;

while(prev->next != *head)

prev = prev->next;

*head = (*head)->next;

prev->next = *head;

free(current);

else if(current->next == *head){

prev->next = *head;
free(current);

else{

prev->next = current->next;

free(current);

void print_list(struct node* head){

struct node* current = head;

if(current == NULL)

return;

do{

printf("%d\t",current->data);

current= current->next;

}while(current!= head);

printf("\n");

int main(){

struct node* head = NULL;

insert_at_end(&head,6);

insert_at_end(&head,8);

insert_at_end(&head,55);

insert_at_end(&head,22);

insert_at_beginning(&head,2);

insert_at_end(&head,5);

if(search(head,8)){

printf("Element 8 is present.\n");

}else{

printf("Element 8 is not present.\n");

delete_node(&head,55);
printf("Linked list:");

print_list(head);

return 0;

Queue in array
#include <stdio.h>

#define MAX_SIZE 100

// Structure to represent queue

struct Queue {

int arr[MAX_SIZE];

int front, rear;

};

// Function to initialize queue

void initialize(struct Queue *q) {

q->front = -1;

q->rear = -1;

// Function to check if queue is empty

int isEmpty(struct Queue *q) {

return (q->front == -1 && q->rear == -1);

// Function to check if queue is full

int isFull(struct Queue *q) {

return (q->rear == MAX_SIZE - 1);

}
// Function to enqueue element into queue

void enqueue(struct Queue *q, int data) {

if (isFull(q)) {

printf("Queue is full\n");

return;

if (isEmpty(q)) {

q->front = q->rear = 0;

} else {

q->rear++;

q->arr[q->rear] = data;

// Function to dequeue element from queue

int dequeue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

int dequeuedData = q->arr[q->front];

if (q->front == q->rear) {

q->front = q->rear = -1;

} else {

q->front++;

return dequeuedData;

// Function to peek at the front element of queue

int peek(struct Queue *q) {


if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

return q->arr[q->front];

void printQueue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return;

int i = q->front;

printf("Queue: ");

while (i != q->rear) {

printf("%d ", q->arr[i]);

i = (i + 1) % MAX_SIZE;

printf("%d\n", q->arr[i]);

int main() {

struct Queue q;

initialize(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

printf("Front element: %d\n", peek(&q));

printf("Dequeued element: %d\n", dequeue(&q));


printf("Dequeued element: %d\n", dequeue(&q));

printf("Front element: %d\n", peek(&q));

printQueue(&q);

return 0;

Queue using linked list


#include <stdio.h>

#include <stdlib.h>

// Structure to represent a node in the linked list

struct Node {

int data;

struct Node* next;

};

// Structure to represent queue

struct Queue {

struct Node* front;

struct Node* rear;

};

// Function to initialize queue

void initialize(struct Queue* q) {

q->front = NULL;

q->rear = NULL;

// Function to check if queue is empty

int isEmpty(struct Queue* q) {


return (q->front == NULL && q->rear == NULL);

// Function to enqueue element into queue

void enqueue(struct Queue* q, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed\n");

return;

newNode->data = data;

newNode->next = NULL;

if (isEmpty(q)) {

q->front = q->rear = newNode;

} else {

q->rear->next = newNode;

q->rear = newNode;

// Function to dequeue element from queue

int dequeue(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

struct Node* temp = q->front;

int dequeuedData = temp->data;

q->front = temp->next;

free(temp);

if (q->front == NULL) {
q->rear = NULL;

return dequeuedData;

// Function to peek at the front element of queue

int peek(struct Queue* q) {

if (isEmpty(q)) {

printf("Queue is empty\n");

return -1;

return q->front->data;

void printQueue(struct Queue* q) {

struct Node* temp = q->front;

if (temp == NULL) {

printf("Queue is empty\n");

return;

printf("Queue: ");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

struct Queue q;

initialize(&q);
enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

printf("Front element: %d\n", peek(&q));

printf("Dequeued element: %d\n", dequeue(&q));

printf("Dequeued element: %d\n", dequeue(&q));

printf("Front element: %d\n", peek(&q));

printQueue(&q);

return 0;

Stack using array


#include <stdio.h>

#define MAX_SIZE 100

// Structure to represent stack

struct Stack {

int arr[MAX_SIZE];

int top;

};

// Function to initialize stack

void initialize(struct Stack *s) {

s->top = -1;

// Function to check if stack is empty

int isEmpty(struct Stack *s) {


return (s->top == -1);

// Function to check if stack is full

int isFull(struct Stack *s) {

return (s->top == MAX_SIZE - 1);

// Function to push element onto stack

void push(struct Stack *s, int data) {

if (isFull(s)) {

printf("Stack Overflow\n");

return;

s->arr[++s->top] = data;

// Function to pop element from stack

int pop(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack Underflow\n");

return -1;

return s->arr[s->top--];

// Function to peek at the top element of stack

int peek(struct Stack *s) {

if (isEmpty(s)) {

printf("Stack is empty\n");

return -1;
}

return s->arr[s->top];

void print_stack(Stack *stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return;

printf("Stack: ");

for (int i = 0; i <= stack->top; i++) {

printf("%d ", stack->arr[i]);

printf("\n");

int main() {

struct Stack s;

initialize(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

printf("Top element: %d\n", peek(&s));

printf("Popped element: %d\n", pop(&s));

printf("Popped element: %d\n", pop(&s));

printf("Top element: %d\n", peek(&s));

print_stack(&s);

return 0;

}
Stack using linked list
#include <stdio.h>

#include <stdlib.h>

// Structure to represent a node in the linked list

struct Node {

int data;

struct Node* next;

};

// Structure to represent stack

struct Stack {

struct Node* top;

};

// Function to initialize stack

void initialize(struct Stack* s) {

s->top = NULL;

// Function to check if stack is empty

int isEmpty(struct Stack* s) {

return (s->top == NULL);

// Function to push element onto stack

void push(struct Stack* s, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed\n");


return;

newNode->data = data;

newNode->next = s->top;

s->top = newNode;

// Function to pop element from stack

int pop(struct Stack* s) {

if (isEmpty(s)) {

printf("Stack Underflow\n");

return -1;

struct Node* temp = s->top;

int poppedData = temp->data;

s->top = temp->next;

free(temp);

return poppedData;

// Function to peek at the top element of stack

int peek(struct Stack* s) {

if (isEmpty(s)) {

printf("Stack is empty\n");

return -1;

return s->top->data;

void printStack(struct Stack* s) {

struct Node* current = s->top;

printf("Stack: ");
while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

printf("NULL\n");

int main() {

struct Stack s;

initialize(&s);

push(&s, 10);

push(&s, 20);

push(&s, 30);

printf("Top element: %d\n", peek(&s));

printf("Popped element: %d\n", pop(&s));

printf("Popped element: %d\n", pop(&s));

printf("Top element: %d\n", peek(&s));

printStack(&s);

return 0;

Linear search
#include <stdio.h>

// Function to perform linear search

int linearSearch(int arr[], int n, int key) {

for (int i = 0; i < n; i++) {

if (arr[i] == key) {
return i; // Return the index if the key is found

return -1; // Return -1 if the key is not found

int main() {

int arr[] = {12, 45, 78, 34, 23, 56, 90};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 34;

// Perform linear search

int index = linearSearch(arr, n, key);

// Check if the key is found or not

if (index != -1) {

printf("Element %d found at index %d.\n", key, index);

} else {

printf("Element %d not found in the array.\n", key);

return 0;

Binary Search
#include <stdio.h>

// Function to perform binary search

int binarySearch(int arr[], int left, int right, int target) {

while (left <= right) {

int mid = left + (right - left) / 2;


// Check if target is present at mid

if (arr[mid] == target)

return mid;

// If target greater, ignore left half

if (arr[mid] < target)

left = mid + 1;

// If target is smaller, ignore right half

else

right = mid - 1;

// If target is not found

return -1;

// Main function

int main() {

int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

int target = 12;

int n = sizeof(arr) / sizeof(arr[0]);

int result = binarySearch(arr, 0, n - 1, target);

if (result == -1)

printf("Element is not present in array\n");

else

printf("Element is present at index %d\n", result);

return 0;

}
Bubble sort
#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap if the element found is greater than the next element

swap(&arr[j], &arr[j + 1]);

// Print the array after each pass

printf("After Pass %d: ", i + 1);

for (int k = 0; k < n; k++) {

printf("%d ", arr[k]);

printf("\n");

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);


printf("Initial array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

bubbleSort(arr, n);

printf("\nSorted array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

return 0;

Quick Sort
#include <stdio.h>

// Function to swap two elements

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

// Function to partition the array and return the pivot index

int partition(int arr[], int low, int high) {

int pivot = arr[high]; // Pivot element

int i = low - 1; // Index of smaller element


for (int j = low; j < high; j++) {

// If current element is smaller than or equal to pivot

if (arr[j] <= pivot) {

i++; // Increment index of smaller element

swap(&arr[i], &arr[j]); // Swap elements at i and j

swap(&arr[i + 1], &arr[high]); // Swap pivot with element at i+1

return i + 1;

// Function to implement Quick Sort algorithm

void quickSort(int arr[], int low, int high) {

if (low < high) {

// Partitioning index

int pi = partition(arr, low, high);

// Sort elements before partition and after partition

printf("Intermediate array after partitioning: \n");

for (int i = low; i <= high; i++) {

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

printf("\n");

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

// Function to print array


void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

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

printf("\n");

// Main function

int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: \n");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

Selection Sort
#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {

int min_index = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[min_index]) {

min_index = j;

// Swap the found minimum element with the first element

swap(&arr[min_index], &arr[i]);

// Print the array after each pass

printf("After Pass %d: ", i + 1);

for (int k = 0; k < n; k++) {

printf("%d ", arr[k]);

printf("\n");

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Initial array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

selectionSort(arr, n);
printf("\nSorted array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

return 0;

Insertion Sort
#include <stdio.h>

void insertionSort(int arr[], int n) {

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

/* Move elements of arr[0..i-1], that are greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

// Print the array after each pass

printf("After Pass %d: ", i);

for (int k = 0; k < n; k++) {

printf("%d ", arr[k]);

}
printf("\n");

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Initial array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

insertionSort(arr, n);

printf("\nSorted array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

return 0;

Merge Sort
#include <stdio.h>

// Function to merge two subarrays of arr[]

// First subarray is arr[l..m]

// Second subarray is arr[m+1..r]


void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

// Create temporary arrays

int L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

// Merge the temporary arrays back into arr[l..r]

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

// Copy the remaining elements of L[], if any

while (i < n1) {


arr[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if any

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Function to perform merge sort

void mergeSort(int arr[], int l, int r) {

if (l < r) {

// Find the middle point

int m = l + (r - l) / 2;

// Sort first and second halves

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

// Merge the sorted halves

merge(arr, l, m, r);

// Print intermediate step

printf("Intermediate array after merging: ");

for (int i = l; i <= r; i++) {

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

}
printf("\n");

// Function to print array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

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

printf("\n");

// Main function

int main() {

int arr[] = {38, 27, 43, 3, 9, 82, 10};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: \n");

printArray(arr, n);

mergeSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

Heap Sort
#include <stdio.h>

// Function to swap two elements

void swap(int *a, int *b) {


int temp = *a;

*a = *b;

*b = temp;

// Function to heapify a subtree rooted with node i which is an index in arr[]

void heapify(int arr[], int n, int i) {

int largest = i; // Initialize largest as root

int left = 2 * i + 1; // Left child

int right = 2 * i + 2; // Right child

// If left child is larger than root

if (left < n && arr[left] > arr[largest])

largest = left;

// If right child is larger than largest so far

if (right < n && arr[right] > arr[largest])

largest = right;

// If largest is not root

if (largest != i) {

swap(&arr[i], &arr[largest]);

// Recursively heapify the affected sub-tree

heapify(arr, n, largest);

// Main function to do heap sort

void heapSort(int arr[], int n) {

// Build heap (rearrange array)

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);

// One by one extract an element from heap

for (int i = n - 1; i > 0; i--) {

// Move current root to end

swap(&arr[0], &arr[i]);

// call max heapify on the reduced heap

heapify(arr, i, 0);

// Print intermediate step

printf("Step %d: ", n - i);

for (int j = 0; j < n; j++) {

printf("%d ", arr[j]);

printf("\n");

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

heapSort(arr, n);
printf("\nSorted array: ");

for (int i = 0; i < n; i++) {

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

printf("\n");

return 0;

BST
#include <stdio.h>

#include <stdlib.h>

// Structure for a node in BST

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to insert a node into BST

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


if (root == NULL) {

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;

// Function to find the minimum value in BST

int findMin(struct Node* root) {

if (root == NULL) {

printf("Tree is empty\n");

return -1;

struct Node* current = root;

while (current->left != NULL) {

current = current->left;

return current->data;

// Function to find the maximum value in BST

int findMax(struct Node* root) {

if (root == NULL) {

printf("Tree is empty\n");

return -1;

struct Node* current = root;


while (current->right != NULL) {

current = current->right;

return current->data;

// Function for inorder traversal of BST

void inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left);

printf("%d ", root->data);

inorder(root->right);

// Function for preorder traversal of BST

void preorder(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorder(root->left);

preorder(root->right);

// Function for postorder traversal of BST

void postorder(struct Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

printf("%d ", root->data);

}
}

int main() {

struct Node* root = NULL;

// Inserting nodes into BST

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

printf("Minimum value in BST: %d\n", findMin(root));

printf("Maximum value in BST: %d\n", findMax(root));

printf("Inorder traversal: ");

inorder(root);

printf("\n");

printf("Preorder traversal: ");

preorder(root);

printf("\n");

printf("Postorder traversal: ");

postorder(root);

printf("\n");

return 0;

}
Postfix-Infix
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

// Structure for stack

struct Stack {

int top;

unsigned capacity;

int* array;

};

// Function to create a stack

struct Stack* createStack(unsigned capacity) {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

if (!stack)

return NULL;

stack->top = -1;

stack->capacity = capacity;

stack->array = (int*)malloc(stack->capacity * sizeof(int));

if (!stack->array)

return NULL;

return stack;

// Function to check if the stack is empty

int isEmpty(struct Stack* stack) {

return stack->top == -1;

}
// Function to push element onto stack

void push(struct Stack* stack, int item) {

stack->array[++stack->top] = item;

// Function to pop element from stack

int pop(struct Stack* stack) {

if (isEmpty(stack))

return -1;

return stack->array[stack->top--];

// Function to get the top element from stack

int peek(struct Stack* stack) {

if (isEmpty(stack))

return -1;

return stack->array[stack->top];

// Function to check if the character is an operator

int isOperator(char ch) {

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

// Function to get the precedence of the operator

int precedence(char ch) {

if (ch == '+' || ch == '-')

return 1;

else if (ch == '*' || ch == '/')

return 2;
return -1;

// Function to convert infix expression to postfix expression

char* infixToPostfix(char* infix) {

int length = strlen(infix);

char* postfix = (char*)malloc((length + 1) * sizeof(char));

if (!postfix)

return NULL;

struct Stack* stack = createStack(length);

if (!stack)

return NULL;

int j = 0;

for (int i = 0; infix[i]; i++) {

if (isalnum(infix[i]))

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

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

push(stack, infix[i]);

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

while (!isEmpty(stack) && peek(stack) != '(')

postfix[j++] = pop(stack);

if (!isEmpty(stack) && peek(stack) != '(')

return NULL; // Invalid expression

else

pop(stack);

} else {

while (!isEmpty(stack) && precedence(infix[i]) <= precedence(peek(stack)))

postfix[j++] = pop(stack);

push(stack, infix[i]);
}

while (!isEmpty(stack))

postfix[j++] = pop(stack);

postfix[j] = '\0';

return postfix;

// Function to evaluate the postfix expression

int evaluatePostfix(char* postfix) {

struct Stack* stack = createStack(strlen(postfix));

if (!stack)

return -1;

for (int i = 0; postfix[i]; i++) {

if (isdigit(postfix[i]))

push(stack, postfix[i] - '0');

else {

int operand2 = pop(stack);

int operand1 = pop(stack);

switch (postfix[i]) {

case '+':

push(stack, operand1 + operand2);

break;

case '-':

push(stack, operand1 - operand2);

break;

case '*':

push(stack, operand1 * operand2);


break;

case '/':

push(stack, operand1 / operand2);

break;

return pop(stack);

int main() {

char infix[] = "5+((6-3)*2)/3+8";

printf("Infix Expression: %s\n", infix);

char* postfix = infixToPostfix(infix);

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

int result = evaluatePostfix(postfix);

printf("Result of Evaluation: %d\n", result);

free(postfix);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy