Question: Can someone help with this linked list on c++. I implemented all the methods except for the function void insert( int pos, T val); that

Can someone help with this linked list on c++. I implemented all the methods except for the function void insert(int pos, T val); that I don't know how to do it. That function is for creating a new node and inserting it in a position. ASAP THANKS for the help

code (class):

/**********************

COMP 315 - Introduction to data structures

Singly linked node

This class implements the singly linked list using templates

Each list has two attributes:

-head: first node in the list

-tail: last node in the list

Considerations:

1. head and tail point to null in an empty list

2. tail->next=null

******************/

#include "IntSLLNode.hpp"

template <class T>

class IntSLList{

public:

//Default constructor: creates an empty list

IntSLList();

//Destructor: deallocate memory

~IntSLList();

//addToHead(T val): creates a new node with val as info,

//and this new node is the new head

void addToHead(T val);

//addToTail(T val): creates a new node with val as info,

//and this new node is the new tail

void addToTail(T val);

//deleteFromHead: remove head from the list,

//the new head is the previous head->next

//if the list had only one node, head and tail point null

void deleteFromHead();

//deleteFromTail: remove tail from the list,

//the new tail is the previous node to tail

//if the list had only one node, head and tail point null

void deleteFromTail();

//In the list is empty, returns true

//otherwise, returns false

bool isEmpty();

//sortInsert(T val): creates a new node, and it is inserted sortly

void sortInsert(T val);

//insert(int pos, T val): creates a new node, and it is inserted in position pos

void insert(int pos, T val);

//printList(): prints all nodes in the list

void printList();

private:

IntSLLNode *head; //A pointer to the first node

IntSLLNode *tail; //A pointer to the last node

};

/****************

Default constructor: creates an empty list

head and tail point null

*****************/

template <class T>

IntSLList::IntSLList()

{

tail = head = 0;

}

/***********************

Destructor: deallocate memory removing each node from the list

*****************/

template <class T>

IntSLList::~IntSLList()

{

//Declare a pointer prtNode

IntSLLNode *prtNode;

//prtNode points head

prtNode=head;

//While there is a node in the list, remove it

while(prtNode != 0)

{

//prtNode points head->next

prtNode = head->getNext();

//delete head

delete head;

//the new head is prtNode

head=prtNode;

}

}

/***********************

addToHead(T val): creates a new node with val as info,

and this new node is the new head

***********************/

template <class T>

void IntSLList::addToHead(T val){

head = new IntSLLNode(val, head);

if(tail==0){

tail = head;

}

}

/***********************

addToTail(T val): creates a new node with val as info,

and this new node is the new tail

***********************/

template <class T>

void IntSLList::addToTail(T val){

if(tail!=0){

tail->setNext(new IntSLLNode(val,0));

tail = tail->getNext();}

else {

head = tail = new IntSLLNode(val,0);

}

}

/***********************

deleteFromHead: remove head from the list,

the new head is the previous head->next

if the list had only one node, head and tail point null

***********************/

template <class T>

void IntSLList::deleteFromHead(){

if (head !=0){

IntSLLNode*tmp;

tmp = head;

head = head ->getNext();

if( head ==0){tail =0;}

delete tmp; }}

/***********************

deleteFromTail: remove tail from the list,

the new tail is the previous node to tail

if the list had only one node, head and tail point null

***********************/

template <class T>

void IntSLList::deleteFromTail(){

if (head !=0){

IntSLLNode*tmp;

tmp = head;

if (head!=tail){while (tmp->getNext() != tail){

tmp = tmp ->getNext();}

tail=tmp;

tmp=tmp->getNext();

tail->setNext(0); }

else {head= tail= 0;}

delete tmp;}

}

/***********************

In the list is empty, returns true

otherwise, returns false

***********************/

template <class T>

bool IntSLList::isEmpty(){

bool res;

res = false;

if(head == 0){

res = true;

}

return res;

}

/***********************

//sortInsert(T val): creates a new node, and it is inserted sortly

***********************/

template <class T>

void IntSLList::sortInsert(T val){

if (head==0){

addToHead(val); }

else { IntSLLNode*tmp, *tmp2;

tmp=head;

while((valgetInfo()) && (tmp !=tail)){

tmp2=tmp;

tmp=tmp->getNext();}

if((tmp == tail) && (valgetInfo())){ addToTail(val);

} else {

if((tmp== head) && (val>tmp->getInfo())){

addToHead(val);}

else {tmp2->setNext(new IntSLLNode(val,tmp));

}

}

}

}

/***********************

insert(int pos, T val): creates a new node, and it is inserted in position pos

HELP PLEASEEEEEEEEEEEE!!!!!!!!!

***********************/

template <class T>

void IntSLList::insert(Node** current, int pos, T val)

{

if (pos < 1 || pos > size + 1)

cout << "Invalid position!" << endl;

else {

while (pos--) {

if (pos == 0){

Node* tmp = getNext(val);

tmp->next = *current;

*current = tmp;

}

else

current = &(*current)->next;

}

size++

}

}

/***********************

printList(): prints all nodes in the list

***********************/

template <class T>

void IntSLList::printList()

{ IntSLLNode*prtNode;

prtNode = head;

while(prtNode !=0){

cout << prtNode->getInfo()<<"";

prtNode=prtNode->getNext();} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!