Question

In: Computer Science

The purpose of this lab is to become familiar with searching, inserting and deleting elements in...

The purpose of this lab is to become familiar with searching, inserting and deleting elements in a linked list.using java  

public boolean contains(String name) // returns true if provided name exists in the list, otherwise returns false public void add(String name) // adds name to the list in its proper lexographical (alphabetical) ordering public void delete(String name) // removes name from list

Solutions

Expert Solution

    public boolean contains(String name) {
                Node temp = this.head;
                while (temp != null) {
                        if (temp.data.compareTo(name) == 0) {
                                return true;
                        }
                        temp = temp.next;
                }
                return false;
        }

        public void add(String name) {
                // if list is empty add directly
                if (this.head == null) {
                        Node nn = new Node();
                        this.head = nn;
                }
                // else insert in alphabetical order
                else {
                        // dummy node used for searching position where the node should be inserted
                        Node dummy = new Node();
                        Node current = dummy;
                        dummy.next = this.head;

                        // iterate until correct position is found
                        while (current.next != null && current.next.data.compareTo(name) < 0) {
                                current = current.next;
                        }

                        // insert node
                        Node nn = new Node();
                        nn.next = current.next;
                        current.next = nn;
                }
        }

        public void delete(String name) {
                Node temp = this.head;
                while (temp != null) {
                        if (temp.data.compareTo(name) == 0) {
                                // search for prev node
                                Node prev = prev(name);
                                // remove the current node
                                prev.next = temp.next;
                        }
                        temp = temp.next;
                }
        }

        private Node prev(String name) {
                Node temp = this.head;
                while (temp != null) {
                        if (temp.next.data.compareTo(name) == 0) {
                                return temp;
                        }
                        temp = temp.next;
                }
                return null;
        }

Since the linkedlist structure was not specified clearly , standard definition is used to complete the code.Feel free to comment in case of any doubts.


Related Solutions

Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar with and practice the measurement of Net Present Value (NPV), payback, and Weighted Average Cost of Capital (WACC) using Microsoft® Excel®. Assignment Steps Resources: Microsoft® Excel®, Capital Budgeting Decision Models Template Calculate the following problems using Microsoft® Excel®: Calculate the NPV for each project and determine which project should be accepted. Project A Project B Project C Project D Inital Outlay (105,000.000) (99,000.00) (110,000.00)...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar with and practice the measurement of Net Present Value (NPV), payback, and Weighted Average Cost of Capital (WACC) using Microsoft Excel. Assignment Steps Resources: Microsoft® Excel®, Capital Budgeting Decision Models Template, Calculate the following problems using Microsoft® Excel®: Calculate the NPV for each project and determine which project should be accepted. Project A Project B Project C Project D Inital Outlay (105,000.000) (99,000.00) (110,000.00)...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of their VMs and running wireshark on their VMs. We also examine Ethernet, IPv4, and TCP addressing at the Network Access, Network, and Transport layers of the TCP/IP stack. Reflection: In two paragraphs reflect the experience of using Wireshark capture (in the lab) on the following questions: What was the most valuable feature of the lab? How did you prepare for this lab? What changes...
The purpose of this assignment is to have you become familiar with polymorphism and be able...
The purpose of this assignment is to have you become familiar with polymorphism and be able to design classes with inheritance The purpose of this assignment is to have you become familiar with polymorphism and be able to design classes with inheritance. Task The School Library has many items available for its students; some can be checked out and others cannot. Among those that can be checked out are journal (i.e., a literary journal) and digital video disk (i.e., a...
Co-occurring Treatment and Recovery Chart The purpose of this assignment is to become familiar with the...
Co-occurring Treatment and Recovery Chart The purpose of this assignment is to become familiar with the treatment and recovery methods, modalities, and strategies. After reading all assigned material complete the “Co-occurring Treatment and Recovery Chart” by filling in the information for the following categories below. Description or definition of the strategy: What is the strategy and modality? For example, regarding stages of change, what is the brief definition of this method? Key concepts and brief definitions: What are the common...
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements...
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements takes time Ω(log n) in the worst case. (Hint: you may want to read Section 8.1 of the textbook for related terminologies and techniques.)
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be able to use your knowledge and understanding of radioisotopes to contribute to the discussion board. Background According to our textbook, “radioisotopes are powerful tools for studying processes in biochemistry, medicine, material science, environmental studies and many other scientific and industrial fields.” Below is a list of radioisotopes and their half-lives. You will be choosing one of these radioisotopes to explore in more detail. Assignment...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be...
Objectives To become familiar with the uses of Radioisotopes through use of the Internet. To be able to use your knowledge and understanding of radioisotopes to contribute to the discussion board. Background According to our textbook, “radioisotopes are powerful tools for studying processes in biochemistry, medicine, material science, environmental studies and many other scientific and industrial fields.” Below is a list of radioisotopes and their half-lives. You will be choosing one of these radioisotopes to explore in more detail. Assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT