链表············

简单实现

链表实现了使用离散的空间来存储数据,节点包括数据域和指针域,可以使用单指针构成单向链表也可以使用双指针构成双向链表。下面使用单指针来实现简单的链表。

#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&&current->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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值