Question

In: Computer Science

Start with the provided code for the class linkedListType. Be sure to implement search, insert, and...

Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided).

Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. Note: the link pointer of the last element of the list is NULL.

Test your new function in main.

Submit a .zip of your entire project.
  

Solutions

Expert Solution

So, here is the solution of your problem I used C++ language to solve your problem.

I put comments also along with the screenshots of code as well as output.

#include <iostream>
using namespace std;

//for formatting purpose only
#define line cout<<"\n---------------------------------------------------------------------------------------------------\n"; 


//node structure
class linkedListType{
    public :
    int data;
    linkedListType *next;
  
    linkedListType(int data){
        this->data = data;
        this->next = nullptr;
    }
};

//to insert node data into list
linkedListType* insert(linkedListType *head,int data){
    linkedListType *node = new linkedListType(data);
        //assign head  into node next if head is null then node's next will be null else node's next will be head 
        node->next = head;
      // then make newly created node as head
        head = node;
    }
    return head;
}
bool search(linkedListType *head,int key){
    linkedListType *node = head;
    //traverse the list
    while(node != nullptr){
        //if key found return true
        if(node->data == key)
            return true;
        node = node->next;
    }
    //if we reached here that means we did not found the element else we must return with true
    return false;
}

// In this we will be traversing the list until we get the node->next == null as soon as we get null assign node into the next of last node
linkedListType* insertLast(linkedListType *head,int data){
    linkedListType *temp = head;
    linkedListType *node = new linkedListType(data);
    if(head == nullptr){
        head = node;
        return head;
    }
    while(temp->next != nullptr)
        temp = temp->next;
    temp->next = node;
    return head;
}

// function to delete node if we get the key equal to the element then asign the next of the previous node to the next of the current node
// and free the memory
linkedListType* delet(linkedListType *head,int key){
    linkedListType *node = head;
    if(node == nullptr){
        cout<<"List is empty!";
        return node;
    }
    //if head is to be deleted
    if(node->data == key){
        linkedListType* temp = node;
        head = head->next;
        temp->next = nullptr;
        free(temp);
        return head;
    }
    while(node->next != nullptr){
        if(node->next->data == key){
            linkedListType* temp = node->next;
            node->next = node->next->next;
            temp->next = nullptr;
            free(temp);
        }
    }
    return head;
}

// to print the linkedList
void print(linkedListType* head){
    if(head == nullptr)
        cout<<"List is empty!";
    else{
        linkedListType* node = head;
        while(node != nullptr){
            cout<<node->data<<"->";
            node = node->next;
        }
        cout<<"NULL";
    }
}

int main()
{
    linkedListType *head = nullptr;
    
    // running loop infinetly untill user choose to Quit
    while(true){
        cout<<"Choose from below menu:\n";
    cout<<"1. Insert\n2. Insert Last\n3. Search\n4. Delete\n5. Quit\n";
    int ch,data,key;
    cin>>ch;
    switch(ch){
        case 1 : cout<<"Enter data : ";
                 cin>>data;
                 head = insert(head,data);
                 line;
                 cout<<"List:\n";
                 print(head);
                 line;
                 break;
        case 2 : cout<<"Enter data : ";
                 cin>>data;
                 head = insertLast(head,data);
                 line;
                 cout<<"List:\n";
                 print(head);
                 line;
                 break;
        case 3 : cout<<"Enter key : ";
                 cin>>key;
                 if(head == nullptr){
                     cout<<"List is empty!";
                 }
                 else{
                     if(search(head,key))
                        cout<<"Found!\n";
                    else
                        cout<<"Not Found!\n";
                }
                 break;
        case 4 : cout<<"Enter data : ";
                 cin>>data;
                 head = delet(head,data);
                 line;
                 cout<<"List:\n";
                 print(head);
                 line;
                 break;
        case 5 : exit(0);
        default : cout<<"Wrong choice!";
    }
    cout<<"\n\n";
    }
    return 0;
}

Screenshots of Code:

Screenshots of Output:

So, this was the solutions of the problem.

I hope I am able to solve your problem, if yes then do give it a like.

It really helps :)


Related Solutions

In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
You are provided with a StackInterface to implement your stack, make sure to implement the data...
You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
a java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that asks the user to enter coins until they have entered enough for the NAU power juice. Open the insert_coins.m file. Initialize total to 0. We do this because initially, no coins have been entered. Using a loop, ask the user to enter a coin until the total matches or exceeds 115 cents. The input should be a char or string, so make sure that...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT