<3> C++链表的基本实现 2021-05-27


#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//链表的实现 和虚拟头节点 
template <typename T>
class LinkedList
{
private:
	class Node
	{
	public:
		T e;
		Node* next;
	public:
		Node():e(NULL),next(NULL){}
		Node(T e){this->e=e;next=NULL;}
		Node(T e,Node* next){this->e=e;this->next=next;}
	public:
		string toString(){
			stringstream ss;
			ss<<e;
			return ss.str();
		}
	};
	Node* dummyHead;//虚拟头节点 
	int size;
public:
	LinkedList(){
		dummyHead = new Node(0);
		size =0;
	}
	int getSize(){return size;}
	bool isEmpty(){return size==0;}
	void addFirst(T e){ add(0,e);}
	void add(int index,T e){
		if(index<0 ||index>size)
			throw "Add failed.Illegal index.";
		Node* prev=dummyHead;
		for(int i=0;i<index;i++)
			prev=prev->next;
		prev->next=new Node(e,prev->next);
		size++;
	}
	void addLast(T e){ add(size,e);} 
	T get(int index){
		if(index<0 ||index>size)
			throw "Add failed.Illegal index.";
		Node* cur=dummyHead->next;
		for(int i=0;i<index;i++) cur=cur->next;
		return cur->e;
	}
	T getFirst(){return get(0);}
	T getLast(){ return get(size-1);}
	void set(int index,T e){
		if(index<0 ||index>size)
			throw "Add failed.Illegal index.";
		Node* cur=dummyHead->next;
		for(int i=0;i<index;i++) cur=cur->next;
		cur->e=e;
	}
	bool contains(T e){
		Node* cur=dummyHead->next;
		while (cur!=NULL){
			if(cur->e==e) return true;
			cur=cur->next;
		} 
		return false;
	}
	void remove(int index){
		if(index<0 ||index>size)
			throw "Add failed.Illegal index.";
		Node* prev=dummyHead;
		for(int i=0;i<index;i++) prev=prev->next;
		Node* retNode=prev->next;
		prev->next=retNode->next;
		size--;
		delete retNode;
	}
	void removeFirst(){return remove(0);}
	void removeLast(){return remove(size-1);}
	string toString(){
		string res="LinkedList:";
		Node* cur=dummyHead->next;
		stringstream ss;
		while (cur!=NULL){
			ss<<cur->e;
			res.append(ss.str()+"->");
			ss.str("");
			cur=cur->next;
		} 
		res.append("NULL\n");
		return res;
	}
};
void TestLinkedList(){
	LinkedList<int> linkedlist;
	for(int i=0;i<5;i++){
		linkedlist.addFirst(i);
		cout<<linkedlist.toString();
	}
	linkedlist.add(2,666);
	cout<<linkedlist.toString();
	linkedlist.remove(2);
	cout<<linkedlist.toString();
	linkedlist.removeFirst();
	cout<<linkedlist.toString();
	linkedlist.removeLast();
	cout<<linkedlist.toString();
}
//基于链表来实现栈 
template <typename T>
class LinkedListStack
{
public:
	LinkedList<T> list;
public:
	LinkedListStack(){}
	int getSize(){return list.getSize();}
	bool isEmpty(){return list.isEmpty();}
	void push(T e){return list.addFirst(e);}
	T top(){return list.getFirst();}
	void pop(){return list.removeFirst();}
	string toString(){
		string res="Stack: top";
		res.append(list.toString());
		return res;
	}
};
void TestLinkedListStack(){
	LinkedListStack<int> list;
	for(int i=0;i<5;i++){
		list.push(i);
		cout<<list.toString();
	}
	list.pop();
	cout<<list.toString();
	list.pop();
	cout<<list.toString();
	list.pop();
	cout<<list.toString();
}
int main(int argc, char *argv[])
{
	//TestLinkedList();
	TestLinkedListStack();
	return 0;
}
//基于链表的队列 
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <typename T>
class LinkedListQueue
{
private:
	class Node
	{
	public:
		T e;
		Node* next;
	public:
		Node():e(NULL),next(NULL){}
		Node(T e){this->e=e;next=NULL;}
		Node(T e,Node* next){this->e=e;this->next=next;}
	public:
		string toString(){
			stringstream ss;
			ss<<e;
			return ss.str();
		}
	};
	Node* head;
	Node* tail;
	int size;
public:
	LinkedListQueue(){ head=tail=NULL;size=0;}
	int getSize(){return size;}
	bool isEmpty(){return size==0;}
	void enqueue(T e){
		if(tail==NULL){
			tail=new Node(e);
			head=tail;
		}
		else{
			tail->next=new Node(e);
			tail=tail->next;
		}
		size++;	
	}
	void dequeue(){
		if(isEmpty()) throw "Cannot dequeue from an empty queue.";
		Node* retNode=head;
		head=head->next;
		if(head==NULL) tail=head;
		delete retNode;
		size--;		
	}
	T getFront(){
		if(isEmpty()) throw "Queue is empty.";
		return head->e;
	}
	string toString(){
		string res="Queue:front ";
		Node* cur=head;
		stringstream ss;
		while (cur!=NULL){
			ss<<cur->e;
			res.append(ss.str()+"->");
			ss.str("");
			cur=cur->next;
		} 
		res.append("NULL tail\n");
		return res;
	}
};
void TestLinkedListQueue(){
	LinkedListQueue<int> queue;
	for(int i=0;i<10;i++){
		queue.enqueue(i);
		cout<<queue.toString();
		
		if(i%3==2){
			queue.dequeue();
			cout<<queue.toString();
		}
	}
}
int main(int argc, char *argv[])
{
	TestLinkedListQueue();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lybc2019/article/details/117332355