Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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

This file was deleted.

9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/bin/
/build/
/.settings/
target
.project
.settings
.classpath
.DS_Store
.idea
*.iml
58 changes: 58 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>kr.pe.advenoh</groupId>
<artifactId>Data-Structure-In-Java</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Data-Structure-In-Java</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.deepak.data.structures.PriorityQueue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* https://algorithmtutor.com/Data-Structures/Tree/Priority-Queues/
* https://stackoverflow.com/questions/2661065/a-good-sorted-list-for-java
* https://www.geeksforgeeks.org/search-insert-and-delete-in-a-sorted-array/
*/
public class MinPriorityQueue<T> {
private List<T> list;

public MinPriorityQueue() {
this.list = new ArrayList<>();
}

public void add(T value) {
list.add(value);
Collections.sort(list, (o1, o2) -> {
if (o1 instanceof Integer) {
if ((int) o1 < (int) o2) {
return -1;
} else if ((int) o1 > (int) o2) {
return 1;
} else {
return 0;
}
} else {
throw new RuntimeException("no compare for this type");
}
});
}

public T poll() {
return list.get(0);
}

public int size() {
return list.size();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
## Queue Introduction
A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court. New additions to a line are made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item.
The picture demonstrates the FIFO access.
![queues](https://cloud.githubusercontent.com/assets/3439029/22179653/301eab38-e00f-11e6-908a-eea4c8515da2.png)
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
There are various applications of Queue :
Queue, as the name suggests is used whenever we need to have any group of objects in an order in which the first one coming in, also gets out first while the others wait for there turn, like in the following scenarios,
- Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
- In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a service representative is free.
- Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive, First come first served.
## Queue Introduction

A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court. New additions to a line are made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item.
The picture demonstrates the FIFO access.

![queues](https://cloud.githubusercontent.com/assets/3439029/22179653/301eab38-e00f-11e6-908a-eea4c8515da2.png)

The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

There are various applications of Queue :

Queue, as the name suggests is used whenever we need to have any group of objects in an order in which the first one coming in, also gets out first while the others wait for there turn, like in the following scenarios,
- Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
- In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a service representative is free.
- Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive, First come first served.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## Stack Introduction
Stack is a logical data structure that can be logically thought as a linear structure and can be represented in real life as pile of plates or deck of cards. In this structure, insertion and deletion of items takes place only at one end called "_Top_" of the stack. The basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it.
The basic implementation of a stack is also called a **LIFO** (Last In First Out) to demonstrate the way it accesses data. There are basically three operations that can be performed on stacks . They are 1) inserting an item into a stack (_push_). 2) deleting an item from the stack (_pop_). 3) displaying the contents of the stack(_peek_).
![push_pop](https://cloud.githubusercontent.com/assets/3439029/22179613/b78d8fd2-e00d-11e6-9f38-63628ad02f16.png)
All operations except size() can be performed in **O(1)** time. size() runs in at worst **O(N)**.
It can be implemented using either Arrays, LinkedList or Queues.
There are various applications of Stack :
- Converting a decimal number to binary number.
- Tower of Hanoi
- Expression evaluation and syntax parsing
- Conversion of Infix to Postfix
- QuickSort etc.
## Stack Introduction

Stack is a logical data structure that can be logically thought as a linear structure and can be represented in real life as pile of plates or deck of cards. In this structure, insertion and deletion of items takes place only at one end called "_Top_" of the stack. The basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it.

The basic implementation of a stack is also called a **LIFO** (Last In First Out) to demonstrate the way it accesses data. There are basically three operations that can be performed on stacks . They are 1) inserting an item into a stack (_push_). 2) deleting an item from the stack (_pop_). 3) displaying the contents of the stack(_peek_).

