Skip to content

Instantly share code, notes, and snippets.

@katsuobushiFPGA
Created October 3, 2014 14:25
Show Gist options
  • Save katsuobushiFPGA/e60a6bd65ee32d6b8118 to your computer and use it in GitHub Desktop.
Save katsuobushiFPGA/e60a6bd65ee32d6b8118 to your computer and use it in GitHub Desktop.
//DLinkedList.h
//Node.h
//main.cpp
#include <stdio.h>
#include "DLinkedList.h"
int main(){
DLinkedList *d = new DLinkedList();
for (int i = 0; i <= 10; i++){
d->add(new Node(i));
}
for (int i = 0; i < d->m_length; i++){
d->getAtPrint(i);
}
for (int i = 0; i < d->m_length; i++){
d->getAtPrint(i);
}
getchar();
return 0;
}
//DLinkedList.h
#include <stdio.h>
#include "Node.h"
class DLinkedList{
public:
DLinkedList();
~DLinkedList();
Node* FindLast();
void add(Node *n);
void remove(int index);
int getAt(int index);
void getAtPrint(int index);//デバッグ用関数
public:
Node *head = NULL;
public:
int m_length = 0;
};
DLinkedList::DLinkedList(){};
DLinkedList::~DLinkedList(){};
Node *DLinkedList::FindLast(){
Node *follow = head;
if (head == NULL){
return NULL;
}
else{
while (follow->next != nullptr){
follow = follow->next;
}
return follow;
}
};
void DLinkedList::add(Node *n){
Node *p_last = FindLast();
if (head == NULL){
head = n;
m_length++;
}
else{
p_last->next = n;
n->prev = p_last;
m_length++;
}
}
void DLinkedList::remove(int index){
Node *p_follow = head;
Node *p_prev = nullptr;
Node *p_next = 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;
}
p_next = p_follow->next;
//head
if (p_prev == nullptr){
if (p_next == nullptr){
head = NULL;
}
else{
head = p_next;
head->prev = nullptr;
delete p_follow;
}
}
//last
else if (p_follow->next == nullptr){
p_prev->next = nullptr;
delete p_follow;
}
else{
p_prev->next = p_next;
p_next->prev = p_prev;
delete p_follow;
}
m_length--;
}
}
int DLinkedList::getAt(int index){
Node *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;
}
void DLinkedList::getAtPrint(int index){
Node *p_follow = head;
if (index > m_length - 1){
return;
}
for (int i = 0; i < index; i++){
p_follow = p_follow->next;
}
if (p_follow == nullptr)return;
if (p_follow->next == nullptr)
printf("getAt(%d): here:%p prev:%p next:%p\n", index, p_follow, p_follow->prev, NULL);
else if (p_follow->prev == nullptr)
printf("getAt(%d): here:%p prev:%p next:%p\n", index, p_follow, NULL, p_follow->next);
else
printf("getAt(%d): here:%p prev:%p next:%p\n", index, p_follow, p_follow->prev, p_follow->next);
}
//Node.h
//int
class Node{
public:
Node();
Node(int val);
~Node();
void setValue(int val);
int getValue();
public:
int value;
Node *next;
Node *prev;
};
//実装部
Node::Node(){
value = NULL;
next = nullptr;
prev = nullptr;
};
Node::Node(int val){
value = val;
next = nullptr;
prev = nullptr;
};
Node::~Node(){};
void Node::setValue(int val){
value = val;
}
int Node::getValue(){
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment