Skip to content

Commit 527e85e

Browse files
committed
upload
1 parent 66908af commit 527e85e

File tree

6 files changed

+543
-0
lines changed

6 files changed

+543
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Week7 结构体与链表
2+
3+
## 结构体
4+
5+
### 结构体与结构体变量
6+
7+
- 定义
8+
- 赋值
9+
10+
### 结构体变量与函数
11+
12+
结构体变量做函数参数和返回值是pass by value,与普通变量特性相同。
13+
14+
### 结构体变量与指针
15+
16+
```c++
17+
struct student
18+
{
19+
int id_num;
20+
char name[10];
21+
};
22+
23+
int main()
24+
{
25+
student mike = {};
26+
student *one = &mike;
27+
cout << (*one).id_num << endl;
28+
cout << one->id_num << endl; // 两种方式等价
29+
}
30+
```
31+
32+
`->`:指向运算符
33+
34+
### 总结
35+
36+
结构体数据类型的特性与普通数据类型的特性是一致的。
37+
38+
## 链表
39+
40+
### 链表的定义
41+
42+
链表是一种非常常用的数据结构
43+
44+
- 链表头:指向第一个链表节点的指针
45+
- 链表结点:链表中的每一个元素,包括:
46+
- 当前节点的数据
47+
- 下一个节点的地址
48+
- 链表尾:不再指向其他结点的结点,存放地址为`NULL`
49+
50+
动态申请内存空间
51+
52+
- `int *pint = new int(1024);` `delete pint`
53+
- `int *pia new int[4];` `delete [] pia;`
54+
55+
动态建立链表节点
56+
57+
```c++
58+
struct student
59+
{
60+
int id;
61+
student *next;
62+
}
63+
64+
student *create()
65+
{
66+
student *head, *temp;
67+
int num, n = 0;
68+
head = new student;
69+
temp = head;
70+
cin >> num;
71+
while (num != -1)
72+
{
73+
n++;
74+
temp->id = num;
75+
temp->next = new student;
76+
temp = temp->next;
77+
cin >> num;
78+
}
79+
if (n == 0)
80+
{
81+
head = NULL;
82+
}
83+
else
84+
{
85+
temp->next = NULL;
86+
}
87+
return head;
88+
}
89+
```
90+
91+
### 链表的操作
92+
93+
遍历
94+
95+
```c++
96+
int main()
97+
{
98+
student *pointer = create();
99+
while (pointer->next != NULL)
100+
{
101+
cout << pointer->id << endl;
102+
pointer = pointer->next;
103+
}
104+
return 0;
105+
}
106+
```
107+
108+
删除
109+
110+
- 第一个结点
111+
112+
```c++
113+
temp = head;
114+
head = head->next;
115+
delete temp;
116+
```
117+
118+
- 中间结点
119+
120+
```c++
121+
// 两个指针,follow和temp
122+
follow->next = temp->next;
123+
delete temp;
124+
```
125+
126+
```c++
127+
student *delete(student *head, int id)
128+
{
129+
student *temp, *follow;
130+
temp = head;
131+
if (head == NULL)
132+
{
133+
return head;
134+
}
135+
if (head->id == id) // 第一个节点
136+
{
137+
head = head->next;
138+
delete temp;
139+
return head;
140+
}
141+
while (temp != NULL && temp->id != id)
142+
{
143+
follow = temp;
144+
temp = temp->next;
145+
}
146+
if (temp == NULL)
147+
{
148+
cout << "Not found" << endl;
149+
}
150+
else
151+
{
152+
follow->next = temp->next; // 中间结点
153+
delete temp;
154+
}
155+
return head;
156+
}
157+
```
158+
159+
插入
160+
161+
```c++
162+
student *insert(studen *head, int id)
163+
{
164+
student *temp, *follow, *insert;
165+
temp = head;
166+
insert = new student;
167+
insert->id = id;
168+
insert->next = NULL;
169+
if (head == NULL)
170+
{
171+
head = insert;
172+
return head;
173+
}
174+
while ((temp-> next != NULL) && (temp->id < id))
175+
{
176+
follow = temp;
177+
temp = temp->next;
178+
}
179+
if (temp == head)
180+
{
181+
insert->next = head;
182+
head = insert;
183+
}
184+
else
185+
{
186+
if(temp->next == NULL)
187+
{
188+
temp->next = insert;
189+
}
190+
else
191+
{
192+
follow->next = insert;
193+
insert->next = temp;
194+
}
195+
}
196+
return head;
197+
}
198+
```
199+
200+
### 双向链表
201+
202+
两个指针`ahead` 和 `next`
203+
204+
其余操作类似单向链表。
205+
206+
## 面向对象
207+
208+
计算机程序的理解:现实世界的解决方案在计算机系统中的映射
3.64 MB
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
编程题#1:含k个3的数
3+
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
4+
5+
注意: 总时间限制: 1000ms 内存限制: 65536kB
6+
7+
描述
8+
输入二个正整数m 和 k,其中1 < m < 100000,1 < k <5 ,判断m 能否被19整除,且恰好含有k个3,如果满足条件,则输出YES,否则,输出NO。
9+
10+
例如,输入:
11+
12+
43833 3
13+
14+
满足条件,输出YES
15+
16+
如果输入
17+
18+
39331 3
19+
20+
尽管有3个3,但不能被19整除,也不满足条件,应输出NO
21+
22+
输入
23+
m 和 k 的值,空格间隔
24+
25+
输出
26+
满足条件时输出 YES,不满足时输出 NO
27+
*/
28+
29+
#include <iostream>
30+
using namespace std;
31+
32+
bool k3(int m, int k);
33+
34+
int main()
35+
{
36+
int m = 0, k = 0;
37+
cin >> m >> k;
38+
39+
if (m % 19 == 0 && k3(m, k))
40+
{
41+
cout << "YES" << endl;
42+
}
43+
else
44+
{
45+
cout << "NO" << endl;
46+
}
47+
return 0;
48+
}
49+
50+
bool k3(int m, int k)
51+
{
52+
int a[6] = { 0 };
53+
for (int i = 0; m > 0; i++)
54+
{
55+
a[i] = m % 10;
56+
m /= 10;
57+
}
58+
59+
int num = 0;
60+
for (int i = 0; i < 6; i++)
61+
{
62+
if (a[i] == 3)
63+
{
64+
num++;
65+
}
66+
}
67+
68+
if (num == k)
69+
{
70+
return true;
71+
}
72+
else
73+
{
74+
return false;
75+
}
76+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
编程题#2:字符串中次数第2多的字母
3+
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
4+
5+
注意: 总时间限制: 1000ms 内存限制: 65536kB
6+
7+
描述
8+
输入一串长度不超过500个符号的字符串,输出在串中出现第2多的英语字母(大小写字母认为相同)和次数(如果串中有其它符号,则忽略不考虑)。如果有多个字母的次数都是第2多,则按串中字母出现的顺序输出第1个。
9+
10+
例 ab&dcAab&c9defgb
11+
12+
这里,a 和 b都出现3次,c和d都出现2次,e、f 和 g 各出现1次,其中的符号&和9均忽略不考虑。因此,出现第2多的应该是 c 和 d,但是 d 开始出现的位置在 c 的前面,因此,输出为
13+
14+
D+d:2
15+
16+
(假定在字符串中,次数第2多的字母总存在)
17+
18+
输入
19+
一个字符串
20+
21+
输出
22+
大写字母+小写字母:个数
23+
*/
24+
25+
#include <iostream>
26+
#include <cctype>
27+
using namespace std;
28+
29+
struct Alphabet
30+
{
31+
char letter; // 字母,不区分大小写
32+
int fisrtShowIndex; // 第一次出现的index
33+
int count; // 字母出现的次数
34+
bool show; // 字母是否出现
35+
};
36+
37+
int main()
38+
{
39+
char str[500];
40+
cin.getline(str, 500);
41+
42+
// 字母表初始化
43+
Alphabet alphabet[26];
44+
for (int i = 0; i < 26; i++)
45+
{
46+
//alphabet[i].letter = char(97 + i);
47+
//alphabet[i].fisrtShowIndex = -1;
48+
//alphabet[i].count = 0;
49+
alphabet[i] = { char(97 + i),0,0,false };
50+
}
51+
52+
// 处理字符串
53+
for (int i = 0; str[i] != '\0'; i++)
54+
{
55+
str[i] = tolower(str[i]);
56+
if (isalpha(str[i]))
57+
{
58+
int index = str[i] - 'a';
59+
alphabet[index].count++;
60+
if (!alphabet[index].show)
61+
{
62+
alphabet[index].fisrtShowIndex = i;
63+
alphabet[index].show = true;
64+
}
65+
}
66+
}
67+
68+
// 找出第二大且最先出现的字母
69+
Alphabet max = { 0,0,0 }, second = { 0,501,0 };
70+
for (int i = 0; i < 26; i++)
71+
{
72+
if (alphabet[i].count > max.count)
73+
{
74+
max = alphabet[i];
75+
}
76+
}
77+
for (int i = 0; i < 26; i++)
78+
{
79+
if (alphabet[i].show && alphabet[i].count < max.count)
80+
{
81+
if (alphabet[i].count > second.count)
82+
{
83+
second = alphabet[i];
84+
}
85+
else if (alphabet[i].count == second.count && alphabet[i].fisrtShowIndex < second.fisrtShowIndex)
86+
{
87+
second = alphabet[i];
88+
}
89+
}
90+
}
91+
cout << (char)toupper(second.letter) << '+' << second.letter << ':' << second.count << endl;
92+
return 0;
93+
}

0 commit comments

Comments
 (0)