#include<iostream>
using namespace std;
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmpty();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
void noDuplicates(const type& newitem);
const linkedListType<Type> & retrieveat(int loc)
//protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template<class Type>
class orderedLinkedListType: public linkedListType<Type>
{
public:
bool searcho(const Type& searchItem);
void insertNode(const Type& newItem);
void deleteNode(const Type& newItem);
};
template<class Type>
ostream& operator<<(ostream&, const linkedListType<Type>&);
}
#include"linklist.h"
#include<cassert>
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
nodeType<Type> *current; // pointer to traverse the list
current = list.first;
while(current != NULL)
{
osObject<<current->info<<" ";
current = current->link;
}
return osObject;
}
template<class Type>
const linkedListType<Type>& linkedListType<Type>:

perator=(const linkedListType<Type>& otherList)
{
if(this != &otherList) //avoid self-copy
copyList(otherList);
return *this;
}
template<class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template<class Type>
bool linkedListType<Type>::isEmpty()
{
return(first == NULL);
}
template<class Type>
int linkedListType<Type>::length()
{
return count;
}
template<class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template<class Type>
Type linkedListType<Type>::front()
{
assert(last != NULL);
return first->info;
}
template<class Type>
Type linkedListType<Type>::back()
{
assert(last != NULL);
return last->info;
}
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
bool found;
current = first;
found = false;
while(current != NULL && !found)
if(current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if(last == NULL)
last = newNode;
}
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
count++;
}
template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL)
cout<<"Cannot delete from an empty list."<<endl;
else
{
if(first->info == deleteItem)
{
current = first;
first = first->link;
count--;
if(first == NULL) // list has only one node
last = NULL;
delete current;
}
else // search the list for the node with the given info
{
found = false;
trailCurrent = first;
current = first->link;
while(current != NULL && !found)
{
if(current->info != deleteItem)
{
trailCurrent = current;
current = current->link;
}
else
found = true;
}
if(found)
{
trailCurrent->link = current->link;
count--;
if(last == current) //node to be deleted is the last node
last = trailCurrent;
delete current;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
}
template<class Type>
linkedListType<Type>::linkedListType()
{
first = NULL;
last = NULL;
count = 0;
}
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
{
first = NULL;
copyList(otherList);
}
template<class Type>
linkedListType<Type>::~linkedListType()
{
destroyList();
}
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if(first != NULL)
destroyList();
if(otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the lis to be copied
count = otherList.count;
first = new nodeType<Type>; //create the node
assert(first != NULL);
first->info = current->info; //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node
current = current->link; //make current point to the next node
while(current != NULL) //copy the remaining list
{
newNode = new nodeType<Type>; //create a node
assert(newNode != NULL);
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last node
current = current->link; //make current point to the next node
}
}
}
template<class Type>
bool orderedLinkedListType<Type>::searcho(const Type& searchItem)
{
bool found;
nodeType<Type> *current;
found = false;
current = first;
while(current != NULL && !found)
if(current->info >=searchItem)
found = true;
else
current = current->link;
if(found)
found = (current->info == searchItem);
return found;
}
template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newItem; //store newitem in the node
newNode->link = NULL; //set the link field of the node to NULL
if(first == NULL) //case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == first) //case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}
}
template<class Type>
void orderedLinkedListType<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; // pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL) //case 1
cout<<"Cannot delete from an empty list."<<endl;
else
{
current = first;
found = false;
while(current != NULL && !found)
if(current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == NULL) //case 4
cout<<"Item to be deleted is not in the list."<<endl;
else
if(current->info == deleteItem) //item to be deleted is in the list
{
if(first == current) //case 2
{
first = first->link;
delete current;
}
else //case 3
{
trailCurrent->link = current->link;
delete current;
}
count--;
}
else
cout<<"Item to be deleted is not in the list."<<endl;
}
}
tamplate<class type>
void orderedLinkedListType<Type>::noDuplicates(const type& newitem)
{
nodetype<type> *current;
bool found= false;
current=first;
while(current !=NULL &&!found)
{ if (current->info !=newitem)
current =current->link;
else
found= true;
cout<<" Item you are trying to insert is already in the list."<<endl;
}
if (found)
list.insertLast(newitem)
}
#include "linklist.h"
#include "lim.h"
int main()
{
int size;
linkedListType<int>list;
cout <<"Enter the size of the List:"<<endl;
cin>>size>>endl;
int item;
int newitem;
for (int i=0;i<size;i++)
{
cout<<"Enter a number to be inserted: "<<endl;
cin>>item;
item== newitem;
list.noDuplicates(newitem);
}