Skip to content

Commit 20f45b2

Browse files
committed
mass commit for the cpp impl.of homework
1 parent d084fb1 commit 20f45b2

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

middle_node.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
class Node{
7+
public:
8+
Node(){
9+
next = NULL;
10+
}
11+
//void setValue(int value){
12+
// data = value;
13+
//}
14+
//string getValue(){
15+
// return data;
16+
//}
17+
string data;
18+
Node* next;
19+
};
20+
21+
class LinkedList{
22+
public:
23+
LinkedList(){
24+
head = NULL;
25+
}
26+
void printAll();
27+
void inserToFront(string);
28+
void insertToBack(string);
29+
Node* getHead(){
30+
return head;
31+
}
32+
private:
33+
Node* head;
34+
};
35+
36+
void LinkedList::printAll(){
37+
Node* p;
38+
p = head;
39+
while(p != NULL){
40+
cout << p->data << endl;
41+
p = p->next;
42+
}
43+
}
44+
void LinkedList::insertToBack(string value){
45+
Node* node = new Node;
46+
node->data = value;
47+
if(head == NULL){
48+
node->next = NULL;
49+
}
50+
else{
51+
node->next = head;
52+
}
53+
head = node;
54+
}
55+
void LinkedList::inserToFront(string value){
56+
Node* node = new Node;
57+
if(head == NULL){
58+
head = node;
59+
}
60+
else{
61+
Node* iter = head;
62+
while(iter->next != NULL){
63+
iter = iter->next;
64+
}
65+
iter->next = node;
66+
}
67+
node->data = value;
68+
node->next = NULL;
69+
}
70+
71+
void middle_node(Node* node){
72+
node->data = node->next->data;
73+
node->next = node->next->next;
74+
}
75+
76+
int main(){
77+
78+
LinkedList list;
79+
list.insertToBack("a");
80+
list.inserToFront("b");
81+
list.inserToFront("c");
82+
list.inserToFront("d");
83+
list.inserToFront("e");
84+
list.inserToFront("f");
85+
list.printAll();
86+
cout<< endl << endl;
87+
Node* node = list.getHead()->next;
88+
middle_node(node);
89+
list.printAll();
90+
91+
return 0;
92+
}

0 commit comments

Comments
 (0)