Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 14ced33

Browse files
committedNov 30, 2017
lab10
1 parent 299c6e7 commit 14ced33

8 files changed

+241
-3
lines changed
 

‎.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ script:
1818
- ( for file in $(find ./lab5 -name "*.cpp"); do ./cpplint.py $file || exit; done );
1919
- ( for file in $(find ./lab6 -name "*.cpp"); do ./cpplint.py $file || exit; done );
2020
- ( for file in $(find ./lab7 -name "*.cpp"); do ./cpplint.py $file || exit; done );
21+
- ( for file in $(find ./lab8 -name "*.cpp"); do ./cpplint.py $file || exit; done );
22+
- ( for file in $(find ./lab9 -name "*.cpp"); do ./cpplint.py $file || exit; done );
23+
- ( for file in $(find ./lab10 -name "*.cpp"); do ./cpplint.py $file || exit; done );
2124
- cd lab1
2225
- mkdir build && cd build
2326
- cmake .. && make
@@ -45,4 +48,19 @@ script:
4548
- cd lab7
4649
- mkdir build && cd build
4750
- cmake .. && make
51+
52+
- cd ../..
53+
- cd lab8
54+
- mkdir build && cd build
55+
- cmake .. && make
56+
57+
- cd ../..
58+
- cd lab9
59+
- mkdir build && cd build
60+
- cmake .. && make
61+
62+
- cd ../..
63+
- cd lab10
64+
- mkdir build && cd build
65+
- cmake .. && make
4866
# - ./executable

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
### [Лабораторная работа 8](lab8/lab8_1.cpp)
4242
* Алгоритмы STL
4343

44-
### Лабораторная работа 9
44+
### [Лабораторная работа 9](lab9/lab9_1.cpp)
4545
* Указатели
4646

47-
### Лабораторная работа 10
48-
* Реализация двусвязного списка
47+
### [Лабораторная работа 10](lab10)
48+
* Реализация двусвязного списка

‎lab10/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required (VERSION 2.8)
2+
3+
include_directories(SYSTEM
4+
./
5+
)
6+
7+
ADD_DEFINITIONS(
8+
-std=c++11
9+
)
10+
11+
# enum your files
12+
add_executable(executable
13+
lab10.cpp
14+
list.cpp
15+
)
16+
17+
target_link_libraries(executable)

‎lab10/lab10.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "list.hpp"
2+
3+
int main()
4+
{
5+
List* l = initList();
6+
7+
pushBack(l, 1);
8+
erase(l, l->Head);
9+
10+
pushBack(l, 2);
11+
pushBack(l, 3);
12+
pushFront(l, -1);
13+
pushFront(l, -2);
14+
pushFront(l, -3);
15+
print(l);
16+
erase(l, l->Head->Next->Next);
17+
print(l);
18+
erase(l, l->Tail);
19+
print(l);
20+
21+
insert(l, l->Head->Next, 100);
22+
print(l);
23+
24+
destroyList(&l);
25+
26+
return 0;
27+
}

‎lab10/list.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "list.hpp"
2+
3+
List::Node* createNode(const List::value_type& data, List::Node* prev, List::Node* next)
4+
{
5+
List::Node* node = new List::Node();
6+
node->Data = data;
7+
node->Prev = prev;
8+
node->Next = next;
9+
10+
return node;
11+
}
12+
13+
void destroyNode(List::Node** ptr)
14+
{
15+
List::Node* node = *ptr;
16+
delete node;
17+
*ptr = nullptr;
18+
}
19+
20+
List* initList()
21+
{
22+
List* ls = new List();
23+
ls->Head = nullptr;
24+
ls->Tail = nullptr;
25+
return ls;
26+
}
27+
28+
void destroyList(List** ls)
29+
{
30+
List* list = *ls;
31+
32+
List::Node* head = list->Head;
33+
while (head)
34+
{
35+
List::Node* tmp = head->Next;
36+
delete head;
37+
head = tmp;
38+
}
39+
delete list;
40+
41+
*ls = nullptr;
42+
}
43+
44+
List::Node* pushFront(List* const list, const List::value_type & data)
45+
{
46+
List::Node* node = createNode(data, nullptr, list->Head);
47+
48+
if (list->Head)
49+
list->Head->Prev = node;
50+
list->Head = node;
51+
52+
if (list->Tail == nullptr)
53+
list->Tail = node;
54+
return node;
55+
}
56+
57+
List::Node* pushBack(List* const list, const List::value_type & data)
58+
{
59+
List::Node* node = createNode(data, list->Tail, nullptr);
60+
61+
if (list->Tail)
62+
list->Tail->Next = node;
63+
list->Tail = node;
64+
65+
if (list->Head == nullptr)
66+
list->Head = node;
67+
return node;
68+
}
69+
70+
List::Node* insert(List* const list, List::Node* const where, const List::value_type& data)
71+
{
72+
List::Node* newNode = createNode(data, where->Prev, where);
73+
List::Node* prev = where->Prev;
74+
if (prev)
75+
prev->Next = newNode;
76+
where->Prev = newNode;
77+
return newNode;
78+
}
79+
80+
List::Node* erase(List* const list, List::Node* const where)
81+
{
82+
List::Node* next = where->Next;
83+
List::Node* prev = where->Prev;
84+
85+
if (list->Head == where)
86+
list->Head = next;
87+
if (list->Tail == where)
88+
list->Tail = prev;
89+
90+
if (prev)
91+
prev->Next = next;
92+
93+
delete where;
94+
95+
return next;
96+
}
97+
98+
size_t size(const List* const list)
99+
{
100+
size_t size = 0;
101+
List::Node* head = list->Head;
102+
while (head)
103+
{
104+
++size;
105+
head = head->Next;
106+
}
107+
return size;
108+
}
109+
110+
void print(const List* const ls)
111+
{
112+
List::Node* tmp = ls->Head;
113+
while (tmp)
114+
{
115+
std::cout << tmp->Data << std::endl;
116+
tmp = tmp->Next;
117+
}
118+
}

‎lab10/list.hpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
struct List
4+
{
5+
using value_type = int;
6+
7+
struct Node
8+
{
9+
value_type Data;
10+
Node* Next;
11+
Node* Prev;
12+
};
13+
14+
Node* Head;
15+
Node* Tail;
16+
};
17+
18+
List::Node* createNode(const List::value_type& data, List::Node* prev, List::Node* next);
19+
20+
void destroyNode(List::Node** ptr);
21+
22+
List* initList();
23+
24+
void destroyList(List** ls);
25+
26+
List::Node* pushFront(List* const list, const List::value_type & data);
27+
28+
List::Node* pushBack(List* const list, const List::value_type & data);
29+
30+
List::Node* insert(List* const list, List::Node* const where, const List::value_type& data);
31+
32+
List::Node* erase(List* const list, List::Node* const where);
33+
34+
size_t size(const List* const list);
35+
36+
void print(const List* const ls);

‎lab9/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required (VERSION 2.8)
2+
3+
include_directories(SYSTEM
4+
./
5+
)
6+
7+
ADD_DEFINITIONS(
8+
-std=c++11
9+
)
10+
11+
# enum your files
12+
add_executable(executable
13+
lab9_1.cpp
14+
)
15+
16+
target_link_libraries(executable)

‎lab9/lab9_1.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <iostream>
2+
3+
int main()
4+
{
5+
return 0;
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.