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

Dsa Programs

The document discusses different data structures like linked lists, stacks, queues, binary trees and AVL trees. It provides code implementations for doubly linked lists, stack push, queue add and binary trees. It also includes algorithms for insertion into a linked list, stack push, queue add and searching a binary tree.

Uploaded by

shejoleradha44
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)
18 views

Dsa Programs

The document discusses different data structures like linked lists, stacks, queues, binary trees and AVL trees. It provides code implementations for doubly linked lists, stack push, queue add and binary trees. It also includes algorithms for insertion into a linked list, stack push, queue add and searching a binary tree.

Uploaded by

shejoleradha44
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/ 5

Linked List : singly and doubled linked list is covered in this example

class Node {

int data;

Node prev, next;

public Node(int data) {

this.data = data;

public class SimpleDoublyLinkedList {

private Node head;

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

public void display() {


Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
SimpleDoublyLinkedList doublyLinkedList = new SimpleDoublyLinkedList();
doublyLinkedList.insert(1);
doublyLinkedList.insert(2);
doublyLinkedList.insert(3);
doublyLinkedList.display();
}
}
Algo for linked list

1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and assign the head pointer to it.
5 If the list is not empty, add the data to a node and link to the current head. Assign the head to
the newly added node.
6. END

Stack Push Implementation


import java.io.*;
import java.util.*; // util imports the stack class
public class StackExample {
public static void main (String[] args) {
Stack<Integer> stk = new Stack<Integer>();

// inserting elements into the stack


stk.push(52);
stk.push(19);
stk.push(33);
stk.push(14);
stk.push(6);
System.out.print("The stack is: " + stk);
}
}
Algo

1 − Checks if the stack is full.


2 − If the stack is full, produces an error and exit.
3 − If the stack is not full, increments top to point next empty space.
4 − Adds data element to the stack location, where top is pointing.
5 − Returns success.
Queue add Implementation
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(6);
q.add(1);
q.add(8);
q.add(4);
q.add(7);
System.out.println("The queue is: " + q);
}
}
Queue Algo

1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END

Tree Implementation : tree needs to just explain whereas if ask about specific tree then
implementation and algo must write there
Binary tree implementation
class Node {

int key;

Node left, right;


public Node(int item)
{
key = item;
left = right = null;
}
}

class BinaryTree {

// Root of Binary Tree


Node root;

// Constructors
BinaryTree(int key) { root = new Node(key); }
BinaryTree() { root = null; }
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();

// Create root
tree.root = new Node(1);
/* Following is the tree after above statement
1
/ \
null null
*/

tree.root.left = new Node(2);


tree.root.right = new Node(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
null null null null */
tree.root.left.left = new Node(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}

Binary tree Algo

1. START
2. Check whether the tree is empty or not
3. If the tree is empty, search is not possible
4. Otherwise, first search the root of the tree.
5. If the key does not match with the value in the root, search its subtrees.
6. If the value of the key is less than the root value, search the left subtree
7. If the value of the key is greater than the root value, search the right subtree.
8. If the key is not found in the tree, return unsuccessful search.
9. END
AVL Tree

Implementation of avl tree:

Algo :

Step 1 − Create a node


Step 2 − Check if the tree is empty
Step 3 − If the tree is empty, the new node created will become the root node of the AVL Tree.
Step 4 − If the tree is not empty, we perform the Binary Search Tree insertion operation and check
the balancing factor of the node in the tree.
Step 5 − Suppose the balancing factor exceeds ±1, we apply suitable rotations on the said node and
resume the insertion from Step 4.

You might also like