数据结构-单链表
2016-07-10 17:03
423 查看
顺序表中插入或删除操作时,需移动大量元素。
单链表:1.非随机存取的存储结构,存/取元素必须从头指针开始;
2.每个节点包含指针域和数据域,浪费了存储空间,单插入、删除方便;
3.头指针--->头节点----->第一个节点, 头节点是为了表述方便,附设的一个节点。
静态链表: 游标(节点在数组中的相对位置)+数组
以下暂时单链表的实现:
Node.h
单链表:1.非随机存取的存储结构,存/取元素必须从头指针开始;
2.每个节点包含指针域和数据域,浪费了存储空间,单插入、删除方便;
3.头指针--->头节点----->第一个节点, 头节点是为了表述方便,附设的一个节点。
静态链表: 游标(节点在数组中的相对位置)+数组
以下暂时单链表的实现:
Node.h
#ifndef NODE_H #define NODE_H class Node{ public: int data; Node*next; void printNode(); }; #endifNode.cpp
#include<iostream> #include"Node.h" using namespace std; void Node::printNode(){ cout << data << endl; };LinkList.h
#include"Node.h" #ifndef LINKLIST_H #define LINKLIST_H class LinkList { public: LinkList(); ~LinkList(); void ClearLinkList(); bool LinkListEmpty(); int LinkListLength(); bool GetElem(int i, Node *pNode); int LocationElem(Node *pNode); bool PriorElem(Node *pCurrentNode, Node *pPreNode); bool NextElem(Node *pCurrentNode, Node *pPreNode); void ListTraverse(); bool ListInsert(int i, Node *pNode); bool ListDelete(int i, Node *pNode); bool ListInsertHead(Node *pNode); bool ListInsertTail(Node *pNode); private: Node *m_pList; int m_iLength; }; #endif //如果是模板类,必须在这里实现类函数LinkList.cpp
#include<iostream> #include"LinkList.h" using namespace std; LinkList::LinkList(){ m_pList = new Node(); m_pList->data = 0; m_pList->next = NULL; m_iLength = 0; }; LinkList::~LinkList(){ ClearLinkList(); delete m_pList; m_pList = NULL; } bool LinkList::LinkListEmpty(){ return m_iLength == 0 ? true : false; }; int LinkList::LinkListLength(){ return m_iLength; }; void LinkList::ClearLinkList(){ Node* currentNode=m_pList->next; while (currentNode != NULL){ Node *temp = currentNode->next; delete currentNode; currentNode = temp; } m_pList->next = NULL; }; bool LinkList::ListInsertHead(Node *pNode){ Node* temp = m_pList->next; Node*newNode = new Node; if (newNode == NULL) return false; newNode->data = pNode->data; m_pList->next = newNode; newNode->next = temp; m_iLength++; return true; }; bool LinkList::ListInsertTail(Node *pNode){ Node*currentNode = m_pList; while (currentNode->next!=NULL){ currentNode = currentNode->next; } Node*newNode = new Node; if (newNode == NULL) return false; newNode->data = pNode->data; newNode->next = NULL; currentNode->next = newNode; m_iLength++; return true; }; bool LinkList::ListInsert(int i, Node *pNode){ if (i<0 || i>m_iLength) return false; Node* currentNode = m_pList; for (int k = 0; k < i;k++){ currentNode = currentNode->next; } Node*newNode = new Node; if (newNode == NULL) return false; newNode->data = pNode->data; newNode->next = currentNode->next; currentNode->next = newNode; m_iLength++; return true; }; bool LinkList::ListDelete(int i, Node *pNode){ if (i<0||i>m_iLength){ return false; } Node*currentNode = m_pList; Node* currentNodeBefore = NULL; for (int k = 0; k <= i;k++){ currentNodeBefore = currentNode; currentNode = currentNode->next; } currentNodeBefore->next = currentNode->next; pNode->data = currentNode->data; delete currentNode; currentNode = NULL; m_iLength--; return true; }; bool LinkList::GetElem(int i, Node *pNode){ if (i<0 || i>=m_iLength){ return false; } Node*currentNode = m_pList; for (int k = 0; k <= i; k++){ currentNode = currentNode->next; } pNode->data = currentNode->data; return true; }; int LinkList::LocationElem(Node *pNode){ Node*currentNode = m_pList; int count = 0; while (currentNode->next!=NULL){ currentNode = currentNode->next; if (currentNode->data == pNode->data){ return count; } count++; } return -1; }; bool LinkList::PriorElem(Node *pCurrentNode, Node *pPreNode){ Node*currentNode = m_pList; Node*tempNode = NULL; while (currentNode->next != NULL){ tempNode = currentNode; currentNode = currentNode->next; if (currentNode->data == pCurrentNode->data){ if (tempNode == m_pList) return false; else { pPreNode->data = tempNode->data; } } } return false; }; bool LinkList::NextElem(Node *pCurrentNode, Node *pPreNode){ Node*currentNode = m_pList; while (currentNode->next != NULL){ currentNode = currentNode->next; if (currentNode->data == pCurrentNode->data){ if (currentNode->next == NULL) return false; pPreNode->data = currentNode->next->data; return true; } } }; void LinkList::ListTraverse(){ Node*currentNode = m_pList; while (currentNode->next != NULL){ currentNode = currentNode->next; currentNode->printNode(); } };主函数:
#include<iostream> #include<stdlib.h> #include"LinkList.h" using namespace std; /*************单链表******************/ int main(void){ Node Node1, Node2, Node3, Node4, Node5,temp; Node1.data = 3;//这个地方不是指针 Node2.data = 4;//这个地方不是指针 Node3.data = 5;//这个地方不是指针 Node4.data = 6;//这个地方不是指针 Node5.data = 7;//这个地方不是指针 LinkList*pList = new LinkList(); /* pList->ListInsertHead(&Node1); pList->ListInsertHead(&Node2); pList->ListInsertHead(&Node3); pList->ListInsertHead(&Node4); */ pList->ListInsertTail(&Node1); pList->ListInsertTail(&Node2); pList->ListInsertTail(&Node3); pList->ListInsertTail(&Node4); pList->ListInsert(1, &Node5); pList->ListDelete(1,&temp); cout << "temp="<<temp.data << endl; pList->ListTraverse(); delete pList; system("pause"); }
相关文章推荐
- 数据结构学习笔记5——双链表的实现
- hashMap
- 数据结构-顺序表
- 链表的实现与原理
- UVA - 11995 I Can Guess the Data Structure! 猜猜数据结构(STL模拟)
- 复习(数据结构):动态数组:c++_常规写法
- 线段树【区庆亮】
- 二叉树应用-二叉搜索树(数据结构基础 第6周)
- 二叉树应用-二叉搜索树类模板的实现(数据结构基础 第6周)
- 算法导论 第三部分——基本数据结构——红黑树
- 数据结构-->二叉树的链式实现 ADT
- 可视化数据结构和算法,用图示来表示
- 第4章第1节练习题6 二叉树叶子节点链接成单链表
- 数据结构学习笔记4——单链表的实现
- 复习(数据结构):动态数组:c_语言数组
- MySQL索引背后的数据结构及算法原理
- 数据结构课程设计
- 航班信息查询和检索系统-数据结构课程设计
- 树状数组----构建与基本操作
- 朋友圈(使用并查集)的实现