0

Hey i want to know how to make my single linked list a double can anyone help?

#include "MyLinkedList.h"
#include <iostream>

int main()
{
    SListNode<int>* list = new SListNode<int>;
    //Set 3 LinkedList nodes to contain 3 int variables.
    list->m_data = 10;//(1).
    list->m_next = new SListNode<int>;
    list->m_next->m_data = 20;//(2).
    list->m_next->m_next = new SListNode<int>;
    list->m_next->m_next->m_data = 30;//(3).
    //Print out the LinkedList.
    std::cout << "list = { ";
    std::cout << list->m_data << ", ";
    std::cout << list->m_next->m_data << ", ";
    std::cout << list->m_next->m_next->m_data << " }" << std::endl;

    list->insertAfter(15);
    list->m_next->m_next->insertAfter(25);

    std::cout << "list = { ";
    std::cout << list->m_data << ", ";
    std::cout << list->m_next->m_data << ", ";
    std::cout << list->m_next->m_next->m_data << ", ";
    std::cout << list->m_next->m_next->m_next->m_data << ", ";
    std::cout << list->m_next->m_next->m_next->m_next->m_data << " }" << std::endl;

    SListNode<int>* iterator = list;

    //print first (10)
    std::cout << "list[0] = " << iterator->m_data << std::endl;
    //skip to third (25) and print it
    for(int i = 0; i < 3; i++)
        iterator = iterator->m_next;
    std::cout << "list[3] = " << iterator->m_data << std::endl;
    getchar();
    getchar();
}

//Declaring a Template.
template<class Datatype>
class SListNode
{
public:
//********Member Variables********
    Datatype m_data;
    SListNode<Datatype>* m_next;
//*********Methods********

//Insert Method.
    void insertAfter(Datatype p_data)
    {
    //Create the new node.
    SListNode<Datatype>* newnode = new SListNode<Datatype>;
    newnode->m_data = p_data;
    //Make the new node point to the next node.
    newnode->m_next = m_next;
    //Make the previous node point to the new node.
    m_next = newnode;
    }
};

Edited by deceptikon: Fixed code tags

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by raptr_dflo
0

This should get you started:

template<class Datatype>
class DListNode
{
public:
    //********Member Variables********
    Datatype m_data;
    DListNode<Datatype>* m_prev;
    DListNode<Datatype>* m_next;
    //*********Methods********
    ...
};

Edited by raptr_dflo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.