Skip to content

Commit 7a2d001

Browse files
committed
常用数据结构,如Link,Stack,Queue
1 parent cdf2b5b commit 7a2d001

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4163
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
*.o
2+
*.jar
3+
*.out
24
*.stackdump
35
*.obj
46
*.swp
57
*.exe
8+
*.out
9+
*.dat
10+
611

712
data/
13+
tmp/
14+
815

datastruct/BTree.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
@20140223 18:31~
3+
4+
1. 二叉树的遍历;
5+
*/
6+
7+

datastruct/Link.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
@20140220
3+
4+
1. 链表有个头指针;
5+
2. 链表插入,删除操作;
6+
3. 链接反转;
7+
8+
api:get(x);remove(x);add(x);print();del(x);reverse();
9+
*/
10+
11+
#include <iostream>
12+
using namespace std;
13+
14+
class Node {
15+
public:
16+
Node() {
17+
next = NULL;
18+
}
19+
int value;
20+
Node* next;
21+
};
22+
23+
class Link {
24+
public:
25+
Link() {
26+
// error1: 初始化
27+
m_head = new Node();
28+
m_size = 0;
29+
}
30+
31+
bool empty() {
32+
return m_head->next == NULL;
33+
}
34+
35+
int get(int x);
36+
37+
void add(int x);
38+
39+
void print();
40+
41+
// 删除位于x的元素
42+
bool remove(int x);
43+
44+
// 删除第一个值为x的元素
45+
int del(int x);
46+
47+
void reverse();
48+
49+
int size() {
50+
return m_size;
51+
}
52+
53+
private:
54+
int m_size;
55+
Node* m_head;
56+
};
57+
58+
int Link::get(int x) {
59+
if (x >= 0 && x < m_size) {
60+
Node* p = m_head->next;
61+
while (x > 0 && p->next != NULL) {
62+
p = p->next;
63+
x --;
64+
}
65+
if (x == 0) {
66+
return p->value;
67+
}
68+
} else {
69+
throw std::overflow_error("x is exceed index!");
70+
}
71+
// implicit conversion of NULL constant to 'int'
72+
return NULL;
73+
}
74+
75+
bool Link::remove(int x) {
76+
if (x >= 0 && x < m_size) {
77+
Node* p = m_head->next;
78+
Node* pre = m_head;
79+
while (x > 0) {
80+
pre = p;
81+
p = p->next;
82+
x--;
83+
}
84+
if (x == 0 && p != NULL) {
85+
pre->next = p->next;
86+
} else {
87+
pre->next = NULL;
88+
}
89+
delete p;
90+
}
91+
m_size--;
92+
return false;
93+
}
94+
95+
96+
void Link::reverse() {
97+
Node* p = m_head->next;
98+
Node* pre = NULL;
99+
while (p != NULL) {
100+
Node* pNext = p->next;
101+
if (pNext == NULL) {
102+
//error: m_head = pNext;
103+
m_head->next = p;
104+
}
105+
p->next = pre;
106+
pre = p;
107+
p = pNext;
108+
}
109+
}
110+
111+
void Link::print() {
112+
// error2: 头指针不清晰
113+
Node* p = m_head->next;
114+
while (p != NULL) {
115+
cout << p->value << "->";
116+
p = p->next;
117+
}
118+
cout << endl;
119+
}
120+
121+
122+
void Link::add(int x) {
123+
Node* p = m_head;
124+
while (p->next != NULL) {
125+
p = p->next;
126+
}
127+
Node* q = new Node();
128+
q->value = x;
129+
p->next = q;
130+
m_size++;
131+
}
132+
133+
int Link::del(int x) {
134+
Node* p = m_head->next;
135+
// error3: || -> &&
136+
while (p->next != NULL && p->next->value != x) {
137+
p = p->next;
138+
}
139+
if (p->next == NULL) {
140+
return -1;
141+
} else {
142+
Node* q = p->next;
143+
p->next = q->next;
144+
delete q;
145+
}
146+
m_size--;
147+
return 0;
148+
}

datastruct/heap.c

Whitespace-only changes.

