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
IntSLLNode
};
/****************
Default constructor: creates an empty list
head and tail point null
*****************/
template <class T>
IntSLList
{
tail = head = 0;
}
/***********************
Destructor: deallocate memory removing each node from the list
*****************/
template <class T>
IntSLList
{
//Declare a pointer prtNode
IntSLLNode
//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
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
if(tail!=0){
tail->setNext(new IntSLLNode
tail = tail->getNext();}
else {
head = tail = new IntSLLNode
}
}
/***********************
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
if (head !=0){
IntSLLNode
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
if (head !=0){
IntSLLNode
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
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
if (head==0){
addToHead(val); }
else { IntSLLNode
tmp=head;
while((val
tmp2=tmp;
tmp=tmp->getNext();}
if((tmp == tail) && (val
} else {
if((tmp== head) && (val>tmp->getInfo())){
addToHead(val);}
else {tmp2->setNext(new IntSLLNode
}
}
}
}
/***********************
insert(int pos, T val): creates a new node, and it is inserted in position pos
HELP PLEASEEEEEEEEEEEE!!!!!!!!!
***********************/
template <class T>
void IntSLList
{
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
{ IntSLLNode
prtNode = head;
while(prtNode !=0){
cout << prtNode->getInfo()<<"";
prtNode=prtNode->getNext();} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
