In: Computer Science
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.
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 :)