Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LISTADT_H
- #define LISTADT_H
- #include <iostream>
- using namespace std;
- //typedef int ItemType;
- template <class ItemType>
- class NodeType
- {
- public:
- ItemType info;
- NodeType<ItemType> * nextPtr;
- };
- template <class ItemType>
- class List
- {
- public:
- List();
- void makeEmpty();
- bool isEmpty();
- void goToStart();
- void goToNext();
- bool isAtEnd();
- ItemType CurrentItem();
- void insert(ItemType);
- void deleteCurrentItem();
- int currentPosition();
- private:
- NodeType<ItemType> * head;
- NodeType<ItemType> * current;
- NodeType<ItemType> * previous;
- int position;
- };
- template <class ItemType>
- List<ItemType>::List()
- {
- head = NULL, current = NULL, previous = NULL;
- position = 1;
- }
- template <class ItemType>
- void List<ItemType>::makeEmpty()
- {
- if (!isEmpty())
- {
- goToStart();
- while (!isAtEnd())
- {
- goToNext();
- delete previous;
- previous = current;
- }
- head = NULL;
- current = NULL;
- previous = NULL;
- position = 1;
- }
- }
- template <class ItemType>
- bool List<ItemType>::isEmpty()
- {
- if (head == NULL)
- return true;
- else
- return false;
- }
- template <class ItemType>
- void List<ItemType>::goToStart()
- {
- if (position == 1)
- return;
- else
- {
- current = head;
- previous = NULL;
- position = 1;
- }
- }
- template <class ItemType>
- void List<ItemType>::goToNext()
- {
- if (!isAtEnd())
- {
- if (current->nextPtr == NULL)
- {
- previous = current;
- current = NULL;
- position += 1;
- }
- else
- {
- previous = current;
- current = current->nextPtr;
- position += 1;
- }
- }
- }
- template <class ItemType>
- bool List<ItemType>::isAtEnd()
- {
- if (current == NULL)
- return true;
- else
- return false;
- }
- template <class ItemType>
- ItemType List<ItemType>::CurrentItem()
- {
- if (!isAtEnd())
- return current->info;
- }
- template <class ItemType>
- void List<ItemType>::insert(ItemType num)
- {
- NodeType<ItemType> * temp;
- temp = new NodeType<ItemType>;
- temp->info = num;
- if (head == NULL)
- {
- temp->nextPtr = NULL;
- head = temp;
- current = head;
- }
- else if (position == 1)
- {
- head = temp;
- head->nextPtr = current;
- current = head;
- }
- else if (!isAtEnd() && current->nextPtr == NULL)
- {
- temp->nextPtr = current;
- current = temp;
- if (previous != NULL)
- {
- previous->nextPtr = current;
- }
- }
- else
- {
- previous->nextPtr = temp;
- temp->nextPtr = current;
- current = temp;
- }
- }
- template <class ItemType>
- void List<ItemType>::deleteCurrentItem()
- {
- if (head == NULL)
- {
- cout << "List is empty." << endl;
- }
- else
- {
- if (current == head)
- {
- head = head->nextPtr;
- current->nextPtr = 0;
- delete current;
- current = head;
- }
- else if (isAtEnd())
- {
- cout << "End of list cannot delete. Move the the start of list." << endl;
- }
- else if (current->nextPtr == NULL)
- {
- delete current;
- current = 0;
- previous->nextPtr = NULL;
- current = previous->nextPtr;
- }
- else
- {
- previous->nextPtr = current->nextPtr;
- current->nextPtr = 0;
- delete current;
- current = previous->nextPtr;
- }
- }
- }
- template <class ItemType>
- int List<ItemType>::currentPosition()
- {
- return position;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement