C++写的一个链表程序

作为刚入公司的小菜鸟,公司居然让我学习C++,以前学的php或许要荒废了呀,刚上来,就让我写个链表程序,查了网上的资料,自己模仿写了一个,如下:
list.h:

#pragma once
#ifndef LIST_H
#define LIST_H
#include "node.h"

class list
{
public:
    list(void);//构造函数
    bool setList(char* data,int i);//该某个节点处的数值类型
    bool insertList(node* pn,int i);//在第i个节点后面增加某个某个节点数据
    bool appendList(node* pn);//在链表末尾追加数据
    bool unshiftList(node* pn);//在链表头部追加数据
    bool deleteList(int i);//删除某个节点
    char* getList(int i);//获得第i个节点的数据
private:
    node* head;//定义头

};
#endif

list.cpp

#include <iostream>
#include "list.h"
#include "node.h"
using namespace std;

//初始化head数据
list::list():head(node::createNode())
{

}

//修改某个节点处的数值类型
bool list::setList(char* data,int i){
if(strlen(data)>STRMAX){
printf(“输入的字符串长度大于 %d,将会被截短”,STRMAX);
}
if(i<0){
printf(“非法参数”);
}
node* next = head;
while(i){
next = next->pNext;
if(next==NULL){
printf(“要搜索的节点不存在”);
}
–i;
}
//为传过来的data复制一份内存,防止出现野指针
char* data_copy =(char*)malloc(STRMAX);
memset(data_copy,’\0’,STRMAX);
strcat(data_copy,data);
next->data=data_copy;//找到位置后进行赋值
return true;

}

//在链表末尾追加数据
bool list::appendList(node* pn){
if(pn==NULL){
printf(“传入节点不存在”);
return false;
}
node* next = head;
while(next->pNext){
next=next->pNext;
}//寻找尾节点
next->pNext=pn;
return true;

}
//在链表头部追加数据
bool list::unshiftList(node* pn){
if(pn==NULL){
printf(“传入节点不存在”);
return false;
}
pn->pNext=head;
head=pn;
return true;

}

//在第i个节点后面增加某个某个节点数据
bool list::insertList(node* pn,int i){
if(i<0){
printf(“非法参数”);
return false;
}
if(i==0){
unshiftList(pn);
}
node* next = head;
while(i){
next=next->pNext;
if(next==NULL){
printf(“插入点大于链表的长度”);
return false;
}
–i;
}
if(next->pNext==NULL){
appendList(pn);
return true;
}
pn->pNext=next->pNext;//将i位置的next指针传给新插入的node数据
next->pNext=pn;//将i位置的next指针指向新值;
return true;
}

//删除某个节点
bool list::deleteList(int i){
if(i<=0){
printf(“非法参数”);
return false;
}
if(i=0){
node* old = head;
head = head->pNext;
delete old;//删除头节点
return true;

}
node* before = head;
while(i-1){
    before=before->pNext;
    if(before->pNext==NULL){
        printf("要删除的节点不存在");
        return false;
    }

    --i;
}
node* cur=before->pNext;
before->pNext=cur->pNext;//把当前的指针赋给before
delete cur;//删除当前node
return true;

}

//获得第i个节点的数据
char* list::getList(int i){
if(i<0){
printf(“非法参数”);

}
node* next = head;
while(i){
    next = next->pNext;
    if(next==NULL){
        printf("要搜索的节点不存在");
    }
    --i;
}
return next->data;

}
“`

`

node.h

pragma once

include

const int STRMAX=20;

class node
{
public:
node();
node(const char* content);
~node();
static node* createNode();
static node* createNode(const char* content);

public:
char* data;
node* pNext;
};
`

node.cpp

include “node.h”

define STRMAX 20`

node::node()
//:data(0),pNext(NULL)
{
data = new char[STRMAX+1];
strncpy (data,”head”,5);
pNext = NULL;
}
node::node(const char* content){
data = new char[STRMAX+1];
strncpy(data,content,STRMAX);
pNext = NULL;
}
node* node::createNode(){
return new node();
}
node* node::createNode(const char* content){
if(content==NULL){
printf(“内容为空”);
return new node();
}
if(strlen(content)>=STRMAX){
printf(“createNode() input string is longer than %d, will be truncated”,STRMAX);
}
return new node(content);
}
/*

node* node::getNodepNext(int i){
if(i<0){
printf(“非法参数”);

}
node* next = head;
while(i){
    next = next->pNext;
    if(next==NULL){
        printf("要搜索的节点不存在");
    }
    --i;
}
return next->pText;

}
*/

node::~node()
{
if(data){
free data;//释放内存
pNext = NULL;
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值