카테고리 없음

doubly linked list

fw93 2018. 3. 24. 16:23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
struct listnode {
    int data;
    listnode * prev;
    listnode * next;
};
 
class list {
private:
    listnode * head;
    listnode * tail;
public:
    list() {
        head = NULL;
        tail = NULL;
    }
    void append(int d);
    void remove();
    void print();
};
 
void list::append(int d) {
    if (head == NULL) {
        listnode * tmp2 = new listnode;
        tmp2->data = d;
        tmp2->prev = NULL;
        tmp2->next = NULL;
        head = tmp2;
        tail = tmp2;
        return;
    }
    listnode * tmp = new listnode;
    tmp->data = d;
    tmp->prev = tail;
    tmp->next = NULL;
    tail->next = tmp;
    tail = tail->next;
}
 
void list::remove() {
    if (tail == head) {
        return;
    }
    tail = tail->prev;
    tail->next = NULL;
}
 
void list::print() {
    listnode * tmp = head;
    while (tmp != NULL) {
        cout << tmp->data << " ";
        tmp = tmp->next;
    }
    cout << endl;
}
 
int main() {
    list test;
    test.append(1);
    test.append(2);
    test.append(3);
    test.remove();
    test.print();
    while(1){ }
    return 0;
}
cs