diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index dfe0770..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto diff --git a/LinkedList/LinkedList.java b/LinkedList/LinkedList.java index db1b5f6..78479b7 100644 --- a/LinkedList/LinkedList.java +++ b/LinkedList/LinkedList.java @@ -1,22 +1,22 @@ import java.util.*; -public class LinkedList { +public class LinkedList { //Creating a Node Class. - public static class Node{ + public static class Node{ //define the Node properties. - int data; //a node contain data. - Node next; // next is pointer which will pointing the next node. + T data; //a node contain data. + Node next; // next is pointer which will pointing the next node. //creating Constructor. - Node(int data){ + Node(T data){ this.data = data; //here this ref the curr object. this.next = null; } } - public static Node head; - public static Node tail; + public Node head; + public Node tail; //Creating the methords of LinkedList. @@ -27,10 +27,10 @@ public void create(){ int n = sc.nextInt(); //taking the input from user how many nodes he want to create. for(int i=1; i<=n; i++){ System.out.print("Enter the " + i + " Node data: "); - int data = sc.nextInt(); //take data as input from user. + T data = (T) sc.next(); //take data as input from user. //Here creating the new Node. - Node newNode = new Node(data); + Node newNode = new Node<>(data); //if only one node is created then head and tail both are pointing the same node. //so, that node is be my head as well as tail. @@ -47,7 +47,7 @@ public void create(){ //2nd : Methord Display.. public void display(){ System.out.println(); - Node curr = head; //take a pointer on head node + Node curr = head; //take a pointer on head node while(curr != null){ //if curr is not null. System.out.print(curr.data + " -> "); //then print the data of curr node. curr = curr.next; //move the curr to the next node. @@ -59,8 +59,8 @@ public void display(){ public void insertAtBeg(){ Scanner sc = new Scanner(System.in); System.out.print("Enter the data you want to insert at the beginning: "); - int data = sc.nextInt(); - Node newNode = new Node(data); //crating the new node with the data. + T data = (T) sc.next(); + Node newNode = new Node<>(data); //crating the new node with the data. newNode.next = head;//as i want to insert the new node at the beginning so, //newnode next will be pointing the head. head = newNode; // now update my head to the new node. @@ -70,8 +70,8 @@ public void insertAtBeg(){ public void insertAtEnd(){ Scanner sc = new Scanner(System.in); System.out.print("Enter the data you want to insert at the end: "); - int data = sc.nextInt(); - Node newNode = new Node(data); //creating the new node with the data. + T data = (T) sc.next(); + Node newNode = new Node<>(data); //creating the new node with the data. tail.next = newNode; // tail alway be in the last node so, // i just pointing the tail next to my new craeted node. tail = newNode; // update my tail pointer. @@ -82,7 +82,7 @@ public void insertAtEnd(){ public int sizeOfLL(){ int size = 0; //why initilze with the scope of 0 because if there is no node then size is 0. - Node curr = head; + Node curr = head; while(curr != null){ size++; curr = curr.next; @@ -96,9 +96,9 @@ public void insertAtPos(){ System.out.print("Enter the position where you want to insert the node: "); int pos = sc.nextInt(); System.out.print("Enter the data you want to insert: "); - int data = sc.nextInt(); - Node newNode = new Node(data); - Node curr = head; + T data = (T) sc.next(); + Node newNode = new Node<>(data); + Node curr = head; for(int i=1; i ll = new LinkedList<>(); //creating the object of LinkedList. int choice; do{ diff --git a/Queue/Main.java b/Queue/Main.java new file mode 100644 index 0000000..399e445 --- /dev/null +++ b/Queue/Main.java @@ -0,0 +1,67 @@ +package Queue; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + + MyQueue q = new MyQueue(12); + //MyQueue q = new MyQueue(12); + + Scanner sc = new Scanner(System.in); + int choice; + String data; + do { + System.out.println("-----------|Queue Menu|----------------"); + System.out.println("1. Add an element to the queue"); + System.out.println("2. Remove an element from the queue"); + System.out.println("3. Peek at the front element of the queue"); + System.out.println("4. Display the queue"); + System.out.println("5. Exit"); + System.out.println("-----------------------------------"); + + + System.out.print("Enter your choice: "); + choice = sc.nextInt(); + sc.nextLine(); + switch (choice) { + case 1: + System.out.print("Enter the element you want to add: "); + data = sc.nextLine(); + q.add(data); + break; + case 2: + System.out.println("Removed element: " + q.remove()); + break; + case 3: + System.out.println("Peek element: " + q.peek()); + break; + case 4: + q.display(); + break; + case 5: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice! Please enter a valid choice."); + } + } while (choice != 5); + + + // q.add("Debu"); + // q.add("Akash"); + // q.add("Jit"); + // q.add("Sourav"); + // q.add("Arijit"); + // q.add("Tuhin"); + + + + // q.display(); + // System.out.println(q.peek()); + // q.remove(); + // q.display(); + + } +} \ No newline at end of file diff --git a/Queue/MyQueue.java b/Queue/MyQueue.java new file mode 100644 index 0000000..5e33ba5 --- /dev/null +++ b/Queue/MyQueue.java @@ -0,0 +1,76 @@ +package Queue; + +class MyQueue { + private Object[] arr; + private int size; + private int front; + private int rear; + + MyQueue(int n) { // Constructor + arr = new Object[n]; + this.size = n; + this.front = -1; + this.rear = -1; + } + + // Check if the queue is empty + public boolean isEmpty() { + return front == -1 && rear == -1; + } + + // Check if the queue is full + public boolean isFull() { + return rear == size - 1; + } + + // Add an element to the queue + public void add(T data) { + if (isFull()) { + System.out.println("The Queue is full! You can't add anything!!"); + return; + } + if (isEmpty()) { + front = 0; + } + arr[++rear] = (T) data; + } + + // Remove an element from the queue + public T remove() { + if (isEmpty()) { + System.out.println("The Queue is already empty!"); + return null; + } + T removedElement = (T) arr[front]; + if (front == rear) { // Queue has only one element + front = -1; + rear = -1; + } else { + front++; + } + return removedElement; + } + + // Peek at the front element of the queue + public T peek() { + if (isEmpty()) { + System.out.println("Queue is already empty! You can't get any peek element."); + return null; + } + return (T)arr[front]; + } + + // Display all elements of the queue + public void display() { + if (isEmpty()) { + System.out.println("The Queue is empty!"); + return; + } + System.out.print("Queue is : -> "); + System.out.print("["); + for (int i = front; i <= rear; i++) { + System.out.print(arr[i] + ","); + } + System.out.println("]"); + } +} diff --git a/README.md b/README.md index 00760dd..f7cae24 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,16 @@ -# Data Structure Implementation 🧑‍💻🥇🛠️ - IT IS A PUBLIC REPO IN WHICH EVERYONE CAN CONTRIBUTE +# Data Structure Implementation 🧑‍💻 + +This repository contains implementations of various data structures. Each data structure is implemented in a separate file or directory. The implementations are written in Java. +This is a repository where keep all important DSA implementation which are asked in interview. Creating a well-organized space to store all your Data Structures and Algorithms (DSA) implementations is essential for efficient access and maintenance. This repository is a collection of various data structures and algorithms implemented in Java. + +## Table of Contents + +1. [LinkedList](https://github.com/debapriyo007/Data-Structure-Implementation/blob/main/LinkedList/LinkedList.java) +2. [Stack](#stack) +3. [Queue](#queue) +4. [Heap](#binary-search-tree) - **This Repository keep Data Structure Implementation Only. 😅** -| No | Title | Implementation | -| --- | ---------------------------------------- | ------------------------------------------------------------- | -| 1 | LinkedList Impementation | [Click Here](https://github.com/debapriyo007/Data-Structure-Implementation/blob/main/LinkedList/LinkedList.java) ## Contributing 🧑🏽‍💻 @@ -13,3 +18,5 @@ Contributions are welcome! Feel free to open a pull request if you have a better solution or want to add solutions. + + diff --git a/Stack/Main.java b/Stack/Main.java new file mode 100644 index 0000000..007c04e --- /dev/null +++ b/Stack/Main.java @@ -0,0 +1,68 @@ +package Stack; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Stack s = new Stack(1000); + + int choice; + Scanner sc = new Scanner(System.in); + do{ + + System.out.println("-----------|Stack|----------------"); + System.out.println("1. Push"); + System.out.println("2. Pop"); + System.out.println("3. Peek"); + System.out.println("4. isEmpty"); + System.out.println("5. isFull"); + System.out.println("6. Size"); + System.out.println("7. Display"); + System.out.println("8. Exit"); + System.out.println("-----------------------------------"); + + + System.out.print("Enter your choice: "); + choice = sc.nextInt(); + switch(choice){ + case 1: + System.out.print("Enter the data you want to push: "); + String data = sc.next(); + s.push(data); + break; + case 2: + System.out.println("Popped element is: "+ s.pop()); + break; + case 3: + System.out.println("Peek element is: "+ s.peek()); + break; + case 4: + System.out.println("Stack is empty: "+ s.isEmpty()); + break; + case 5: + System.out.println("Stack is full: "+ s.isFull()); + break; + case 6: + System.out.println("Size of the stack is: "+ s.size()); + break; + case 7: + s.display(); + break; + case 8: + System.out.println("Exiting..."); + break; + default: + System.out.println("Invalid choice!Please enter a valid choice."); + } + }while(choice!=8); + sc.close(); + } + + // s.push(21); + // s.push(11); + // s.push(31); + // s.push(233); + + // s.display(); + // s.pop(); + // s.display(); +} diff --git a/Stack/Stack.java b/Stack/Stack.java new file mode 100644 index 0000000..985a923 --- /dev/null +++ b/Stack/Stack.java @@ -0,0 +1,68 @@ +package Stack; +import java.util.*; + +//Implementation of Stack Using Array Only . +class Stack{ + + private Object[]arr; + private int index = 0; + + public Stack(int size){ + arr = new Object[size]; + } + + //now creating the methods of stack. + public void push(T data){ + //check if the stack is full or not. + if(index == arr.length){ + System.out.println("Stack is full!"); + return; + } + arr[index] = data; + index++; + } + + public T pop(){ + //check if the stack is empty or not. + if(index == 0){ + System.out.println("Stack is empty!You can't pop from an empty stack."); + return null; + } + index--; + return (T)arr[index]; + } + + public T peek(){ + if(index == 0){ + System.out.println("Stack is empty!You can't peek from an empty stack."); + return null; + } + return (T)arr[index-1]; //this is my + } + + public boolean isEmpty(){ + return index == 0; + } + + public boolean isFull(){ + return index == arr.length; + } + + public int size(){ + return index; + } + + public void display(){ + System.out.print("Stack is : -> "); + System.out.print("["); + for(int i = 0;i<=index-1;i++){ + System.out.print(arr[i] + ","); + } + System.out.println("]"); + } + +} + + + +