A singly linked list, often called linear linked list is a dynamic data structure which may grow or shrink linearly and growing and shrinking depend on the operation made. In this type of linked list, there is only one link in each node, where link points to the next node in the list. The link of last node is a NULL pointer.

#### Template Implementation Of Singly Linked List Using C++

``````
#include<iostream>
using namespace std;

template<typename T>class Node{
private:
T data;
Node<T>* next;
public:
Node(){
this->next = NULL;
}
};

private:
public:
}

Node<T>* node = new Node<T>[1];
node->data = item;
return;
}
while(temp->next != NULL){
temp = temp->next;
}
temp->next = node;
}

Node<T>* node = new Node<T>[1];
node->data = item;
return;
}
cout<<"new node added at front !"<<endl;
}

if(index > length() || index < 0){
cout<<"index out of bound !"<<endl;
return;
}
Node<T>* node = new Node<T>[1];
node->data = item;
int count = 0;
while(temp != NULL && count < index){
if(count == index-1){
if(temp->next != NULL){
node->next = temp->next;
}
temp->next = node;
cout<<"new node added at index "<<index<<" !"<<endl;
break;
}
count++;
temp = temp->next;
}

}

int length(){
int len = 0;
while(temp != NULL){
len++;
temp = temp->next;
}
return len;
}

void displayAll(){
return;
}
while(temp != NULL){
cout<<temp->data<<" | ";
temp = temp->next;
}
cout<<endl<<"--------------------------"<<endl;
}

void remove(){
return;
}
cout<<"last item removed"<<endl;
return;
}

while(temp != NULL){
if(temp->next->next == NULL){
temp->next = NULL;
cout<<"last item removed"<<endl;
break;
}
temp = temp->next;
}

}

void remove(int index){
return;
}
if(index >= length() || index < 0){
cout<<"index out of bound !"<<endl;
return;
}
if(index == 0){
removeFront();
cout<<"item removed at index "<<index<<endl;
return;
}

int count = 0;
while(temp != NULL){
if(count == index - 1){
temp->next = temp->next->next;
cout<<"item removed at index "<<index<<endl;
break;
}
count++;
temp = temp->next;
}
}

void removeFront(){
return;
}
cout<<"front item removed"<<endl;
}

T get(int index){
return -9999;
}
if(index >= length() || index < 0){
cout<<"index out of bound !"<<endl;
return -9999;
}
if(index == 0){
}
int count = 0;
T res;
while(temp != NULL){
if(count++ == index){
res = temp->data;
break;
}
temp = temp->next;
}
return res;
}

};

int main(){
int ch, item, index;
bool quit = false;
do{
cout<<"===================================="<<endl;
cout<<"select option :"<<endl;
cout<<"1: insert back"<<endl;
cout<<"2: insert front"<<endl;
cout<<"3: insert at index"<<endl;
cout<<"4: display items"<<endl;
cout<<"5: get item at index"<<endl;
cout<<"6: delete back"<<endl;
cout<<"7: delete front"<<endl;
cout<<"8: delete at index"<<endl;
cout<<"9: exit"<<endl;
cin>>ch;
cout<<"===================================="<<endl;
switch (ch)
{
case 1:
cout<<"enter item to insert:"<<endl;
cin>>item;
break;
case 2:
cout<<"enter item to insert:"<<endl;
cin>>item;
break;
case 3:
cout<<"enter item to insert:"<<endl;
cin>>item;
cout<<"enter index:"<<endl;
cin>>index;
break;
case 4:
list.displayAll();
break;

case 5:
cout<<"enter index:"<<endl;
cin>>index;
cout<<"item at index "<<index<<": "<<list.get(index)<<endl;
break;
case 6:
list.remove();
break;
case 7:
list.removeFront();
break;
case 8:
cout<<"enter index:"<<endl;
cin>>index;
list.remove(index);
break;
case 9:
quit = true;
break;
default:
cout<<"invalid selection"<<endl;
break;
}
}while(!quit);
return 0;
}
``````