Skip to content
Advertisement

Make a moving cursor using doubly linked list in java

I’m trying to write a code for a simple moving cursor in java. I have to use doubly linked list. Also I should implement doubyLinkedList class myself. In this program first we take an integer like n from user as number of lines. Then we take n lines of input. Each line contains only one character which can be < or > or - or one of the lower case English alphabet. For > or < we move the cursor to right or left. For - we delete the character in the place of cursor. For alphabets we add them to the doubly linked list. At last we print the result in one line without spaces. I have made the doubyLinkedList class and needed methods. I think it is working properly but the time complexity of the code must be O(n). I guess it’s O(n^2) now.

import java.util.*;
public class DoublyLinkedList {
static Node head = null;
static int size = 0;
class Node {
    String data;
    Node prev;
    Node next;
    Node(String d) {
        data = d;
    }
}
static Node deleteNode(Node del) {
    if (head == null || del == null)
        return null;
    if (head == del)
        head = del.next;
    if (del.next != null)
        del.next.prev = del.prev;
    if (del.prev != null)
        del.prev.next = del.next;
    del = null;
    size --;
    return head;
}
public String GetNth(int index) {
    Node current = head;
    int count = 0;
    while (current != null)
    { if (count == index)
        return current.data;
        count++;
        current = current.next;
    }
    assert (false);
    return null;
}
public static void deleteNodeAtGivenPos(int n) {
    size--;
    if (head == null || n <= 0)
        return;
    Node current = head;
    int i;
    for (i = 1; current != null && i < n; i++)
        current = current.next;
    if (current == null)
        return;
    deleteNode(current);
}
void insertFirst (String data){
    size++;
    Node newNode = new Node(data);
    if (head == null) {
        newNode.prev = null;
        head = newNode;
        return;
    }
    else
        head.prev= newNode;
    newNode.next=head;
    head = newNode;
}
void append(String data) {
    size++;
    Node newNode = new Node(data);
    Node last = head;
    newNode.next = null;
    if (head == null) {
        newNode.prev = null;
        head = newNode;
        return;
    }
    while (last.next != null)
        last = last.next;
    last.next = newNode;
    newNode.prev = last;
}
public void insertAtGivenPos(String s , int index){
    Node newNode= new Node(s);
    Node current= head;
    if (index==0)
        insertFirst(s);
    else if(index==size)
        append(s);
    else{
        for(int j = 0; j < index && current.next != null; j++){
            current = current.next;
        }
        newNode.next = current;
        current.prev.next = newNode;
        newNode.prev = current.prev;
        current.prev = newNode;
        size++;
    }
}
public void print(Node node) {
    while (node != null) {
        System.out.print(node.data + "");
        node = node.next;
    }
}
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.nextLine());
    String [] arr= new String[n];
    for (int i=0; i< n; i++)
        arr[i] = sc.nextLine();
    DoublyLinkedList dll = new DoublyLinkedList();
    int cursor= 0;
    for (int i=0; i< n; i++){
        if (arr[i].matches("[a-z]")){
            dll.insertAtGivenPos(arr[i], cursor);
            cursor++;
        }
        else if (arr[i].contains("-")&& dll.GetNth(cursor-1)!= null) {
            dll.deleteNodeAtGivenPos(cursor);
            cursor--;
        }
        else if (arr[i].contains("<") && dll.GetNth(cursor-1)!= null)
            cursor--;
        else if (arr[i].contains(">") && dll.GetNth(cursor+1)!= null)
            cursor++;
    }
    dll.print(dll.head);
}
}

How can I improve my code to reduce time complexity?

Edit: Also I understood that my code is giving wrong answer for some cases. Could you help me debug it?

Advertisement

Answer

If you are not forced to follow implementation details, I suggest not to have a cursor. Instead, keep a variable currentNode which is the node that the cursor would point to. So for every command (or data) in the input loop, you have one O(1) operation as bellow and hence will get O(n) time complexity.

1 – for > : change the currentNode to currentNode.next

2 – for < do the reverse

3 – for - change the previous and the next node of currentNode to point to each other (so the current is deleted)

4 – and finally for insertion : create a node and like 3, change the the side nodes to point to it

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement