- 论坛徽章:
- 0
|
10可用积分
考虑了几个因素: 内存管理,错误处理,记录head的位置等等。在win和solaris上面用gcc均测试通过。
想请各位看官点评一下,看看有没有设计上的缺陷(内存? 排错?)或者性能上的风险。这个类以后会作为一个生产程序的基础,所以我需要很仔细地来考量它。
10分待送,给指出本程序弱点的dx
cat>main.cpp
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define STRMAX 20
class node{
node(){
data=new char[STRMAX+1];
strncpy(data,"head",5);
pnext=NULL;
}
node(const char* content){
data=new char[STRMAX+1];
strncpy(data,content,STRMAX);
pnext=NULL;
}
~node(){
if(data)delete data;
pnext=NULL;
}
public:
char* data;
node* pnext;
//这样做是为了保证node都是在堆上生成的。因为如果在stack上有node, 而主程序去delete这个node
//的地址,程序就会崩溃。所以内存管理就包装在node类的内部,外部用函数去掉用它
static node* createNode(){
printf("createNode() called\n");
return new node();
}
static node* createNode(const char* content){
if(content==NULL){
printf("createNode() received null string for input param\n");
return new node();
}
if(strlen(content)>=STRMAX){
printf("createNode() input string is longer than %d, will be truncated\n",STRMAX);
}
printf("createNode(%s) called\n",content);
return new node(content);
}
static void destroyNode(node* pn){
delete pn;
}
void printNode(){
printf("node data=%s\n",data);
}
};
class list{
node* head;
node* tail;
public:
list(){
head=node::createNode();
tail=head;
printf("list::ctor() finish\n");
}
~list(){
node* cur=head;
node* next;
do{
next=cur->pnext;
node::destroyNode(cur);
cur=next;
}while(cur);
}
void print(){
node* cur=head;
do{
printf("node pointer=%d,next=%d\n",cur,cur->pnext);
cur->printNode();
cur=cur->pnext;
}while(cur);
}
void printTail(){
printf("list tail information:");
tail->printNode();
}
bool append(node* pn){
if(pn==NULL){
printf("append() fail with null input pointer param\n");
return false;
}
printf("list::append() called\n");
tail->pnext=pn;
tail=pn;
return true;
}
bool insert(int insertAfter/*在位置insertAfter之后插入一个节点 从0开始算*/,node* pn){
//e.g, if list has mutiple elements and insertAfter is 0
//then insert it after the head node
if(insertAfter<0){
printf("insert() received negative param:%d\n",insertAfter);
return false;
}
if(pn==NULL){
printf("insert fail with null input pointer param\n");
return false;
}
node* next=head;
while(insertAfter){
next=next->pnext;
if(next==NULL){
printf("traverse next pointer %d insertAfter is NULL, insert() failed!!!!!!!\n");
return false;
}
--insertAfter;
}
if(next==tail)
append(pn);
else{
printf("list::insert() called\n");
pn->pnext=next->pnext;
next->pnext=pn;
}
return true;
}
bool remove(int at){//删除链表某个位置上的元素
//Never remove head node
if(at<=0){
printf("insert() received <=0 param:%d\n",at);
return false;
}
node* next=head;
node* old;
while(at){
old=next;
next=next->pnext;
if(next==NULL){
printf("next %d insertAfter is NULL, insert() failed\n");
return false;
}
--at;
}
//printf("old->data is %s,pnext->data is%s\n",old->data,old->pnext->data);
//printf("next->pnext data is %s\n",next->pnext->data);
old->pnext=next->pnext;
//printf("now,old->data is %s,pnext->data is%s\n",old->data,old->pnext->data);
if(tail==next){
printf("remove tail, reset tail\n");
tail=old;
}
node::destroyNode(next);
return true;
}
void reverse(){
printf("====================开始反转======================\n");
if(head==tail)return;
node* cur=head->pnext;
node* rev=cur->pnext;
node* next;
cur->pnext=NULL;
while(rev){
next=rev->pnext;
rev->pnext=cur;
cur=rev;
rev=next;
}
head->pnext=cur;
}
};
int main(int argc, char *argv[])
{
list l;
l.append(node::createNode("1st node"));
l.append(node::createNode("2nd node"));
l.append(node::createNode("4th node"));
l.insert(2,node::createNode("3rd node"));
l.insert(4,node::createNode("5th node"));
l.insert(5,node::createNode("6 okorfail???"));
l.print();
printf("========================================================\n");
l.remove(3);
l.remove(5);
l.print();
l.reverse();
l.print();
return EXIT_SUCCESS;
}
[ 本帖最后由 jeanlove 于 2009-1-23 16:08 编辑 ] |
|