Lab.cpp and text file is given lab22.cpp: input.txt: Question: linked list with
ID: 3704353 • Letter: L
Question
Lab.cpp and text file is given
lab22.cpp:
input.txt:
Question: linked list with its original values. Much of the structure is given for a menu-driven C++ program to implement and perform operations on a singly linked list. 1. Read and understand the given code 2. Understand each function's purpose. Take note of the parameter types and return types 3. Implement the following functions: a. bool isEmpty(node *current_head) b. void insertPos(node *current, int val, int toSearch) c. void printList node *current) d. void deleteLast(node *current, node *trailCurrent) e. void deleteAll(node *current) f. int frequency(node *current, int toSearch) g. void sort(node *head) Test your implementation. Ensure each menu choice works correctly. Handling invalid input is not required. 4. 5. Submit a single completed .cpp file on Blackboard.Explanation / Answer
bool isEmpty(node *current_head)
{
boolean result=false;
if(current_head==null)
result=true;
return result;
}
----------------------------------------------------------------------------
void insertPos(node* current, int val, int toSearch)
{
check if the given current node is NULL */
if (current == NULL)
{
cout>>"Nothing was found and nothing is inserted";
return;
}
while(current.data!=null)
{
if(current.data==isSearch){
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
new_node.data=val; }
current=current->next;
}
if(current->next==null)
new_node->next=null;
current=*new_node;
}
----------------------------------------------------------------------------void printList(node *current)
{
if(current==null)
return;
else
cout>>current->data;
printList(current->next);
}
----------------------------------------------------------------------------
void deleteLast(node *current, node *trailCurrent)
{
while(current->next!=null)
{
trailCurrent=current;
current=current->next;
}
trailCurrent->next=null;
}
----------------------------------------------------------------------------
void deleteAll(node *current)
{
if(current==null)
return;
deleteAll(current->next);
current->next=null;
}
----------------------------------------------------------------------------
int frequency(node *current, int toSearch)
{
int count=0;
while(current->next!=null)
{
if(current->data==tosearch)
count++;
current=current->next;
}
return count;
}
----------------------------------------------------------------------------
void Sort(node *head)
{
int swapped, i;
node *temp1=new node;
node *temp2 = NULL;
/* Checking for empty list */
if (head == NULL)
return;
do
{
swapped = 0;
temp1 = hehad;
while (temp1->next != temp2)
{
if (temp1->data > temp1->next->data)
{
swap(temp1, temp1->next);
swapped = 1;
}
temp1 = temp1->next;
}
temp2 = temp1;
}
while (swapped);
}
/* function to swap data of two nodes a and b*/
void swap(node *a, node *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}
I hope the functions has been a help to you. Let me know if problem persists. Happy learning :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.