[go: up one dir, main page]

0% found this document useful (0 votes)
43 views24 pages

DSA File

The document contains an index of programming assignments with titles and dates. The assignments involve creating and manipulating different data structures using functions including singly linked lists, doubly linked lists, stacks, queues, binary search trees, and sorting algorithms. Specific tasks include inserting and deleting elements, converting infix to postfix expressions, and implementing sorting methods. Sample code is provided for some of the assignments.
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)
43 views24 pages

DSA File

The document contains an index of programming assignments with titles and dates. The assignments involve creating and manipulating different data structures using functions including singly linked lists, doubly linked lists, stacks, queues, binary search trees, and sorting algorithms. Specific tasks include inserting and deleting elements, converting infix to postfix expressions, and implementing sorting methods. Sample code is provided for some of the assignments.
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/ 24

INDEX

S. NO. TITLE DATE


1. Write a program that uses functions to perform the following: 11/07/2023
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.

2. Write a program that uses functions to perform the following: 11/07/2023


a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion
.
3. Write a program that uses stack operations to convert a given 25/07/2023
infix expression into its postfix Equivalent, implement the
stack using an array.

4. Write program to implement a double ended queue using 01/08/2023


a) array and
b) doubly linked list respectively.

5. Write a program that uses functions to perform the following: 08/08/2023


a) Create a binary search tree of integers.
b) Insert elements in the binary tree.

6. Write a program that uses functions to perform the following: 08/08/2023


a) Create a binary search tree of integers.
b) Delete elements from binary tree.

7. Write program for implementing the following sorting 12/09/2023


methods to arrange a list of integers in ascending order:
a) Insertion sort
b) Merge sort
c) Bubble sort.

8. Write program for implementing the following sorting 26/09/2023


methods to arrange a list of integers in ascending order:
a) Quick sort
b) Selection sort.

9. Write program implement Merge Sort. 10/10/2023


PRACTICAL 1
AIM: -Write a program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.
INPUT
class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
LinkedList() {
head = null;
}
// Function to insert a new node at the end of the linked list
void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}

// Function to delete a given integer from the linked list


void delete(int key) {
Node current = head;
Node prev = null;
if (current != null && current.data == key) {
head = current.next;
return;
}
while (current != null && current.data != key) {
prev = current;
current = current.next;
}
if (current == null) {
System.out.println(key + " not found in the list.");
return;
}
prev.next = current.next;
}
// Function to display the contents of the linked list
void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
}
public class Main {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.insert(11);
linkedList.insert(12);
linkedList.insert(13);
linkedList.insert(14);

System.out.println("Original List:");
linkedList.display();
int keyToDelete = 3;
linkedList.delete(keyToDelete);
System.out.println("List after deleting " + keyToDelete + ":");
linkedList.display();
}
}

