Simple simulation list container

Keywords: data structure linked list list


I'm a little white. I'm here to share some daily programming with you. I hope the big guys will support me a lot!!! ❤️❤️❤️

container

Containers: used to manage a collection of objects of a certain type. Various data structures, such as vector, list, deque, set and map, are used to store data. From the implementation point of view, STL container is a class template.

iterator

Iterators: used to traverse elements of a collection of objects. It acts as the glue between container and algorithm. There are five types. From the implementation point of view, iterator is a class template that overloads pointer related operations such as operator *, operator - >, operator + +, operator -. All STL containers come with their own iterators. Only the designer of the container knows how to traverse its own elements. A native pointer is also an iterator.

List

List is realized by double linked list, and the elements are also stored in the heap. Each element is placed in a piece of memory. Its memory space can be discontinuous. Data access is carried out through pointers. This feature makes its random access unusual and inefficient, so it does not provide overloading of [] operator.
However, due to the characteristics of linked list, it can efficiently support insertion and deletion operations anywhere.
The access start and the last two elements are the fastest, and the access time of other elements is the same.

Advantages, disadvantages and applicable scenarios
Advantages: discontinuous memory, dynamic operation, can be inserted or deleted at any position, and high efficiency.
Disadvantages: random access is not supported.
Applicable scenario: it is applicable to scenarios where insertion and deletion operations are often performed and random access is not often performed.

Source code:

#include<iostream>
using namespace std;
template <class T>
struct Node {
public:
	Node(T data):data(data){}//structure
	Node(T data,Node<T>* next):data(data),next(next){}//structure
	T& getdata() {//Get data
		return data;
	}
	Node<T>* &getnext() {//Get next
		return next;
	}
protected:
	T data;//Data domain
	Node<T>* next;//Pointer field
};
template <class T>
class List {
public:
	List() {//structure
		headNode =tailNode = nullptr;
		cursize = 0;
	}
	~List() {//Deconstruction
		Node<T>* pmove = headNode;
		while (!empty()) {
			delete pmove;
		}
	}
	void push_front(T data) {//Head insertion
			headNode = new Node<T>(data, headNode);
			cursize++;
	}
	void push_back(T data) {//Tail interpolation
		Node<T>* newNode = new Node<T>(data);
		tailNode->getnext() = newNode;
		tailNode = newNode;
		cursize++;
	}
	void pop_front() {//Header deletion
		Node<T>* tempNode = headNode->getnext();
		delete headNode;
		headNode = tempNode;
		cursize--;
	}
	void pop_back() {//Tail deletion
		Node<T>* leftNode = headNode;
		Node<T>* rightNode = headNode;
		while (leftNode->getnext()) {
			leftNode = rightNode;
			rightNode = rightNode->getnext();
		}
		delete tailNode;
		tailNode = leftNode;
		tailNode->getnext() = nullptr;
		cursize--;
	}
	int size() {//Get the number of nodes
		return cursize;
	}
protected:
	Node<T>* headNode;//Head node
	Node<T>* tailNode;//Tail node
	int cursize;//Number of nodes
public:
	class iterator {//Built in iterator
	public:
		iterator() {//structure
			pmove = nullptr;
		}
		iterator(Node<T>* move):pmove(move){}//structure
		iterator operator=(Node<T>* move) {
			pmove = move;
			return iterator(pmove);
		}
		bool operator!=(Node<T>* move) {//Reload=
			return pmove != move;
		}
		void operator++(int) {//Heavy load++
			pmove = pmove->getnext();
		}
		T operator*() {//Heavy load*
			return pmove->getdata();
		}
	protected:
		Node<T>* pmove;//Move pointer
	};
	Node<T>* begin() {//Get header node
		return headNode;
	}
	Node<T>* end() {//Get tail node
		return tailNode;
	}
	bool empty() {//Judge whether there are nodes
		return cursize == 0;
	}
};

Test code:

void test() {
	const int arraySize = 4;
	string Man[arraySize] = { "He Meimei 1","He Meimei 2" ,"He Meimei 3","He Meimei 4" };
	List<string> list;
	for (int i = 0; i < arraySize; i++) {
		list.push_front(Man[i]);
	}
	List<string>::iterator iter;
	for (iter = list.begin(); iter != list.end(); iter++)
		cout << *iter << endl;
	cout << "......................"<<endl;
	list.pop_front();
	for (iter = list.begin(); iter != list.end(); iter++)
		cout << *iter << endl;
	cout << "......................"<<endl;
	list.pop_back();
	for (iter = list.begin(); iter != list.end(); iter++)
		cout << *iter << endl;
	return 0;
}

Operation effect:

Posted by chaiwei on Thu, 09 Sep 2021 12:21:34 -0700