0% found this document useful (0 votes)
9 views8 pages

C Code for Linked List and BST Operations

Uploaded by

yuviksonii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

C Code for Linked List and BST Operations

Uploaded by

yuviksonii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assessment

Name -Vaibhav Agarwal


Reg no -24BCE2397

Q1)

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

// Node structure
typedef struct node {
int data;
struct node* next;
} node;

node* head = NULL;

// Function to insert at end


void insertEnd(int element) {
node* newnode = (node*) malloc(sizeof(node));
newnode->data = element;
newnode->next = NULL;

if (head == NULL) {
head = newnode;
} else {
node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
}
}

// Function to find smallest element


int find_smallest() {
int smallest = head->data;
node* ptr = head;
while (ptr != NULL) {
if (ptr->data < smallest) {
smallest = ptr->data;
}
ptr = ptr->next;
}
return smallest;
}

// Function to delete a given element


void del(int element) {
node* ptr = head;
node* prev = NULL;

// If head is to be deleted
if (head != NULL && head->data == element) {
head = head->next;
free(ptr);
return;
}

// Otherwise search the node


while (ptr != NULL && ptr->data != element) {
prev = ptr;
ptr = ptr->next;
}

if (ptr == NULL) return; // Element not found

prev->next = ptr->next;
printf("Element deleted is: %d\n", ptr->data);
free(ptr);
}

// Function to display list


void display() {
node* ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

int main() {
// Insert elements
insertEnd(100);
insertEnd(20);
insertEnd(35);
insertEnd(30);
insertEnd(45);
insertEnd(40);
insertEnd(50);
insertEnd(80);
insertEnd(10);
insertEnd(90);
insertEnd(75);

printf("Linked list before deletion:\n");


display();

del(find_smallest()); // Delete smallest

printf("Linked list after deletion:\n");


display();

return 0;
}
Q2)
#include <stdio.h>
#include <stdlib.h>

// Node structure
typedef struct node {
int data;
struct node *left, *right;
} Node;

// Function to create a new node


Node* getnode(int element) {
Node* newnode = (Node*) malloc(sizeof(Node));
newnode->data = element;
newnode->left = newnode->right = NULL;
return newnode;
}

// Insert into BST


Node* insert(Node* root, int element) {
Node* newnode = getnode(element);

if (root == NULL) {
return newnode;
}

Node* current = root;


Node* parent = NULL;

while (current != NULL) {


parent = current;
if (element < current->data) {
current = current->left;
} else {
current = current->right;
}
}

if (element < parent->data)


parent->left = newnode;
else
parent->right = newnode;
return root;
}

// Inorder traversal
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
Node* root = NULL;

// Insert elements
root = insert(root, 6);
root = insert(root, 21);
root = insert(root, 28);
root = insert(root, 14);
root = insert(root, 18);
root = insert(root, 11);
root = insert(root, 32);
root = insert(root, 25);
root = insert(root, 23);
root = insert(root, 37);
root = insert(root, 27);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 19);
root = insert(root, 30);
root = insert(root, 12);

printf("Inorder traversal of the BST: ");


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

return 0;
}

You might also like