Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class List {
- private static class Node {
- String data;
- Node pPrev;
- Node pNext;
- public Node(String data) {
- this.data = data;
- this.pPrev = null;
- this.pNext = null;
- }
- }
- private Node head;
- private Node tail;
- private int length = 0;
- public List() {
- head = null;
- tail = null;
- }
- public int getLength() {
- return this.length;
- }
- public void addElem(final String data) {
- Node temp = new Node(data);
- if (head == null) {
- head = temp;
- } else {
- if (tail == null) {
- head.pNext = temp;
- tail = temp;
- tail.pPrev = head;
- } else {
- tail.pNext = temp;
- temp.pPrev = tail;
- tail = temp;
- }
- }
- length++;
- }
- public void deleteElem(final int index) {
- boolean isDeleted = false;
- if (index == 0) {
- if (head.pNext != null) {
- head = head.pNext;
- }
- head.pPrev = null;
- isDeleted = true;
- }
- else {
- if (index == length - 1) {
- tail = tail.pPrev;
- tail.pNext = null;
- isDeleted = true;
- }
- }
- if (!isDeleted) {
- Node temp;
- int counter;
- if (index <= length - 1 - index) {
- temp = head;
- counter = 0;
- while (counter != index) {
- temp = temp.pNext;
- counter++;
- }
- } else {
- temp = tail;
- counter = length - 1;
- while (counter != index) {
- temp = temp.pPrev;
- counter--;
- }
- }
- temp.pPrev.pNext = temp.pNext;
- temp.pNext.pPrev = temp.pPrev;
- }
- System.out.println("\n|||ЭЛЕМЕНТ УСПЕШНО УДАЛЁН|||\n");
- length--;
- }
- public void reverse() {
- Node fromHead = head;
- Node fromTail = tail;
- String temp;
- int counter = 0;
- while (counter != (length / 2)) {
- temp = fromHead.data;
- fromHead.data = fromTail.data;
- fromTail.data = temp;
- fromHead = fromHead.pNext;
- fromTail = fromTail.pPrev;
- counter++;
- }
- }
- public String printList() {
- StringBuilder str = new StringBuilder();
- Node temp = head;
- int counter = 0;
- str.append("\n|||СПИСОК|||\n\n");
- while (counter != length) {
- str.append(counter).append(". ").append(temp.data).append("\n");
- temp = temp.pNext;
- counter++;
- }
- return str.toString();
- }
- public boolean isEmpty() {
- return head == null;
- }
- }
Add Comment
Please, Sign In to add comment