Skip to content

Instantly share code, notes, and snippets.

@kageurufu
Last active December 17, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kageurufu/5640618 to your computer and use it in GitHub Desktop.
Save kageurufu/5640618 to your computer and use it in GitHub Desktop.
linkedlist
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
template <class T>
class linkedList {
struct node {
node* ptr;
T data;
};
node* list;
public:
linkedList() { list = NULL; };
void unshift(T data);
void push(T data);
void insert(int index, T data);
T shift();
T pop();
int count();
T operator[] (int index);
void deleteAt(int index);
void print();
};
template <class T>
void linkedList<T>::unshift(T data) {
node* next = new node();
next->data = data;
next->ptr = list;
list = next;
}
template <class T>
void linkedList<T>::push(T data) {
node* next = new node();
next->data = data;
next->ptr = NULL;
node* temp = list;
while(temp->ptr != NULL) {
temp = temp->ptr;
}
temp->ptr = next;
}
template <class T>
void linkedList<T>::insert(int index, T data) {
node* next = new node();
next->data = data;
node* temp = list;
index--;
while(index--) {
temp = temp->ptr;
}
next->ptr = temp->ptr;
temp->ptr = next;
}
template <class T>
T linkedList<T>::pop() {
if(list == NULL) {
throw "Index of out bounds"
}
node* temp = list;
node* last = NULL;
while(temp->ptr != NULL) {
last = temp;
temp = temp->ptr;
}
T data = temp->data;
free(temp);
last->ptr = NULL;
return data;
}
template <class T>
T linkedList<T>::shift() {
if(list == NULL) {
throw "Index of out bounds";
}
node* head = list;
T data = head->data;
list = list->ptr;
free(head);
return data;
}
template <class T>
void linkedList<T>::print() {
node* temp = list;
while(temp != NULL) {
std::cout << temp->data << std::endl;
temp = temp->ptr;
}
};
template <class T>
int linkedList<T>::count() {
if(list == NULL) {
return 0;
}
node* temp = list;
int i = 0;
while(temp != NULL) {
i++;
temp = temp->ptr;
}
return i;
}
template <class T>
T linkedList<T>::operator[](int index) {
if(index == 0) {
throw "Index of out bounds"
}
node* temp = list;
while(--index) {
temp = temp->ptr;
if(temp == NULL) {
throw "Index of out bounds"
}
}
return temp->data;
}
template <class T>
void linkedList<T>::deleteAt(int index) {
if(list == NULL){
throw "Index of out bounds";
}
if(index == 0) {
this->shift();
return;
}
node* temp = list;
node* last = NULL;
while(index-- && temp != NULL){
last = temp;
temp = temp->ptr;
}
if(temp == NULL) {
throw "Index of out bounds";
}
last->ptr = temp->ptr;
free(temp);
}
#endif
#include <iostream>
#include "linkedlist.h"
using namespace std;
int main(int argc, char* argv) {
linkedList<char*> list;
list.print();
list.unshift("testing");
list.push("ending?");
list.unshift("lol");
list.insert(1, "test");
list.print();
getchar();
list.deleteAt(0);
list.deleteAt(1);
list.print();
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment