list.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
void runlistdemo();
class List
{
public:
string name;
class List*next;
bool hasNext() {
return this->next != NULL;
}
};
#endif // LIST_H_INCLUDED
list.cpp
#include "list.h"
//#include <sstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>
#include "tools.h"
List* createList( int count);
void printListNode(List* firstNode);
List * insertListNode(List* firtsNode,List *node, int position);
List * deleteListNode(List* firtsNode,int position);
void removeList(List* firtsNode);
List* createNode(string name);
List* findTheLastNode(List * firstNode);
void changeNodeName(List*firstNode, int position, string changeName);
void reverseList(List* firstNode);
string numberTostring(int num){
stringstream ss;
ss << num;
string result;
ss >> result;
return result;
}
void runlistdemo(){
cout<<"创建一个列表"<<endl;
List*firstNode = createList(10);
printListNode(firstNode);
cout<<"增加一个节点"<<endl;
List *node0 = createNode("@0");
List *node5 = createNode("@5");
List *node100 = createNode("@100");
firstNode = insertListNode(firstNode, node0, 0);
printListNode(firstNode);
firstNode = insertListNode(firstNode, node5, 5);
printListNode(firstNode);
firstNode = insertListNode(firstNode, node100, 100);
printListNode(firstNode);
cout<<"删除一个节点"<<endl;
firstNode = deleteListNode(firstNode,0);
printListNode(firstNode);
firstNode = deleteListNode(firstNode,5);
printListNode(firstNode);
firstNode = deleteListNode(firstNode,1000);
printListNode(firstNode);
cout<<"修改一个节点"<<endl;
cout<<"清空这个链表"<<endl;
removeList(firstNode);
printListNode(firstNode);
}
List* insertListNode(List* firstNode, List *node, int position){
// position 在范围内-插入, 超过范围-末尾加入
List* tmpNode = firstNode;
int tmpPosition = 0; //从0开始
if(position == tmpPosition) { //作为最新的列表
node->next=firstNode;
return node;
}
while(true){
if(!tmpNode->hasNext()){
//放在末尾
List * lastNode = findTheLastNode(firstNode);
lastNode->next = node;
break;
}
if(position < tmpPosition-1) {
//找到了位置
node->next = tmpNode->next;
tmpNode->next = node;
break;
} else {
tmpNode = tmpNode->next;
tmpPosition++;
}
}
return firstNode; //最新的列表
}
List* deleteListNode(List* firtsNode,int position){
List* tmpNode = firtsNode;
int tmpPoistion = 0;
if(position == tmpPoistion) { //删除第一个
firtsNode = tmpNode->next;
delete tmpNode;
return firtsNode;
}
while(true){
if(!tmpNode->hasNext()) {
break; //不做任何操作
}
if(tmpPoistion == position-1) {
//删掉这个节点
List* deleteNode = tmpNode->next;
tmpNode->next = deleteNode->next;
delete deleteNode;
break;
} else {
tmpNode = tmpNode->next;
tmpPoistion++;
}
}
return firtsNode;
}
void removeList(List* firtsNode){
List * tmpNode = firtsNode;
while(true){
if(tmpNode->hasNext()) {
List * deleteNode = tmpNode;
tmpNode = tmpNode->next;
delete deleteNode;
} else {
break;
}
}
}
// 修改名字
void changeNodeName(List*firstNode, int position, string changeName){
List*tmpNode = firstNode;
int tmpPosition = 0;
while(true){
if(!tmpNode->hasNext()) {
break;
}
}
}
List* createNode(string name){
List* node = new List;
node->name = name;
node->next = NULL;
return node;
}
List* findTheLastNode(List * firstNode){
List * tmpNode = firstNode;
do {
if(!tmpNode->hasNext()) {
return tmpNode;
} else {
tmpNode = tmpNode->next;
}
} while(true) ;
}
List* createList( int count) {
List * firstnode = createNode("first");
for(int i=0; i<count; i++){
List* next = createNode(numberTostring(i));
List* lastNode = findTheLastNode(firstnode);
lastNode->next = next;
}
return firstnode;
}
void printListNode(List* firstNode){
List* tmpNode = firstNode;
int tmpPosition = 0;
cout<<"[[[";
do {
cout<<tmpPosition++<<"::"<<tmpNode->name<<"-->";
if(tmpNode->hasNext()) {
tmpNode = tmpNode->next;
} else {
cout<<"]]]"<<endl;
return;
}
} while(true);
}
void reverseList(List* firstNode){
}
// 循环链表