Created
December 29, 2018 05:58
-
-
Save xProgrammer-007/5c31ac454170e37f036b524788416373 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "LinkedList.h" | |
template <typename T> struct Node{ | |
T data; | |
Node* next; | |
}; | |
template <class T> class LinkedList { | |
private: | |
Node<T> *head; | |
Node<T> *tail; | |
int count; | |
public: | |
LinkedList() { | |
head = NULL; | |
tail = NULL; | |
count = 0; | |
} | |
void display(); | |
void insertAtBegin(T data); | |
void insertAtEnd(T data); | |
void insertAtPosition(T data ,int pos); | |
void removeAtPosition(int pos); | |
void removeFromBegin(); | |
void removeFromEnd(); | |
}; | |
template <class T> | |
void LinkedList<T>::display(){ | |
Node<T>* temp = head; | |
while (temp != NULL){ | |
cout << temp->data << endl ; | |
temp = temp->next; | |
} | |
cout << "Count is :" << count << endl; | |
} | |
template <class T> | |
void LinkedList<T>::insertAtBegin(T data){ | |
Node<T> *temp = new Node<T>; | |
temp->data = data; | |
temp->next = NULL; | |
if(count == 0){ | |
head = temp; | |
tail = temp; | |
}else{ | |
temp->next = head; | |
head = temp; | |
} | |
count++; | |
} | |
template <class T> | |
void LinkedList<T>::insertAtEnd(T data){ | |
Node<T> *temp = new Node<T>; | |
temp->data = data; | |
temp->next = NULL; | |
if(count == 0 ){ | |
head = tail = temp; | |
}else{ | |
temp->next = tail; | |
tail = temp; | |
tail->next = NULL; | |
} | |
count++; | |
} | |
template <class T> | |
void LinkedList<T>::insertAtPosition(T data,int pos){ | |
Node<T> *temp ,*cur , *prev; | |
temp = new Node<T>; | |
prev = new Node<T>; | |
temp->data = data; | |
cur = new Node<T>; | |
cur = head; | |
for (int i = 0; i < pos; ++i) { | |
prev = cur; | |
cur = cur->next; | |
} | |
prev->next = temp; | |
temp->next = cur; | |
count++; | |
} | |
template <class T> | |
void LinkedList<T>::removeFromBegin(){ | |
Node<T> *temp = head; | |
head = head->next; | |
delete temp; | |
count--; | |
} | |
template <class T> | |
void LinkedList<T>::removeFromEnd(){ | |
Node<T> *cur = new Node<T>; | |
Node<T> *prev = new Node<T>; | |
cur = head; | |
while(cur->next != NULL){ | |
prev = cur; | |
cur = cur->next; | |
} | |
tail = prev; | |
tail->next = NULL; | |
cout << "tail->next" << tail->next << endl; | |
delete cur; | |
count--; | |
} | |
template <class T> | |
void LinkedList<T>::removeAtPosition(int p){ | |
Node<T> * prev = new Node<T>; | |
Node<T> *cur = new Node<T>; | |
cur = head; | |
for (int i = 0; i < p; ++i) { | |
prev = cur; | |
cur= cur->next; | |
} | |
prev->next = cur->next; | |
count--; | |
} | |
int main() { | |
LinkedList <int> linkedList; | |
linkedList.insertAtBegin(100); | |
linkedList.insertAtBegin(200); | |
linkedList.insertAtBegin(300); | |
linkedList.removeFromEnd(); | |
linkedList.insertAtEnd(1000); | |
// linkedList.insertAtPosition(2000,3); | |
// linkedList.removeFromBegin(); | |
linkedList.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment