Building a List Class - C++ Data Structure

C++ examples for Data Structure:Linked List

Description

Building a List Class

Demo Code

#include <iostream> 
#include <array> 
#include <vector> 
#include <ios> 

using namespace std; 


class ListNode /*from   ww w. j  a v  a  2s.c  o  m*/
{ 
private: 
        void* m_Data = nullptr; 
        ListNode* m_Last = nullptr; 
        ListNode* m_Next = nullptr; 
  
public: 
        ListNode(void* data) 
        : m_Data{data} 
        { 
        } 
  
        void* GetData() 
        { 
                return m_Data; 
        } 
  
        void SetLast(ListNode* last) 
        { 
                m_Last = last; 
        } 
        ListNode* GetLast() 
        { 
                return m_Last; 
        } 
  
        void SetNext(ListNode* next) 
        { 
                m_Next = next; 
        } 
  
        ListNode* GetNext() 
        { 
                return m_Next; 
        } 
}; 

int main(int argc, char *argv [])
{   
 
        unsigned int firstData = 1; 
        ListNode first(&firstData); 
  
        unsigned int secondData = 2; 
        ListNode second(&secondData); 
  
        unsigned int thirdData = 3; 
        ListNode third(&thirdData); 
  
        first.SetNext(&second); 
  
        second.SetLast(&first); 
        second.SetNext(&third); 
  
        third.SetLast(&third); 
  
        for (ListNode* iter = &first; iter != nullptr; iter = iter->GetNext()) 
        { 
                unsigned int* number = static_cast<unsigned int*>(iter->GetData()); 
                cout << *number << endl; 
        } 
        return 0; 
}

Result


Related Tutorials