Skip to content

Implement DSA with Generics #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

38 changes: 19 additions & 19 deletions LinkedList/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import java.util.*;

public class LinkedList {
public class LinkedList<T> {

//Creating a Node Class.
public static class Node{
public static class Node<T>{
//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<T> 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<T> head;
public Node<T> tail;

//Creating the methords of LinkedList.

Expand All @@ -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<T> 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.
Expand All @@ -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<T> 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.
Expand All @@ -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<T> 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.
Expand All @@ -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<T> 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.
Expand All @@ -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<T> curr = head;
while(curr != null){
size++;
curr = curr.next;
Expand All @@ -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<T> newNode = new Node<>(data);
Node<T> curr = head;
for(int i=1; i<pos-1; i++){
curr = curr.next;
}
Expand All @@ -111,7 +111,7 @@ public void insertAtPos(){

public static void main(String[] args) {

LinkedList ll = new LinkedList(); //creating the object of LinkedList.
LinkedList<String> ll = new LinkedList<>(); //creating the object of LinkedList.

int choice;
do{
Expand Down
67 changes: 67 additions & 0 deletions Queue/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package Queue;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

MyQueue<String> q = new MyQueue<String>(12);
//MyQueue<Integer> q = new MyQueue<Integer>(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();

}
}
76 changes: 76 additions & 0 deletions Queue/MyQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package Queue;

class MyQueue <T> {
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("]");
}
}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# 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 🧑🏽‍💻

Contributions are welcome! Feel free to open a pull request if you have a better solution or want to add solutions.




68 changes: 68 additions & 0 deletions Stack/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package Stack;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Stack<String> s = new Stack<String>(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();
}
Loading