Data structure and algorithm-linked list test questions (java)

tags: Data Structures and Algorithms

Single list

Data type

class Student{
    int id;
    String name;
    Student next; // point to the next node

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student[ Student ID = " + id + ", name = " + name + ']';
    }
}

Single linked list

class SinglyLinkedList{
    private Student head = new Student(0,""); //Head node

    public Student getHead(){
        return head;
    }

    //Traversal
    public void info(){
        if (head.next == null){
            System.out.println("The linked list is empty");
            return;
        }
        Student temp = head.next;
        while (true){
            if(temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //Add to
    public void add(Student student){
        Student temp = head; //Auxiliary variable
        boolean isFalse = false;//Does the added number (id) exist?
        while (true){
            if (temp.next == null){ //Last of the list
                break;
            }
            if (temp.next.id > student.id){ //Number comparison, location found
                break;
            }
            if (temp.next.id == student.id){//The id value to be added already exists
                isFalse = true;//Indicates that the number exists
                break;
            }
            //temp move backward
            temp = temp.next;
        }
        //Determine whether the id value exists
        if (isFalse){
            System.out.println("The data to be added already exists, and the student ID is = " + student.id);
        }else
            student.next = temp.next;
            temp.next = student;
        }
}

1. Find the number of valid nodes in the singly linked list

Traverse through the head node

Code:

class Unity{
   public static int getLenght(Student head){
        int lenght = 0; //count
        if (head.next == null){//Empty linked list
            return lenght;
        }
        Student temp = head.next;
        while (temp != null){
            lenght++; //Accumulate
            temp = temp.next; //Move back
        }
        return lenght;
    }
 }

test:

public class Demo1 {
    public static void main(String[] args) {
        SinglyLinkedList list = new SinglyLinkedList();
        Student st1 = new Student(11,"Yiyi");
        Student st2 = new Student(62,"Er");
        Student st3 = new Student(393,"Thirty-three");
        Student st4 = new Student(991,"Sisi");
        list.add(st1);
        list.add(st2);
        list.add(st4);
        list.add(st3);
        list.info();

        //1, find the number of valid nodes in the singly linked list
        int lenght = Unity.getLenght(list.getHead());
        System.out.println("Number of singly linked list nodes:" + lenght);
    }
}

2. Find the kth node from the bottom in the singly linked list 0

Linked list length-reciprocal node (k) = node position to find
code:

  public static Student reciprocal(Student head,int index){
        if (head.next == null){
            System.out.println("The linked list is empty, no node exists");
            return null;
        }
        int lenght = getLenght(head);//length
        if (index > lenght || index <= 0){
            System.out.println("How can I say, not in this range");
            return null;
        }
        Student temp = head;
        for (int i = 0; i <= lenght - index; i++) {
            temp = temp.next;
        }
        return temp;
    }

test:

	//2, find the kth node from the bottom in the singly linked list
	Student reciprocal = Unity.reciprocal(list.getHead(), -1);
    System.out.println(reciprocal);

3. Reversal of singly linked list

Ideas:
defines a new head node, traverses the original linked list, and takes out each node,
is placed after the new head node and changes the head node point of the linked list.

1. Create a new head node newHead
Two auxiliary pointers
temp = head.next; next = temp.next

2. Traverse
newHead.next = temp; temp = next; next = temp.next

3. Put the removed node at the top
temp.next = newHead.next;

4. Change the direction
head.next = newHead.next;

Code:

   public static void reversetList(Student head){
        if (head.next == null || head.next.next == null){
            //No need to reverse when empty or only one node
            return;
        }
        //Create a new head node
        Student newHead = new Student(0,"");
        Student temp = head.next;
        Student next = null;
        while (temp != null){
            next = temp.next;//Save the nodes if there are next time
            temp.next = newHead.next;//The next node points to the forefront of the new linked list
            newHead.next = temp;//Connect to the new linked list
            temp = next;//temp move backward
        }
        head.next = newHead.next;//Change point
    }

test:

	 System.out.println("**********Reverse**********");
     Unity.reversetList(list.getHead());
     list.info();

4. Print singly linked list from end to beginning

Ideas:
Method 1: Reverse, then traverse and print
Method 2: Store the data in an array, and then print from the end
Method 3: Stack structure, first-in-last-out (to be added...)

Code: array

public static void printArray(Student head){
    if (head.next == null || head.next.next == null){
        //No need when empty or only one node
        return;
    }
    int size = getLenght(head);
    Student student[] = new Student[size];
    Student temp = head.next;
    for (int i = 0; i < size; i++) {
        student[i] = temp;
        temp = temp.next;
    }
    for (int i = 1; i <= size; i++) {
        System.out.println(student[size - i]);
    }
}

test:

	System.out.println("**********flashback**********");
    Unity.printArray(list.getHead());

5. Merge two ordered singly linked lists, and the merged list is still in order

Code: It doesn't feel very good,Is there any other way?

   public static void twoList(Student oneHead,Student twoHead){
        if (oneHead.next == null || twoHead.next == null){
            return;
        }
        Student one = oneHead.next;
        Student two = twoHead.next;
        Student student = new Student(0,"");
        Student temp = student;

       while (one != null){
            while (two != null){
                if (one.id > two.id){
                    temp.next = two;
                    temp = temp.next;
                }else {
                    temp.next = one;
                    temp = temp.next;
                    break;
                }
                two = two.next;
            }
            //The last id value of one is greater than all values
            if (two == null && one != null){
                temp.next = one;
                temp = temp.next;
            }
           one = one.next;
       }
       //The last id value of two is greater than all values
       while (true){
           if (two != null){
               temp.next = two;
               temp = temp.next;
           }else {
               break;
           }
           two = two.next;
       }
       oneHead.next = student.next;
    }

test:

   SinglyLinkedList list2 = new SinglyLinkedList();
   Student stu1 = new Student(17,"11");
   Student stu2 = new Student(12,"22");
   Student stu3 = new Student(110,"33");
   list2.add(stu1);
   list2.add(stu2);
   list2.add(stu3);
   list2.info();
   System.out.println("**********merge**********");
   Unity.twoList(list.getHead(),list2.getHead());
   list.info();

Intelligent Recommendation

Java data structure and algorithm (5)-linked list

 Linked list We know that an array is a general data structure that can be used to implement many data structures such as stacks and queues. The linked list is also a widely used general data struct...

Java data structure and algorithm 3 linked list

Linked list idea Linked list is stored in the way of nodes Each node contains two fields: data+next: point to the next node The nodes of the linked list are not necessarily stored continuously The lin...

Java data structure and algorithm-linked list

One: Introduction to the linked list: The linked list is an ordered list, but it is stored in memory as follows Summary: 1) The linked list is stored in the form of nodes, which is a chain storage 2) ...

Java data structure and algorithm _02 linked list

Java data structure and algorithm _02 Singly linked list Add data (create) Modify node information Delete node information Complete code Interview questions Complete code Doubly linked list Complete c...

Java data structure and algorithm-handwritten linked list

You can refer to the structure of the linked list: Complete code operation result:...

More Recommendation

Linked list of Java algorithm and data structure

Welcome to my blog:https://Mr00wang.github.io study notesLinked list of Java algorithm and data structure Linked list 1. Introduction to Linked List The linked list is stored in the form of nodes, whi...

Algorithm of java data structure singly linked list

1. Introduction to linked lists The linked list is an ordered list, but it is stored in the memory as follows:             1. The linked list is stored in the form of nod...

Java data structure and algorithm-doubly linked list

Doubly linked list This is the code implementation of a doubly linked list in java. Here, the node creation of the doubly linked list is simply realized (different from the singly linked list, a point...

Java data structure and algorithm-singly linked list

Compared with arrays and queues, linked lists are a non-contiguous, non-sequential storage structure. The linked list is stored in the form of nodes (chain storage) Each node contains a data field (da...

Data structure and algorithm (java)-linked list

Single list:It is a chain access data structure that uses a set of storage units with arbitrary addresses to store the data elements in the linear table. The data in the linked list is represented by ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top