开始时程序:
ngnsvr9 [** NONE **]/home/xionghailong/c++ $ cat orig.cpp
#include<iostream>
using namespace std;
struct Node
{
Node *p_next;
int value;
};
Node* addNode (Node* p_list, int value)
{
Node *p_new_node = new Node;
p_new_node->value = value;
p_new_node->p_next = p_list;
return p_new_node;
}
void printList(const Node* p_list)
{
const Node* p_cur_node = p_list;
while (p_cur_node != NULL)
{
cout<< p_cur_node->value<<endl;
p_cur_node = p_cur_node->p_next;
}
}
int main()
{
Node *p_list = NULL;
for(int i = 0; i < 10; ++i)
{
int value;
cout<<"Enter value for list node: ";
cin >> value;
p_list = addNode(p_list, value);
}
printList(p_list);
}
当工程代码很大时,代码的可读性会大大降低;我们可以将代码分解为:linkedlist.cpp,linkedlist.h,orig.cpp
linkedlist.cpp:
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat linkedlist.cpp
#include <iostream>
#include "linkedlist.h"
using namespace std;
Node* addNode (Node* p_list, int value)
{
Node *p_new_node = new Node;
p_new_node->value = value;
p_new_node->p_next = p_list;
return p_new_node;
}
void printList(const Node* p_list)
{
const Node* p_cur_node = p_list;
while (p_cur_node != NULL)
{
cout<< p_cur_node->value<<endl;
p_cur_node = p_cur_node->p_next;
}
}
linkedlist.h:
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat linkedlist.h
struct Node
{
Node *p_next;
int value;
};
Node* addNode (Node* p_list, int value);
void printList(const Node* p_list);
orig.cpp:
void printList(const Node* p_list);ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat orig.cpp
#include <iostream>
#include "linkedlist.h"
using namespace std;
int main()
{
Node *p_list = NULL;
for(int i = 0; i < 10; ++i)
{
int value;
cout<<"Enter value for list node: ";
cin >> value;
p_list = addNode(p_list, value);
}
printList(p_list);
}
可以直接用如下命令进行执行:
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ g++ orig.cpp linkedlist.cpp
In file included from orig.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
orig.cpp:17:2: warning: no newline at end of file
In file included from linkedlist.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out linkedlist.cpp linkedlist.cpp.bak linkedlist.h linkedlist.h.bak makefile.txt makefile.txt.bak orig.cpp orig.cpp.bak
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ./a.out
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 23
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
3
3
3
3
3
23
2
2
2
2
采用Makefile文件可以更加直接明了:
Makefile文件如下:
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat makefile.txt
all: orig
orig: orig.o linkedlist.o
g++ orig.o linkedlist.o -o orig
orig.o: orig.cpp
g++ -c orig.cpp
linkedlist.o: linkedlist.cpp
g++ -c linkedlist.cpp
clean:
rm -rf *o orig
执行:
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ make -f makefile.txt
g++ -c orig.cpp
In file included from orig.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
orig.cpp:17:2: warning: no newline at end of file
g++ -c linkedlist.cpp
In file included from linkedlist.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
g++ orig.o linkedlist.o -o orig
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out linkedlist.cpp linkedlist.cpp.bak linkedlist.h linkedlist.h.bak linkedlist.o makefile.txt makefile.txt.bak orig orig.cpp orig.cpp.bak orig.o
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ./orig
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 1
Enter value for list node: 2
Enter value for list node: 3
Enter value for list node: 1
Enter value for list node: 23
Enter value for list node: 4
Enter value for list node: 5
Enter value for list node: 56
56
5
4
23
1
3
2
1
2
2
生成了可执行文件orig
执行clean删除.o 和可执行文件
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ make -f makefile.txt clean
rm -rf *o orig
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out linkedlist.cpp linkedlist.cpp.bak linkedlist.h linkedlist.h.bak makefile.txt makefile.txt.bak orig.cpp orig.cpp.bak
希望对大家能有所帮助。