Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a function, to be included in a sorted linked list class, called betwee

ID: 3729333 • Letter: 1

Question

1) Write a function, to be included in a sorted linked list class, called betweenltems, that will receive two T arameters called start and end. The function will return how many items in the list are between start and end both included) For example, if the list is 6 79 14 21 45 and start is 8 and end is 21, the function will return 3. Consider (and test your function) on all cases (ex: in the list above if start is 78 and end is 95, or if start is and end is 5, or if start is 5 and end is 50, or if start is 2 and end is 15, or if start is 14 and end is 60). Also if parameters are negative or start is greater than or equal to end, then obviously there cannot be anything between them. Consider efficieney (list is sorted).

Explanation / Answer

/*

* C++ Program to Implement Singly Linked List

*/

#include<iostream>

using namespace std;

/*

* Node Declaration

*/

struct Node

{

int data;

struct Node *next;

}*head;

/*

* Class Declaration

*/

class sList

{

public:

Node* createNode(int);

void display();

void sortedInsert(struct Node **,struct Node *);

int betweenItems(int from, int to);

sList()

{

head = NULL;

}

};

int main()

{

sList sl;

head = NULL;

sl.sortedInsert(&head,sl.createNode(6));

sl.sortedInsert(&head,sl.createNode(7));

sl.sortedInsert(&head,sl.createNode(9));

sl.sortedInsert(&head,sl.createNode(14));

sl.sortedInsert(&head,sl.createNode(21));

sl.sortedInsert(&head,sl.createNode(45));

sl.display();

cout<<endl;

cout<<"Elements betweenItems(8,21) ="<<sl.betweenItems(8,21)<<endl;

cout<<"Elements betweenItems(78,95) ="<<sl.betweenItems(78,95)<<endl;

cout<<"Elements betweenItems(5,50) ="<<sl.betweenItems(5,50)<<endl;

cout<<"Elements betweenItems(50,5) ="<<sl.betweenItems(50,5)<<endl;

  

  

}

int sList::betweenItems(int f,int t)

{

int count = 0; // Initialize count

struct Node* current = head; // Initialize current

if(f>t)

return 0;

while(current!=NULL && current->data<=f)

{

current = current->next;

}

while (current != NULL && current->data<=t)

{

count++;

current = current->next;

}

return count;

}

void sList::sortedInsert(struct Node** head_ref, struct Node* new_node)

{

struct Node* current;

/* Special case for the head end */

if (*head_ref == NULL || (*head_ref)->data >= new_node->data)

{

new_node->next = *head_ref;

*head_ref = new_node;

}

else

{

/* Locate the node before the point of insertion */

current = *head_ref;

while (current->next!=NULL &&

current->next->data < new_node->data)

{

current = current->next;

}

new_node->next = current->next;

current->next = new_node;

}

}

Node *sList::createNode(int value)

{

struct Node *temp, *s;

temp = new(struct Node);

if (temp == NULL)

{

cout<<"Memory not allocated "<<endl;

return 0;

}

else

{

temp->data = value;

temp->next = NULL;

return temp;

}

}

/*

* Display Elements of a link list

*/

void sList::display()

{

struct Node *temp;

if (head == NULL)

{

cout<<"The List is Empty"<<endl;

return;

}

temp = head;

cout<<"Elements of list are: "<<endl;

while (temp != NULL)

{

cout<<temp->data<<"->";

temp = temp->next;

}

cout<<"NULL"<<endl;

}

outPUT:-