OUTPUT
PRACTICAL 2
AIM: - Write a program that uses functions to perform the following:
a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.
INPUT
class Node {

int data;

Node prev;

Node next;

Node(int data) {

this.data = data;

this.prev = null;

this.next = null;

class DoublyLinkedList {

Node head;

DoublyLinkedList() {

head = null;

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

void insert(int data) {

Node newNode = new Node(data);

if (head == null) {

head = newNode;

} else {

Node current = head;

while (current.next != null) {

current = current.next;

}
current.next = newNode;

newNode.prev = current;

// Function to delete a given integer from the doubly linked list

void delete(int key) {

Node current = head;

while (current != null) {

if (current.data == key) {

if (current.prev != null) {

current.prev.next = current.next;

} else {

head = current.next;

if (current.next != null) {

current.next.prev = current.prev;

return;

current = current.next;

System.out.println(key + " not found in the list.");

// Function to display the contents of the doubly linked list

void display() {

Node current = head;

while (current != null) {

System.out.print(current.data + " <-> ");

current = current.next;

System.out.println("null");

}
public class Main {

public static void main(String[] args) {

DoublyLinkedList doublyLinkedList = new DoublyLinkedList();

doublyLinkedList.insert(41);

doublyLinkedList.insert(42);

doublyLinkedList.insert(43);

doublyLinkedList.insert(44);

System.out.println("Original Doubly Linked List:");

doublyLinkedList.display();

int keyToDelete = 3;

doublyLinkedList.delete(keyToDelete);

System.out.println("Doubly Linked List after deleting " + keyToDelete + ":");

doublyLinkedList.display();

OUTPUT
PRACTICAL 3
AIM: - . Write a program that uses stack operations to convert a given infix expression into
its postfix Equivalent, implement the stack using an array.
INPUT
import java.util.Stack;
public class InfixToPostfix {
public static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public static String infixToPostfix(String infixExpression) {
StringBuilder postfix = new StringBuilder();
Stack<Character> stack = new Stack<>();

for (int i = 0; i < infixExpression.length(); i++) {


char c = infixExpression.charAt(i);

if (Character.isLetterOrDigit(c)) {
postfix.append(c);
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
postfix.append(stack.pop());
}
stack.pop(); // Pop '('
} else {
while (!stack.isEmpty() && precedence(c) <= precedence(stack.peek())) {
postfix.append(stack.pop());
}
stack.push(c);
}
}
while (!stack.isEmpty()) {
if (stack.peek() == '(') {
return "Invalid infix expression";
}
postfix.append(stack.pop());
}
return postfix.toString();
}
public static void main(String[] args) {
String infixExpression = "a+b*(c^d-e)^(f+g*h)-i";
String postfixExpression = infixToPostfix(infixExpression);
System.out.println("Infix Expression: " + infixExpression);
System.out.println("Postfix Expression: " + postfixExpression);
}
}

OUTPUT
PRACTICAL 4
AIM: - Write program to implement a double ended queue using
a) array and
b) doubly linked list respectively.
INPUT
a) Using array
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(31);
deque.addFirst(32);
deque.addLast(33);
deque.addLast(34);
System.out.println("Deque elements: " + deque);

System.out.println("Front element: " + deque.peekFirst());


System.out.println("Rear element: " + deque.peekLast());

deque.removeFirst();
deque.removeLast();
System.out.println("Deque elements after deletion: " + deque);
}
}

OUTPUT
b) Double linked list
INPUT
import java.util.LinkedList;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Integer> deque = new LinkedList<>();
deque.addFirst(31);
deque.addFirst(32);
deque.addLast(33);
deque.addLast(34);
System.out.println("Deque elements: " + deque);
System.out.println("Front element: " + deque.peekFirst());
System.out.println("Rear element: " + deque.peekLast());
deque.removeFirst();
deque.removeLast();

System.out.println("Deque elements after deletion: " + deque);


}
}

OUTPUT
PRACTICAL 5
AIM: - Write a program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Insert elements in the binary tree.
INPUT
class TreeNode {

int data;

TreeNode left;

TreeNode right;

public TreeNode(int data) {

this.data = data;

this.left = null;

this.right = null;

class BinarySearchTree {

TreeNode root;

public BinarySearchTree() {

root = null;

public void insert(int data) {

root = insertRec(root, data);

private TreeNode insertRec(TreeNode root, int data) {

if (root == null) {

root = new TreeNode(data);

return root;

if (data < root.data) {

root.left = insertRec(root.left, data);

} else if (data > root.data) {

root.right = insertRec(root.right, data);


}

return root;

public class Main {

public static void main(String[] args) {

BinarySearchTree bst = new BinarySearchTree();

// Insert elements into the binary search tree

bst.insert(50);

bst.insert(30);

bst.insert(70);

bst.insert(20);

bst.insert(40);

bst.insert(60);

bst.insert(80);

System.out.println("Binary Search Tree created with elements: 50, 30, 70, 20, 40, 60, 80");

OUTPUT
PRACTICAL 6
AIM: - Write a program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Delete elements from binary tree
INPUT
class TreeNode {
int data;
TreeNode left;
TreeNode right;

public TreeNode(int data) {


this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
TreeNode root;

public BinarySearchTree() {
root = null;
}
public void insert(int data) {
root = insertRec(root, data);
}
private TreeNode insertRec(TreeNode root, int data) {
if (root == null) {
return new TreeNode(data);
}
if (data < root.data) {
root.left = insertRec(root.left, data);
} else if (data > root.data) {
root.right = insertRec(root.right, data);
}
return root;
}
public void delete(int data) {
root = deleteRec(root, data);
}
private TreeNode deleteRec(TreeNode root, int data) {
if (root == null) {
return root;
}
if (data < root.data) {
root.left = deleteRec(root.left, data);
} else if (data > root.data) {
root.right = deleteRec(root.right, data);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}
return root;
}
private int minValue(TreeNode root) {
int minValue = root.data;
while (root.left != null) {
minValue = root.left.data;
root = root.left;
}
return minValue;
}
public void inorderTraversal(TreeNode node) {
if (node != null) {
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);
}
}
}
public class Main {
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
int[] elements = {50, 30, 70, 20, 40, 60, 80};
for (int element : elements) {
bst.insert(element);
}
System.out.println("Inorder Traversal of the Tree:");
bst.inorderTraversal(bst.root);
System.out.println();
int elementToDelete = 30;
bst.delete(elementToDelete);
System.out.println("After deleting " + elementToDelete + ":");
System.out.println("Inorder Traversal of the Tree:");
bst.inorderTraversal(bst.root);
System.out.println();
}
}

OUTPUT
PRACTICAL 7
AIM: - . Write program for implementing the following sorting methods to arrange a list of
integers in ascending order:
a) Insertion sort
b) Merge sort
c) Bubble sort
INPUT
a) Insertion sort
import java.util.Arrays;
public class InsertionSort{
// Insertion Sort
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
int[] arr = {94, 64, 95, 52, 28, 19, 10, 32, 48};
// Insertion Sort
int[] insertionSortedArray = Arrays.copyOf(arr, arr.length);
insertionSort(insertionSortedArray);
System.out.println("Insertion Sort: " + Arrays.toString(insertionSortedArray));

}
}

OUTPUT
b) Merge sort
import java.util.Arrays;
public class SortingMethods {
// Merge Sort
public static void mergeSort(int[] arr) {
if (arr.length > 1) {
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
mergeSort(left);
mergeSort(right);
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < left.length) {
arr[k] = left[i];
i++;
k++;
}
while (j < right.length) {
arr[k] = right[j];
j++;
k++;
}
}
}

public static void main(String[] args) {


int[] arr = {94, 64, 95, 52, 28, 19, 10, 32, 48};
// Merge Sort
int[] mergeSortedArray = Arrays.copyOf(arr, arr.length);
mergeSort(mergeSortedArray);
System.out.println("Merge Sort: " + Arrays.toString(mergeSortedArray));
}
}

OUTPUT
c) Bubble sort
import java.util.Arrays;
public class BubbleSort {
// Bubble Sort
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
// Bubble Sort
int[] bubbleSortedArray = Arrays.copyOf(arr, arr.length);
bubbleSort(bubbleSortedArray);
System.out.println("Bubble Sort: " + Arrays.toString(bubbleSortedArray));
}
}

OUTPUT
PRACTICAL 8
AIM: - Write program for implementing the following sorting methods to arrange a list of
integers in ascending order:
a) Quick sort
b) Selection sort
a) Quick sort
INPUT
import java.util.Arrays;
public class Main {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int partitionIndex = partition(arr, low, high);
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}

OUTPUT

b) Selection sort
import java.util.Arrays;
public class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
selectionSort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}

OUTPUT

PRACTICAL 9
AIM: - Write program implement Merge Sort.
INPUT
import java.util.Arrays;
public class MergeSort {
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
for (int i = 0; i < n1; i++) {
leftArray[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
rightArray[j] = arr[mid + 1 + j];
}
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArray[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArray[j];
j++;
k++;
}
}
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
mergeSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}

OUTPUT
https://www.google.co.in/books/edition/_/bVbj9nhvHd4C?hl=en&gbpv=1&pg=PR4

You might also like