![push_pop](https://cloud.githubusercontent.com/assets/3439029/22179613/b78d8fd2-e00d-11e6-9f38-63628ad02f16.png)

All operations except size() can be performed in **O(1)** time. size() runs in at worst **O(N)**.
It can be implemented using either Arrays, LinkedList or Queues.

There are various applications of Stack :
- Converting a decimal number to binary number.
- Tower of Hanoi
- Expression evaluation and syntax parsing
- Conversion of Infix to Postfix
- QuickSort etc.
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
## Strings Introduction
- Strings are basically sequence of characters stored in a memory.
- They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object.
- Java provides a String class to create a manipulate strings. These are objects backed up by char array.
- When a string is created, it's value is cached in string pool and then stored in Heap.
- Below is the example of creation of string using, both literal and object. When it's created using literals, it's value is cached but when created using the new object, it does not get's cached, until we explicitly asks to cache it.
```java
String s1 = "Hello"; // New string creation using literals.
String s2 = "Hello";
String s3 = new String("Hello"); // This value is not cached in the string pool
System.out.println(s1 == s2); // Prints True
System.out.println(s1 == s3); // Prints False
```
- To make s2 and s3 point to same reference, we have to invoke the API **intern()** of String class. This API will ensure that string gets cached in the string pool.
```java
String s3 = new String("Hello").intern();
```
<img width="614" alt="screen shot 2017-02-12 at 11 51 43 am" src="https://cloud.githubusercontent.com/assets/3439029/22865449/c407fafa-f119-11e6-89ca-04d45abe425b.png">
- Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change.
- For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
- Moreover, Strings are thread safe as well in java.
**StringBuffer and StringBuilder :**
- As explained earlier, Strings are immutable because String literals with same content share the same storage in the string common pool. Modifying the content of one String directly may cause adverse side-effects to other Strings sharing the same storage.
- JDK provides two classes to support mutable strings: StringBuffer and StringBuilder (in core package java.lang) . A StringBuffer or StringBuilder object is just like any ordinary object, which are stored in the heap and not shared, and therefore, can be modified without causing adverse side-effect to other objects.
- StringBuilder class was introduced in JDK 1.5. It is the same as StringBuffer class, except that StringBuilder is not synchronized for multi-thread operations. However, for single-thread program, StringBuilder, without the synchronization overhead, is more efficient.
**StringTokenizer**
- Very often, you need to break a line of texts into tokens delimited by white spaces. The java.util.StringTokenizer class supports this.
**String API's :**
```java
- Below are some basic API's. All are not covered here,
1. charAt()
- returns the character located at the specified index.
2. equalsIgnoreCase()
- determines the equality of two Strings, ignoring their case (upper or lower case doesn't matters with this function.
3. length()
- returns the number of characters in a String.
4. replace()
- replaces occurrences of character with a specified new character.
5. substring()
- returns a part of the string. This method has two forms,
- public String substring(int begin);
- public String substring(int begin, int end);
6. toLowerCase()
- returns string with all uppercase characters converted to lowercase
7. valueOf()
- used to convert primitive data types into Strings.
8. toString()
- returns the string representation of the object used to invoke this method.
- toString() is used to represent any Java Object into a meaningful string representation
9. trim()
- returns a string from which any leading and trailing white spaces has been removed
```
## Strings Introduction

- Strings are basically sequence of characters stored in a memory.
- They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object.
- Java provides a String class to create a manipulate strings. These are objects backed up by char array.
- When a string is created, it's value is cached in string pool and then stored in Heap.
- Below is the example of creation of string using, both literal and object. When it's created using literals, it's value is cached but when created using the new object, it does not get's cached, until we explicitly asks to cache it.

```java
String s1 = "Hello"; // New string creation using literals.
String s2 = "Hello";
String s3 = new String("Hello"); // This value is not cached in the string pool
System.out.println(s1 == s2); // Prints True
System.out.println(s1 == s3); // Prints False
```

- To make s2 and s3 point to same reference, we have to invoke the API **intern()** of String class. This API will ensure that string gets cached in the string pool.

```java
String s3 = new String("Hello").intern();
```
<img width="614" alt="screen shot 2017-02-12 at 11 51 43 am" src="https://cloud.githubusercontent.com/assets/3439029/22865449/c407fafa-f119-11e6-89ca-04d45abe425b.png">

- Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change.
- For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
- Moreover, Strings are thread safe as well in java.

**StringBuffer and StringBuilder :**

- As explained earlier, Strings are immutable because String literals with same content share the same storage in the string common pool. Modifying the content of one String directly may cause adverse side-effects to other Strings sharing the same storage.
- JDK provides two classes to support mutable strings: StringBuffer and StringBuilder (in core package java.lang) . A StringBuffer or StringBuilder object is just like any ordinary object, which are stored in the heap and not shared, and therefore, can be modified without causing adverse side-effect to other objects.
- StringBuilder class was introduced in JDK 1.5. It is the same as StringBuffer class, except that StringBuilder is not synchronized for multi-thread operations. However, for single-thread program, StringBuilder, without the synchronization overhead, is more efficient.

**StringTokenizer**

- Very often, you need to break a line of texts into tokens delimited by white spaces. The java.util.StringTokenizer class supports this.

**String API's :**

```java
- Below are some basic API's. All are not covered here,
1. charAt()
- returns the character located at the specified index.
2. equalsIgnoreCase()
- determines the equality of two Strings, ignoring their case (upper or lower case doesn't matters with this function.
3. length()
- returns the number of characters in a String.
4. replace()
- replaces occurrences of character with a specified new character.
5. substring()
- returns a part of the string. This method has two forms,
- public String substring(int begin);
- public String substring(int begin, int end);
6. toLowerCase()
- returns string with all uppercase characters converted to lowercase
7. valueOf()
- used to convert primitive data types into Strings.
8. toString()
- returns the string representation of the object used to invoke this method.
- toString() is used to represent any Java Object into a meaningful string representation
9. trim()
- returns a string from which any leading and trailing white spaces has been removed
```
Loading