网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 论文素材 设计下载 最新论文 下载排行 论文上传 在线投稿 联系我们
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > C/C++ > 正文
C++实现一般线性链表类
来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/15
下载{$ArticleTitle}原创论文样式
以下的C++类LinkList实现了线性链表的一般操作。可以直接在其他的程序中直接建立它的对象,其中线性表中的数据在此为整型,具体应用的时候可以适当的修改,并可以在此基础上继续封装特定的功能。

头文件:LinkList.h

typedef struct LNode {
int data;
struct LNode *next;
}LNode, *pLinkList;

class LinkList {
private:
pLinkList m_pList;
int m_listLength;
public:
LinkList();
~LinkList();
bool InitList ();
bool DestroyList ();
bool ClearList();
bool IsEmpty ();
int GetLength ();
bool GetNode(int position, LNode** node);
int LocateElem(int elem);
bool SetNodeData(int position, int newData);
bool GetNodeData(int position, int &data);
bool InsertNode(int beforeWhich, int data);
bool DeleteNode(int position);
};

Cpp文件:LinkList.cpp

#include <iostream.h>
#include "LinkList.h"

LinkList::LinkList() {
m_pList = NULL;
m_listLength = 0;

InitList();
}

LinkList::~LinkList() {
if (!DestroyList()) {
  DestroyList();
}
}

//初始化,分配一个头节点。
bool LinkList::InitList() {
if (!(m_pList = new LNode)) {
  return false;
}
m_pList->next = NULL;

return true;
}

//销毁链表。
bool LinkList::DestroyList() {
if (!ClearList()) {
  return false;
}

delete m_pList;

return true;
}

//判断链表是否为空。若为空,返回true,否则返回false。
bool LinkList::IsEmpty() {
if (m_pList->next == NULL) {
  return true;
}
return false;
}

//返回链表的中当前节点数。
int LinkList::GetLength() {
return m_listLength;
}

//将链表清空,释放当前所有节点。
bool LinkList::ClearList() {
if (m_pList == NULL) {
  return false;
}

LNode *pTemp = NULL;
while (m_pList->next != NULL) {
  pTemp = m_pList->next;
  m_pList->next = pTemp->next;
  delete pTemp;
}
m_listLength = 0;

return true;
}

//将position指定的节点内的数据设置为newData。
//第一个有效节点的position为1。
bool LinkList::SetNodeData(int position, int newData) {
LNode *pTemp = NULL;

if (!(GetNode(position, &pTemp))) {
  return false;
}

pTemp->data = newData;

return true;
}

//得到指定位置节点的数据。
//节点索引从1到listLength。
bool LinkList::GetNodeData(int position, int &data) {
LNode *pTemp = NULL;

if (!(GetNode(position, &pTemp))) {
  return false;
}

data = pTemp->data;

return true;
}

//在链表中插入一个节点。
//插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。
//beforeWhich的取值在1到ListLength+1之间。
bool LinkList::InsertNode(int beforeWhich, int data) {
LNode *pTemp = NULL;

if (beforeWhich < 1 || beforeWhich > (m_listLength + 1)) {
  return false;
}

if (!(GetNode(beforeWhich - 1, &pTemp))) {
  return false;
}

LNode *newNode = new LNode;
newNode->data = data;
newNode->next = pTemp->next;
pTemp->next = newNode;

m_listLength++;

return true;
}

//删除一个指定的节点。
//节点位置由position指定。
//positon的值从1到listLength。
//若链表为空或指定的节点不存在则返回false。
bool LinkList::DeleteNode(int position) {
if (position < 1 || position > m_listLength) {
  return false;
}

LNode *pTemp = NULL;
if (!(GetNode(position - 1, &pTemp))) {
  return false;
}

LNode *pDel = NULL;
pD
  • 上一篇资讯: C++内码转换的三种方法
  • 下一篇资讯: C++实现字符串查找
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号