Data Structures Implementation
2 min readNov 8, 2022
Implementation Concepts DSA
1. Singly LinkedList
class HelloWorld {
static class Node{
int data;
Node next;
public Node(int item){
data = item;
next = null;
}
}
static Node head;
public static HelloWorld add(HelloWorld list, int item){
if(list.head == null){
list.head = new Node(item);
return list;
} else
{
Node temp = list.head;
while(temp.next != null)
{
temp = temp.next;
}
temp.next = new Node(item);
}
return list;
}
public static HelloWorld delete(HelloWorld list, int key)
{
Node temp = list.head;
if(list.head != null && list.head.data == key)
{
list.head = temp.next;
return list;
}
Node prev = null;
while(temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
if(temp != null)
{
prev.next = temp.next;
}
return list;
}
public static void print(Node head){
if(head == null) return;
while(head != null)
{
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args) {
HelloWorld list = new HelloWorld();
list.add(list, 23);
list.add(list, 21);
list.add(list, 90);
list.add(list, 9);
list.add(list, 10);
list.delete(list, 23);
print(list.head);
}
}
2. Doubly LinkedList
uhhi
3. Binary Tree
class BinaryTree{
static class Node{
Node left, right;
int data;
public Node(int item){
data = item;
left = right = null;
}
}
static Node root;
// adding node
public static void add(int item){
root = addRec(root,item);
}
public static Node addRec(Node root, int item){
if(root == null){
root = new Node(item);
return root;
}
if(item < root.data){
root.left = addRec(root.left, item);
}else if(item > root.data){
root.right = addRec(root.right, item);
}
return root;
}
// printing using traversalpublic static void inorder(Node root){
if(root == null) return;
inorder(root.left);
System.out.println(root.data + " ");
inorder(root.right);
}
public static void main(String[] args) {
BinaryTree bst = new BinaryTree();
bst.add(23);
bst.add(21);
bst.add(90);
bst.add(9);
bst.add(10);
inorder(root);
}
}
4. Stack
// Stack implementation in Javaclass Stack {// store elements of stack
private int arr[];
// represent top of stack
private int top;
// total capacity of the stack
private int capacity;// Creating a stack
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}// push elements to the top of stack
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");
System.exit(1);
}// insert element on top of stack
System.out.println("Inserting " + x);
arr[++top] = x;
}// pop elements from top of stack
public int pop() {// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}// pop element from top of stack
return arr[top--];
}// return size of the stack
public int getSize() {
return top + 1;
}// check if the stack is empty
public Boolean isEmpty() {
return top == -1;
}// check if the stack is full
public Boolean isFull() {
return top == capacity - 1;
}// display elements of stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}public static void main(String[] args) {
Stack stack = new Stack(5); stack.push(1);
stack.push(2);
stack.push(3); System.out.print("Stack: ");
stack.printStack();// remove element from stack
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();}
}