-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked.cpp
More file actions
174 lines (152 loc) · 3.29 KB
/
linked.cpp
File metadata and controls
174 lines (152 loc) · 3.29 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>
using namespace std;
#define SIZE 100
#define EOS '\0'
class node {
public :
string name;
double score;
node *link;
void set_data(string s, double n);
};
void node::set_data(string s, double n)
{
name = s;
score = n;
}
class my_list {
node *head, *tail;
public :
my_list();
void add_to_head(node t);
void add_to_tail(node t);
node delete_from_head();
int num_nodes();
bool list_empty();
void invert();
friend bool list_equal(my_list a, my_list b);
};
my_list::my_list()
{
head = NULL;
tail = NULL;
}
void my_list:: add_to_head(node t)
{
node *p;
p = new node;
(*p) = t;
p-> link = head;
head = p;
if (tail == NULL) // 이전 상태가empty였다면, Tail도첫node로
tail = p;
}
void my_list:: add_to_tail(node t)
{
node *p;
p = new node;
(*p) = t;
p-> link = NULL;
if (tail != NULL) // 이전상태가empty가아니라면
tail->link = p;
else // 이전상태가empty였다면head도변경
head = p;
tail = p;
}
node my_list:: delete_from_head()
{
node temp;
node *t;
t = head;
temp = *head;
head = head->link;
delete t;
if (head == NULL) // 삭제 후 empty가 되면, tail값도 조정
tail = NULL;
return temp;
}
int my_list:: num_nodes( )
{
node *t;
int count = 0;
for (t = head; t != NULL; t = t->link)
count++;
return count;
}
bool my_list:: list_empty( )
{
if (head == NULL)
return true;
else
return false;
}
bool equal_data(node t1, node t2)
{
if (t1.name != t2.name)
return false;
if (t1.score != t2.score)
return false;
return true;
}
void my_list::invert()
{
if(head == NULL || head == tail)
return;
node *pre, *cur, *next;
pre = NULL;
cur = head;
next = NULL;
tail = head;
while(cur != NULL) {
next = cur->link; // Save the next node
cur->link = pre; // Reverse the link
pre = cur; // Move pointers one position ahead
cur = next;
}
head = pre;
}
bool is_equal(node *p1, node *p2)
{
if ( (p1 == NULL) && (p2 == NULL) )
return true;
if ( (p1 == NULL) || (p2 == NULL) ) // AND 조건은 처리 되었으므로, OR 조건 가능
return false;
if ( equal_data(*p1, *p2) )
return(is_equal(p1->link, p2->link));
else
return false;
}
bool list_equal(my_list a, my_list b)
{
return is_equal(a.head, b.head);
}
int main( )
{
my_list a,b;
node tmp;
tmp.set_data("Kim", 83.5);
a.add_to_head(tmp);
tmp.set_data("Lee", 78.2);
a.add_to_head(tmp);
tmp.set_data("Park", 91.3);
a.add_to_head(tmp);
tmp.set_data("Choi", 85.1);
a.add_to_head(tmp);
tmp.set_data("Choi", 85.1);
b.add_to_head(tmp);
tmp.set_data("Park", 91.3);
b.add_to_head(tmp);
tmp.set_data("Lee", 78.2);
b.add_to_head(tmp);
tmp.set_data("Kim", 83.5);
b.add_to_head(tmp);
b.invert();
if(list_equal(a, b))
cout << "Yes, the two lists are identical. \n";
else
cout << "No, They are not identical.\n";
tmp = b.delete_from_head();
cout << tmp.name << " : " << tmp.score << "\n";
return 0;
}