# Applications

#### LL

```java
package com.gs.corejava.datastructures;

public class MySinglyLinkedList<T> {

	private class MyNode<E extends Object> {
		E data;
		MyNode<E> next = null;

		public MyNode() {
		}

		public MyNode(E data) {
			this.data = data;
		}
	}

	private MyNode<T> start;
	private int size = 0;
	private MyNode<T> currentNode;

	public MySinglyLinkedList() {
		start = new MyNode<T>();
		start.next = null;
	}

	public void add(T data) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		if (null == currentNode) {
			while (null != head) {
				prev = head;
				head = head.next;
			}
			currentNode = myNode;
			prev.next = currentNode;
		} else {
			currentNode.next = myNode;
			currentNode = myNode;
		}
		size++;
	}

	public void addFirst(T data) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		myNode.next = head.next;
		head.next = myNode;
		size++;
	}

	public void addLast(T data) {
		add(data);
	}

	public void add(T data, int index) {
		MyNode<T> myNode = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		int currentIndex = 0;
		while (null != head && currentIndex <= index) {
			prev = head;
			head = head.next;
			currentIndex++;
		}
		myNode.next = prev.next;
		prev.next = myNode;
		size++;
	}

	public MyNode<T> remove(T data) {
		MyNode<T> node = new MyNode<T>(data);
		MyNode<T> head = start;
		MyNode<T> prev = start;
		while (null != head) {
			if (null != node.data && node.data.equals(head.data)) {
				break;
			}
			prev = head;
			head = head.next;
		}
		prev.next = head.next;
		head = null;
		size--;
		return node;

	}

	public MyNode<T> remove(int index) {
		MyNode<T> head = start;
		MyNode<T> prev = start;
		int currentIndex = 0;
		while (null != head && currentIndex <= index) {
			prev = head;
			head = head.next;
			currentIndex++;
		}
		MyNode<T> myNodeToReturn = head;
		prev.next = head.next;
		head = null;
		size--;

		return myNodeToReturn;

	}

	public Object[] toArray() {
		Object[] array = new Object[size];
		int k = 0;
		MyNode<T> head = start.next;
		while (null != head) {
			array[k] = head.data;
			k++;
			head = head.next;
		}
		return array;

	}

}

```

```java
package com.gs.corejava.datastructures;

public class TestLinkedList {

	public static void main(String[] args) {

		testIntegerLinkedListAdd();

		System.out.println("---for String objects---");

		testStringLinkedListAddMethod();

		System.out.println("---for Custom objects like Teacher----");

		testCustomObjLinkedListAdd();
	}

	/**
	 * 
	 */
	private static void testIntegerLinkedListAdd() {
		MySinglyLinkedList<Integer> linkedList = new MySinglyLinkedList<Integer>();

		linkedList.add(123);
		linkedList.add(456);
		linkedList.add(454);
		linkedList.add(224);

		linkedList.add(-80, 2);

		linkedList.addFirst(12);

		linkedList.remove(new Integer(454));
		linkedList.remove(2);

		Object[] arr = linkedList.toArray();
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

	/**
	 * 
	 */
	private static void testCustomObjLinkedListAdd() {
		// ---------for Custom objects like Teacher-----------
		MySinglyLinkedList<Teacher> listOfTeacher = new MySinglyLinkedList<Teacher>();
		Teacher teacher1 = new Teacher(1, "Mohit");
		Teacher teacher2 = new Teacher(2, "Sumit");
		Teacher teacher3 = new Teacher(3, "Sahil");
		Teacher teacher4 = new Teacher(4, "Naveen");
		listOfTeacher.add(teacher1);
		listOfTeacher.add(teacher2);
		listOfTeacher.add(teacher3);
		listOfTeacher.add(teacher4);
		Object[] arrayOfTeacher = listOfTeacher.toArray();
		Teacher teacherObj;
		for (int i = 0; i < arrayOfTeacher.length; i++) {
			if (arrayOfTeacher[i] instanceof Teacher) {
				teacherObj = (Teacher) arrayOfTeacher[i];
				System.out.println(teacherObj);
			}
		}
	}

	/**
	 * 
	 */
	private static void testStringLinkedListAddMethod() {
		// -------for String objects-------
		MySinglyLinkedList<String> listOfNames = new MySinglyLinkedList<String>();
		listOfNames.add("Mohit");
		listOfNames.add("Rohit");
		listOfNames.add("Sumit");
		listOfNames.add("Amit");
		Object[] arrayOfNames = listOfNames.toArray();
		for (int i = 0; i < arrayOfNames.length; i++) {
			System.out.println(arrayOfNames[i]);
		}
	}

}

class Teacher {
	private int id;
	private String name;

	/**
	 * @param id
	 * @param name
	 */
	public Teacher(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}

}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-core-java-for-java.gitbook.io/project/4-generics/applications.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
