您的位置:首页 > 理论基础 > 数据结构算法

数据结构-单链表

2016-07-10 17:03 423 查看
  顺序表中插入或删除操作时,需移动大量元素。

 单链表:1.非随机存取的存储结构,存/取元素必须从头指针开始;

                  2.每个节点包含指针域和数据域,浪费了存储空间,单插入、删除方便;

                 3.头指针--->头节点----->第一个节点, 头节点是为了表述方便,附设的一个节点。

静态链表: 游标(节点在数组中的相对位置)+数组

以下暂时单链表的实现:

Node.h

#ifndef NODE_H
#define NODE_H
class Node{
public:
int data;
Node*next;
void printNode();
};

#endif
Node.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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: