简单实现
链表实现了使用离散的空间来存储数据,节点包括数据域和指针域,可以使用单指针构成单向链表也可以使用双指针构成双向链表。下面使用单指针来实现简单的链表。
#include<iostream>
#include"my_list.h"
using namespace std;
class List;
//template<class T>
class Node {
friend class List;//方法1:使List实例的对象可以外部访问Node实例对象的私有数据
private:
Node* front;
int data;
};
class List {
public:
void test();
void show();
private:
Node* first;//指向第一个节点的指针
};
void List::show() {
std::cout << first->data << endl
<< first->front->data << endl
<< first->front->front->data << endl;
}
void List::test() {
Node* f = new Node();
f->data = 20;
f->front = 0;
first = f;
//std::cout<< f->data;
f = new Node();
f->data = 21;
f->front = 0;
first->front = f;
f = new Node();
f->data = 22;
f->front = 0;
first->front->front = f;
}
int main() {
List test_list;
test_list.test();
test_list.show();
MyList myl;
myl.test();
myl.show();
system("pause");
return 0;
}
my_list.h
#pragma once
#ifndef _MY_LIST_H
#define _MY_LIST_H
#include<iostream>
using namespace std;
class MyList { //方法2:嵌套类
private:
class MyNode {
public:
char data[3];
MyNode* front;
};
MyNode* first;
public:
void test();
void show();
};
void MyList::show() {
std::cout << first->data << endl
<< first->front->data << endl
<< first->front->front->data << endl;
}
void MyList::test() {
MyNode* f = new MyNode();
f->data[0] = 'B';
f->data[1] = 'A';
f->data[2] = 'T';
f->front = 0;
first = f;
//std::cout<< f->data;
f = new MyNode();
f->data[0] = 'C';
f->data[1] = 'A';
f->data[2] = 'T';
f->front = 0;
first->front = f;
f = new MyNode();
f->data[0] = 'D';
f->data[1] = 'A';
f->data[2] = 'T';
f->front = 0;
first->front->front = f;
}
#endif
链表实现
实现链表的节点插入、删除、翻转、显示、连接链表等操作。
list.h
#ifndef _LIST_H
#define _LIST_H
#include<iostream>
using namespace std;
template<class Type> class List;
template<class Type>
class ListNode {
friend class List<Type>;
private:
Type data;
ListNode* link;
ListNode(Type);//私有构造
};
template<class Type>
ListNode<Type>::ListNode(Type item) {
data = item;
link = 0;
}
template<class Type>
class List {
public:
List() { first = 0; }
void insert(Type);
void show();
void Delete(Type item);
void invert();
void concatenate(List<Type>&);
private:
ListNode<Type>* first;
};
template<class Type>
void List<Type>::insert(Type k) {
ListNode<Type> *newnode = new ListNode<Type>(k);
newnode->link = first;
first = newnode;
}
template<class Type>
void List<Type>::show() {
for (ListNode<Type>* current = first; current; current = current->link) {
cout << current->data;
if (current->link) cout << "->";
}
cout << endl;
}
template<class Type>
void List<Type>::Delete(Type item) {
ListNode<Type>* previous = 0;
ListNode<Type>* current;
for (current = first; current&¤t->data != item;previous = current,current =current->link)
{
;//do nothing
}
if (current) {
if (previous) previous->link = current->link;
else first = first->link;
delete current;
}
}
template<class Type>
void List<Type>::invert() {
ListNode<Type> *p = first, *q = 0,*r =0;
while (p) {
r = q;
q = p;
p = p->link;
q->link = r;
}
first = q;
}
template<class Type>
void List<Type>::concatenate(List<Type>& b) {
if (!first) { first = b.first; return; }
if (!b.first) {
ListNode<Type>* p;
for (p = first; p->link; p = p->link);//找到末尾
p->link = b.first;
}
}
#endif
main.cpp
#include<iostream>
#include"list.h"
using namespace std;
int main() {
List<int> intList;
intList.insert(12);
intList.insert(25);
intList.insert(55);
intList.insert(89);
intList.show();
intList.invert();
intList.show();
intList.Delete(25);
intList.show();
cout << "测试。。。" << endl;
List<char> charList;
charList.insert('A');
charList.insert('B');
charList.insert('C');
charList.insert('D');
charList.show();
List<char> charList2;
charList.insert('a');
charList.insert('b');
charList.insert('c');
charList.insert('d');
charList.concatenate(charList2);
charList.show();
system("pause");
return 0;
}