Last active
August 29, 2015 14:07
-
-
Save katsuobushiFPGA/9643247ef87e31a4920c to your computer and use it in GitHub Desktop.
This file contains 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
//templateNode.h | |
template <class T> class Node{ | |
public: | |
Node(); | |
Node(T val); | |
~Node(); | |
void setValue(T val); | |
T getValue(); | |
public: | |
T value; | |
Node<T> *next; | |
Node<T> *prev; | |
}; | |
//実装部 | |
template <class T> | |
Node<T>::Node(){ | |
value = NULL; | |
next = nullptr; | |
prev = nullptr; | |
}; | |
template <class T> | |
Node<T>::Node(T val){ | |
value = val; | |
next = nullptr; | |
prev = nullptr; | |
}; | |
template <class T> | |
Node<T>::~Node(){}; | |
template <class T> | |
void Node<T>::setValue(T val){ | |
value = val; | |
} | |
template <class T> | |
T Node<T>::getValue(){ | |
return value; | |
} | |
//templateSLinkedList | |
//SingleLinkedList.h | |
#include <stdio.h> | |
#include "templateNode.h" | |
template <class T> | |
class SingleLinkedList{ | |
public: | |
SingleLinkedList(); | |
~SingleLinkedList(); | |
Node<T> *FindLast(); | |
void add(Node<T> *n); | |
void remove(int index); | |
T getAt(int index); | |
public: | |
Node<T> *head = NULL; | |
public: | |
int m_length = 0; | |
}; | |
template <class T> | |
SingleLinkedList<T>::SingleLinkedList(){}; | |
template <class T> | |
SingleLinkedList<T>::~SingleLinkedList(){}; | |
template <class T> | |
Node<T> *SingleLinkedList<T>::FindLast(){ | |
Node<T> *follow = head; | |
if (head == NULL){ | |
return NULL; | |
} | |
else{ | |
while (follow->next != NULL){ | |
follow = follow->next; | |
} | |
return follow; | |
} | |
}; | |
template <class T> | |
void SingleLinkedList<T>::add(Node<T> *n){ | |
Node<T> *p_last = FindLast(); | |
if (head == NULL){ | |
head = n; | |
m_length++; | |
} | |
else{ | |
p_last->next = n; | |
m_length++; | |
} | |
} | |
template <class T> | |
void SingleLinkedList<T>::remove(int index){ | |
Node<T> *p_follow = head; | |
Node<T> *p_prev = nullptr; | |
if (index > m_length - 1){ | |
return; | |
} | |
else{ | |
if (head == NULL)return; | |
for (int i = 0; i < index; i++){ | |
if (i==index - 1)p_prev = p_follow; | |
p_follow = p_follow->next; | |
} | |
//head | |
if (p_prev == nullptr){ | |
if (p_follow->next == nullptr){ | |
head = NULL; | |
} | |
else{ | |
head = p_follow->next; | |
} | |
delete p_follow; | |
} | |
//last | |
else if (p_follow->next == nullptr){ | |
p_prev->next = nullptr; | |
delete p_follow; | |
} | |
else{ | |
p_prev->next = p_follow->next; | |
delete p_follow; | |
} | |
m_length--; | |
} | |
} | |
template <class T> | |
T SingleLinkedList<T>::getAt(int index){ | |
Node<T> *p_follow = head; | |
if (index > m_length - 1){ | |
return NULL; | |
} | |
for (int i = 0; i < index; i++){ | |
p_follow = p_follow->next; | |
} | |
return p_follow->value; | |
} | |
//main.cpp | |
#include <stdio.h> | |
#include "templateSList.h" | |
int main(){ | |
SingleLinkedList<int> *s = new SingleLinkedList<int>(); | |
Node<int> *n= new Node<int>(2); | |
for (int i = 0; i < 10; i++){ | |
s->add(new Node<int>(i)); | |
} | |
for (int i = 0; i < s->m_length; i++){ | |
printf("getAt(%d):%d\n", i,s->getAt(i)); | |
} | |
printf("\n"); | |
s->remove(0); | |
s->remove(9);//Not existing element | |
s->remove(8); | |
for (int i = 0; i < s->m_length; i++){ | |
printf("getAt(%d):%d\n", i, s->getAt(i)); | |
} | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment