jeudi 9 février 2017

c++ 2d linked list: how can i insert or remove an element?

Vote count: 0

the code below are my Node class, LinkedList class and main(). My problem is when i insert() and delete() from a 1d LinkedList, it shows no errors, but when i do this to a 2d LinkList, the elements doesn't change, could anyone guide me the proper way to create and use a 2d LinkedList?

template<class ItemType>
class Node
{
private:
   ItemType        item; 
   Node<ItemType>* next; 

public:
   Node();
   Node(const ItemType& anItem);
   void setNext(Node<ItemType>* nextNodePtr);
   ItemType getItem() const ;
   Node<ItemType>* getNext() const ;
};

template<class ItemType>
Node<ItemType>::Node() : next(nullptr) {}

template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr) {}

template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)
{
   next = nextNodePtr;
}

template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
   return item;
}

template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
   return next;
}

LinkedList

template<class ItemType>
class LinkedList 
{
private:
    Node<ItemType>* headPtr;                            
    int itemCount;               
    Node<ItemType>* getNodeAt(int pos) const;    
public:
    LinkedList();   
    void delete(int pos);    
    void insert(const ItemType& newEntry);
    ItemType getElement(int pos) const;
    ItemType operator [] (int index) const;
};

template<class ItemType>
void LinkedList<ItemType>::delete(int pos)
{
        Node<ItemType>* curPtr = nullptr;
        if (position == 1)
        {
            curPtr = headPtr; 
            headPtr = headPtr->getNext();
        }
        else
        {
            Node<ItemType>* prevPtr = getNodeAt(pos - 1);
            curPtr = prevPtr->getNext();
            prevPtr->setNext(curPtr->getNext());
        }  
        curPtr->setNext(nullptr);
        delete curPtr;
        curPtr = nullptr;

        itemCount--;  
} 

template<class ItemType>
void LinkedList<ItemType>::insert(const ItemType& newEntry)
{
    Node<ItemType>* curPtr = new Node<ItemType> (newEntry);
    if (getLength() == 0)
    {
        headPtr = curPtr;
    }
    else
    {
        Node<ItemType>* prevPtr = getNodeAt(getLength());
        prevPtr->setNext(curPtr);
    }
    itemCount++;
}

template<class ItemType>
Node<ItemType>* LinkedList<ItemType>::getNodeAt(int pos) const
{
   Node<ItemType>* curPtr = headPtr;
   for (int skip = 1; skip < pos; skip++)
      curPtr = curPtr->getNext();

   return curPtr;
}  

template<class ItemType>
ItemType LinkedList<ItemType>::getElement(int pos) const 
{
      Node<ItemType>* nodePtr = getNodeAt(pos);
      return nodePtr->getItem();
}

main

    LinkedList <int> s;
    for (int i = 1; i <= 10; i++)
    {
        s.insert(i);
    }

    LinkedList <int> t;
    for (int i = 1; i <= 10; i++)
    {
        t.insert(2*i + 100);
    }

    LinkedList <LinkedList <int> > ss;
    ss.insert(s);
    ss.insert(t);
    ss[1].insert(1119);
    ss[1].delete(1);
    for (int i = 1; i <= 10; i++)
    {
        cout << ss[1].getElement(i) << " ";
    }
    cout << endl;

    for (int i = 1; i <= 10; i++)
    {
        cout << ss[2].getElement(i) << " " ;
    }

the outcome remains unchanged:

1 2 3 4 5 6 7 8 9 10
102 104 106 108 110 112 114 116 118 120

expected outcome:

2 3 4 5 6 7 8 9 10 1119
102 104 106 108 110 112 114 116 118 120

asked 20 secs ago

Let's block ads! (Why?)



c++ 2d linked list: how can i insert or remove an element?

Aucun commentaire:

Enregistrer un commentaire