Linked List using Templates in C++ program is given below:
// linkedlist.cpp
// linked list using templates
// See also: Link List program in C++
#include "stdafx.h" //Remove this line if you see any error.
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
template <typename T>
struct link //one element of list
{
T data; //data item
link* next; //pointer to next link
};
////////////////////////////////////////////////////////////////
template <class T>
class linklist //a list of links
{
private:
link<T>* first; //pointer to first link
public:
linklist() //no-argument constructor
{ first = NULL; } //no first link
void additem(T d); //add data item (one link)
void display(); //display all links
};
//-------------------------------------------------------------void
template <class T>
void linklist<T>::additem(T d) //add data item
{
link<T> *newLink = new link<T>;
newLink->data = d;
//insert at head of link list
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
template <class T>
void linklist<T>::display() //display all links
{
link<T>* current = first; //set ptr to first link
while( current != NULL ) //quit on last link
{
cout << current->data << endl; //print data
current = current->next; //move to next link
}
}
////////////////////////////////////////////////////////////////
int main()
{
linklist<int> li; //make linked list
li.additem(25); //add four items to list
li.additem(36);
li.additem(49);
li.display(); //display entire list
linklist<char> lch; //make linked list
lch.additem('a'); //add four items to list
lch.additem('b');
lch.additem('c');
lch.display(); //display entire list
return 0;
}
#include
ReplyDeleteusing namespace std;
template
class LinkedList
{
private:
struct Node
{
struct Node* prev;
struct Node* next;
T data;
};
struct Node* pNode;
public:
LinkedList();
~LinkedList();
void append(T);
void addAtBeg(T);
void addAfter(int, T);
void deleteNode(T);
void display();
void count();
};
template
LinkedList::LinkedList()
{
pNode = NULL;
}
template
LinkedList::~LinkedList()
{
struct Node* temp = pNode;
while(pNode)
{
temp = pNode;
pNode = pNode->next;
delete temp;
}
}
template
void LinkedList::append(T data)
{
if(pNode == NULL)
{
pNode = new struct Node;
pNode->next = NULL;
pNode->prev = NULL;
pNode->data = data;
return;
}
else
{
struct Node* temp = pNode;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new struct Node;
temp->next->prev = temp;
temp->next->next = NULL;
temp->next->data = data;
}
}
template
void LinkedList::addAtBeg(T data)
{
if(pNode == NULL)
{
pNode = new struct Node;
pNode->next = NULL;
pNode->prev = NULL;
pNode->data = data;
return;
}
else
{
struct Node *temp = new struct Node;
temp->next = pNode;
pNode->prev = temp;
temp->prev = NULL;
temp->data = data;
pNode = temp;
}
}
template
void LinkedList::addAfter(int position, T data)
{
cout << "Assumed will have some element and position is a valid position" <next;
}
struct Node *tempNode = new struct Node;
tempNode->data = data;
tempNode->prev = temp;
tempNode->next = temp->next;
temp->next = tempNode;
}
template
void LinkedList::deleteNode(T data)
{
cout << "Here also not handled the condition, assumed data is in the link"<data != data)
{
temp = temp->next;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
template
void LinkedList::display()
{
cout<< "Not checked all the condition,assumed that list have some element" <next;
}
cout << "Number of elements in the list = " << count << endl;
}
int main()
{
LinkedList *pLinkObj = new LinkedList();
pLinkObj->append(2);
pLinkObj->append(4);
pLinkObj->append(6);
pLinkObj->append(8);
pLinkObj->append(10);
cout << "======" << endl;
pLinkObj->display();
cout << "======" <addAtBeg(1);
cout << "======" << endl;
pLinkObj->display();
cout << "======" <addAfter(4, 7);
cout << "======" << endl;
pLinkObj->display();
cout << "======" <deleteNode(8);
pLinkObj->display();
pLinkObj->count();
delete pLinkObj;
return 0;
}
In the above code in place of "template" please use "template"
ReplyDeleteerror in this code { T was not declared }
Delete