Skip to content

Instantly share code, notes, and snippets.

@katsuobushiFPGA
Last active August 29, 2015 14:07
Show Gist options
  • Save katsuobushiFPGA/7c61a7d83b260da8235c to your computer and use it in GitHub Desktop.
Save katsuobushiFPGA/7c61a7d83b260da8235c to your computer and use it in GitHub Desktop.
//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;
}
//LinkedList.h
#include <stdio.h>
#include "Node.h"
class LinkedList{
public:
LinkedList();
~LinkedList();
void add(Node *n);
void remove();
void print();
public:
Node *head=new Node(NULL);
};
LinkedList::LinkedList(){};
LinkedList::~LinkedList(){};
void LinkedList::add(Node *n){
Node *follow = head;
if (head->value == NULL)
head->value = n->value;
else{
while (follow -> next != nullptr){
follow = follow -> next;
}
//双方向リスト
follow->next = n;
n->prev = follow;
}
}
void LinkedList::remove(){};
void LinkedList::print(){
printf("head->next:%d\n",*head->next);
}
//main.cpp
#include "LinkedList.h"
#include <stdio.h>
int main(){
LinkedList *l = new LinkedList();
l->add(new Node(1));
l->add(new Node(2));
l->add(new Node(3));
l->add(new Node(4));
printf("head:%d\n", *l->head);
printf("next:%d", *l->head->next);
printf("prev:%d\n", *l->head->next->prev);
printf("next:%d", *l->head->next->next);
printf("prev:%d\n", *l->head->next->next->prev);
printf("next:%d", *l->head->next->next->next);
printf("prev:%d\n", *l->head->next->next->next->prev);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment