Changing data inside a node to integer from string [closed]

here below there is a from a subject data structures it is a linked list management C++ code that create a singly linked list and do other operations such as inserting a node, traversing, appending, inserting and deleting. As explained in the title I want to change data inside the node to integer from string, but I could not figure it out about how to do that. Could you help me to modify my code? I just want to change it by integer. Thanks in advance. #include #include using namespace std; template //initializing a template class class SNode { public: E elem; // linked list element value SNode* next; // next item in the list //friend class SLinkedList; // provide SLinkedList access }; template class SLinkedList { // a singly linked list public: SLinkedList() {// empty list constructor this->head = nullptr; }; void addFront(const E& e) { // add to front of list SNode* v = new SNode; // create new node v->elem = e; // store data v->next = this->head; // head now follows v this->head = v; }; void append(const E& e) { // find the tail SNode* v = new SNode; // create new node v->elem = e; // store data v->next = nullptr; // node next is null if (this->head != nullptr) { // if list is non empty SNode* nodePtr = this->head; // start cursor while (nodePtr->next!=nullptr) { nodePtr = nodePtr->next; } nodePtr->next = v; // assign lst node next to new node } else { // list is empty this->head=v; // change head to new node } } void insertAtNode(const E& key, const E& e) { // find the node SNode* nodePtr = this->head; // start cursor while (nodePtr!=nullptr && key != nodePtr->elem) { // traverse the list nodePtr = nodePtr->next; // move to next node } if (nodePtr == nullptr) { // key not found cout next; // new node s next is the next to cursor nodePtr->next = v; // node next is the new } } void deleteNode (const E& key) { SNode* nodePtr = this->head; // start cursor SNode* prevNode = nullptr; // keeping reference of the previous node while (nodePtr!=nullptr && key != nodePtr->elem) { // traverse the list while the key is not found prevNode = nodePtr ; // keep the reference of the previous node while traversing nodePtr = nodePtr->next; // move to next node } if (nodePtr == nullptr) { // key not found cout head = nodePtr->next; // change head to the next node } else { prevNode->next = nodePtr->next; // change the next of the previous node to the next of the current node } delete nodePtr; // delete the node } } ~SLinkedList() { //destructor SNode* prevNode = nullptr; // initializing reference of the previous node SNode* nodePtr = this->head; // start cursor from the head while (nodePtr!=nullptr) { // traverse the list prevNode = nodePtr ; // keep the reference of the previous node while traversing nodePtr = nodePtr->next; // move to next node delete prevNode; // delete the previous node } } void displayList(string header) { // function to display the list SNode* nodePtr = this->head; // start cursor from the head cout

Mar 23, 2025 - 13:20
 0
Changing data inside a node to integer from string [closed]

here below there is a from a subject data structures it is a linked list management C++ code that create a singly linked list and do other operations such as inserting a node, traversing, appending, inserting and deleting. As explained in the title I want to change data inside the node to integer from string, but I could not figure it out about how to do that. Could you help me to modify my code? I just want to change it by integer. Thanks in advance.

#include 
#include 
using namespace std;
template  //initializing a template class
class SNode 
{   public:
E elem;                 // linked list element value
SNode* next;             // next item in the list
//friend class SLinkedList;         // provide SLinkedList access
};

template 
class SLinkedList {             // a singly linked list
public:
    SLinkedList() {// empty list constructor
        this->head = nullptr;
    };              
    void addFront(const E& e) {         // add to front of list
        SNode* v = new SNode;     // create new node
        v->elem = e;                    // store data
        v->next = this->head;                   // head now follows v
        this->head = v;  
    };
    void append(const E& e) {
        // find the tail
        SNode* v = new SNode;     // create new node
        v->elem = e;                    // store data
        v->next = nullptr;              // node  next is null
        
        if (this->head != nullptr) { // if  list is non empty
            SNode* nodePtr = this->head; // start cursor
            while (nodePtr->next!=nullptr) {
                nodePtr = nodePtr->next;
            }
            nodePtr->next =  v;     // assign lst node  next to new node 
        }
        else {                      // list is empty
            this->head=v;           // change  head to new node
        }
    }
    void insertAtNode(const E& key, const E& e) { 
        // find the node
        SNode* nodePtr = this->head; // start cursor
        while (nodePtr!=nullptr && key != nodePtr->elem) { // traverse the list
                nodePtr = nodePtr->next; // move to next node
            } 
        if (nodePtr == nullptr) { // key not found
            cout << "\n" << key << " not found !\n";
        }    
        else { // when the key is found
             SNode* v = new SNode;        // create new node
            v->elem = e;                    // store data
            v->next = nodePtr->next;        // new node s next is the next to cursor
            nodePtr->next = v;              // node  next is the new 
        }
    }
    void deleteNode (const E& key) {
        SNode* nodePtr = this->head; // start cursor
        SNode* prevNode = nullptr; // keeping reference of the previous node
        while (nodePtr!=nullptr && key != nodePtr->elem) { // traverse the list while the key is not found
                prevNode = nodePtr ; // keep the reference of the previous node while traversing
                nodePtr = nodePtr->next; // move to next node
        }
        if (nodePtr == nullptr) { // key not found
            cout << "\n" << key << " not found !\n";
        }    
        else { // otherwise, when the key is found
          if (this->head->elem == key) {         // first one is to be deleted
              this->head = nodePtr->next; // change head to the next node 
          }         
          else {
              prevNode->next = nodePtr->next; // change the next of the previous node to the next of the current node
          }
          delete nodePtr; // delete the node
        }
    }
    ~SLinkedList() {                    //destructor
          SNode* prevNode = nullptr; // initializing reference of the previous node
          SNode* nodePtr = this->head; // start cursor from the head 
          while (nodePtr!=nullptr) {        // traverse the list
                    prevNode = nodePtr ; // keep the reference of the previous node while traversing
                    nodePtr = nodePtr->next; // move to next node
                    delete prevNode; // delete the previous node
          }
    }
    void displayList(string header) { // function to display the list
        SNode* nodePtr = this->head; // start cursor from the head
        cout << header << ": " ;
        while (nodePtr != nullptr) { // traverse the list and print the elements
            cout << nodePtr->elem << "-";
            nodePtr = nodePtr->next;
        }
        cout << "\n";
    };
private:
    SNode* head;             // head of the list
};

And the here's the main function that does mentioned operations by using the singly linked list.

int main()
{
// Testing the singly linked list
SLinkedList * myList = new SLinkedList (); // creating a singly linked list

myList->append(100);    myList->displayList("append 100");
myList->deleteNode(100);    myList->displayList("delete 100");
myList->addFront(2);    myList->displayList("addFront 2");
myList->deleteNode(100);    myList->displayList("delete 100");
myList->append(10);    myList->displayList("append10");
myList->append(101);    myList->displayList("append101");
myList->insertAtNode(10,11);    myList->displayList("insertAtNode 10,11");
myList->insertAtNode(2,3);    myList->displayList("insertAtNode 2,3");
myList->insertAtNode(101,102);    myList->displayList("insertAtNode101,102");
myList->deleteNode(105);    myList->displayList("delete 105");
myList->deleteNode(100);    myList->displayList("delete 100");
myList->deleteNode(102);    myList->displayList("delete102");
myList->deleteNode(2);    myList->displayList("delete 2");

myList->~SLinkedList();  myList->displayList("destructed");


return 0;
}