datastruct/link1.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// msbd 13.1
2+
// create/length/print single link
3+
// 2014-01-31
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class Link {
10+
public:
11+
Link() {
12+
m_head = new Node();
13+
m_length = 0;
14+
}
15+
16+
int size();
17+
void push_back(const char v);
18+
void print_link();
19+
void remove(int i);
20+
void insert(int i, const char v);
21+
22+
private:
23+
struct Node {
24+
char value;
25+
Node* next;
26+
Node():next(NULL){}
27+
Node(char v) {
28+
value = v;
29+
}
30+
};
31+
private:
32+
Node* m_head;
33+
int m_length;
34+
};
35+
36+
int Link::size()
37+
{
38+
return m_length;
39+
}
40+
41+
void Link::remove(int i)
42+
{
43+
if (i < 0 || i >= m_length) {
44+
cout << "nothing to remove.." << endl;
45+
return;
46+
}
47+
if (i == 0) {
48+
Node* p = m_head->next;
49+
m_head->next = NULL;
50+
delete p;
51+
} else {
52+
Node* p = m_head->next;
53+
while (--i) {
54+
p = p->next;
55+
}
56+
Node* q = p->next;
57+
p->next = p->next->next;
58+
delete q;
59+
}
60+
m_length --;
61+
cout << "remove success" << endl;
62+
}
63+
64+
void Link::push_back(const char v)
65+
{
66+
Node* p = m_head;
67+
Node* p2;
68+
while(p != NULL) {
69+
p2 = p;
70+
p = p->next;
71+
}
72+
Node* add = new Node(v);
73+
p2->next = add;
74+
m_length++;
75+
}
76+
77+
void Link::print_link()
78+
{
79+
Node* p = m_head;
80+
while(p != NULL) {
81+
cout << p->value << "->";
82+
p = p->next;
83+
}
84+
cout << endl;
85+
}
86+
87+
int main()
88+
{
89+
Link* link = new Link();
90+
link->push_back('a');
91+
link->push_back('b');
92+
link->push_back('c');
93+
link->push_back('d');
94+
link->print_link();
95+
link->remove(2);
96+
link->print_link();
97+
cout << "len: " << link->size() << endl;
98+
}
99+
100+

datastruct/link2.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
@20140220
3+
4+
1. 链表有个头指针;
5+
2. 链表插入,删除操作;
6+
3. 链接反转;
7+
*/
8+
9+
#include <iostream>
10+
using namespace std;
11+
12+
class Node {
13+
public:
14+
Node() {
15+
next = NULL;
16+
}
17+
char value;
18+
Node* next;
19+
};
20+
21+
class Link {
22+
public:
23+
Link() {
24+
// error1: 初始化
25+
m_head = new Node();
26+
}
27+
28+
void add(char x) {
29+
Node* p = m_head;
30+
while (p->next != NULL) {
31+
p = p->next;
32+
}
33+
Node* q = new Node();
34+
q->value = x;
35+
p->next = q;
36+
}
37+
38+
void print() {
39+
// error2: 头指针不清晰
40+
Node* p = m_head->next;
41+
while (p != NULL) {
42+
cout << p->value << "->";
43+
p = p->next;
44+
}
45+
cout << endl;
46+
}
47+
48+
int del(char x) {
49+
Node* p = m_head->next;
50+
// error3: || -> &&
51+
while (p->next != NULL && p->next->value != x) {
52+
p = p->next;
53+
}
54+
if (p->next == NULL) {
55+
return -1;
56+
} else {
57+
Node* q = p->next;
58+
p->next = q->next;
59+
delete q;
60+
}
61+
return 0;
62+
}
63+
64+
void reverse() {
65+
Node* p = m_head->next;
66+
Node* pre = NULL;
67+
while (p != NULL) {
68+
Node* pNext = p->next;
69+
if (pNext == NULL) {
70+
//error: m_head = pNext;
71+
m_head->next = p;
72+
}
73+
p->next = pre;
74+
pre = p;
75+
p = pNext;
76+
}
77+
}
78+
79+
private:
80+
Node* m_head;
81+
};
82+
83+
int main()
84+
{
85+
Link link;
86+
link.add('a');
87+
link.add('b');
88+
link.add('c');
89+
link.add('d');
90+
link.print();
91+
link.del('c');
92+
link.print();
93+
link.reverse();
94+
link.print();
95+
}

0 commit comments

Comments
 (0)