Home > Blockchain >  How To Output Characters Using Linked Lists?
How To Output Characters Using Linked Lists?

Time:09-17

#include <iostream>
using namespace std;

class FloatList
{
  private:
      // Declare a structure for the list
      struct ListNode
       {
           float value;
           struct ListNode *next;
       }; 
      ListNode *head;   // List head pointer
    
  public:
      FloatList();  // Constructor
      void appendNode(float num);
};

FloatList::FloatList()  
 { 
    head = NULL; 
 }

void FloatList::appendNode(float num)
{
    ListNode *newNode, *nodePtr;
    // Allocate a new node & store num
    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;
  
    // If there are no nodes in the list
    // make newNode the first node
    if (!head)
        head = newNode;
    else    // Otherwise, insert newNode at end
    {
        // Initialize nodePtr to head of list
        nodePtr = head;
        // Find the last node in the list
        while (nodePtr->next)
            nodePtr = nodePtr->next; 
        // Insert newNode as the last node
        nodePtr->next = newNode;
    } cout << num << " has been APPENDED!" << endl;
}


int main()
{
    FloatList list;

    list.appendNode(2.5);
    list.appendNode(7.9);
    list.appendNode(12.6);
}

INSTRUCTIONS : Convert the above Linked List ADT with Append Operation into a Linked List Template for it to be capable of handling data of different data types. Once your program is complete, change your main program to the following:

int main()
 {
     FloatList list;

     list.appendNode('a');
     list.appendNode('b');
     list.appendNode('c');
   cout <<  "Successful Append!" << endl;
 }

I am able to print the floats (2.5) (7.9) and (12.6), but I cannot edit the program to be able to print 'a', 'b', and 'c'

This is the expected output:

a has been APPENDED!
b has been APPENDED!
c has been APPENDED!
Successful Append!

CodePudding user response:

If you wanted to make it work for string types, or char types, you would have to turn your linked list into templated class. Look at your definition of node:

struct ListNode
       {
           float value;
           struct ListNode *next;
       }; 

You defined float as value, so how do you expect it co hold char type? I changed your linkedlist so now it can take any type you desire:

#include <iostream>
using namespace std;
template <typename T>
class FloatList
{
private:
    // Declare a structure for the list
    struct ListNode
    {
        T value;
        struct ListNode* next;
    };
    ListNode* head;   // List head pointer

public:
    FloatList()
    {
        head = NULL;
    }// Constructor
    void appendNode(T num)
    {
        ListNode* newNode, * nodePtr;
        // Allocate a new node & store num
        newNode = new ListNode;
        newNode->value = num;
        newNode->next = NULL;

        // If there are no nodes in the list
        // make newNode the first node
        if (!head)
            head = newNode;
        else    // Otherwise, insert newNode at end
        {
            // Initialize nodePtr to head of list
            nodePtr = head;
            // Find the last node in the list
            while (nodePtr->next)
                nodePtr = nodePtr->next;
            // Insert newNode as the last node
            nodePtr->next = newNode;
        } cout << num << " has been APPENDED!" << endl;
    }
};



int main()
{
    FloatList<float> list;

    list.appendNode(2.5);
    list.appendNode(7.9);
    list.appendNode(12.6);
    FloatList<char> char_list;
    char_list.appendNode('c');
    char_list.appendNode('d');
    char_list.appendNode('e');
}

As your list is now templated class, you must specify what is the templated type:

FloatList<float> list;

The solution is really nice and simple, all you had to do is to change two things in your class definition: a) tell compiler that this class is templated

template <typename T>
    class FloatList

b) swap all floats into T (our desired type):

 float value;
 T value;
 void appendNode(float num) 
 void appendNode(T num)
  • Related