The basic operations of C++ linked list-the construction of the linked list, the destruction of the linked list, the insertion of the linked list, the deletion of the linked list, the inversion of the linked list, the inversion of the head insertion


Some basic operations of the linked list are given below:

Constructor of linked list

template <typename T>
List<T>::List()
{
    
    
	head = new node<T>;
	head->next=NULL;
	count=0;
}

Destructor of linked list

template <typename T>
List<T>::~List<T>()
{
    
    
	node<T>* p;
	while(head->next != NULL)
	{
    
    
		p = head->next;
		head->next = p->next;
		delete p;
		count--;
	}
	delete head;
}

Create linked list

template <typename T>
void List<T>::createList(T x)
{
    
    
	node<T>* rear = head;
	while (rear->next!=NULL)
		rear = rear->next;
	node<T>* p = new node<T>;
	p->data = x;
	p->next = NULL;
	rear->next = p;
	rear = p;//这一步似乎可要可不要
	count++;
}

Linked list insertion

template <typename T>
void List<T>::insert(T x,int num)
{
    
    
	if (num>count+1)
	{
    
    
		cout << "No location to inserte!" << endl;
		return;
	}
	int n=0;
	node<T>* tem=head;
	while (num-1!=n)
	{
    
    
		tem=tem->next;
		n++;
	}
	node<T>* p=new node<T>;
	p->next=tem->next;
	tem->next=p;
	p->data=x;
	count++;
}

Delete of linked list

template <typename T>
void List<T>::del(int n)
{
    
    
	int num=count-n,k=0;
	node<T>* p=head;
	while(num!=k)
	{
    
    
		p=p->next;
		k++;
	}
	p->next=p->next->next;
	count--;
}

In-place inversion of linked list

Regarding the two inversions, I want to talk about it a little bit. In-place inversion is to change the direction of next and finally realize the flip. A simple diagram is roughly as follows:
Insert picture description here

template <typename T>
void List<T>::reverse1()
{
    
    
	node<T>* q=NULL;
	node<T>* temp;
	head=head->next;//这一步操作是避免原来head->next->data随机取值导致逆置的链表多出一个随机数。
	while(head!=NULL)
	{
    
    
		temp=head;
		head=head->next;
		temp->next=q;
		q=temp;
	}
	node<T>* new_head=new node<T>;//构建一个头结点,否则逆置之后的head->next指向第二个数,观察print()函数可知,打印的第一项是head->next。
	new_head->next=temp;
	head = new_head;
}

The head of the linked list is reversed

This one also draws a rough diagram, I hope you can compare with the above picture, and be inspired:
Insert picture description here

template <typename T>
void List<T>::reverse2()
{
    
    
	node<T>* p = head->next;
	node<T>* q;
	head->next=NULL;
	while(p)
	{
    
    
		q=p;
		p=p->next;
		q->next=head->next;
		head->next=q;
	}
}

The complete code is as follows

#include <iostream>
using namespace std;

template <typename T>
struct node
{
    
    
	T data;
	node* next;
};

template <typename T>
class List
{
    
    
public:
	List();//构造一个链表
	~List();//析构一个链表
	void createList(T x);//创建一个链表
	void insert(T x,int num);//这特定的位置插入结点
	void del(int n);//删除链表倒数第 n 个结点
	int length();//求链表的长度
	void reverse1();//就地转置
	void reverse2();//头插法逆置
	void print();//打印结果
private:
	int count;
	node<T>* head;
};


template <typename T>
List<T>::List()
{
    
    
	head = new node<T>;
	head->next=NULL;
	count=0;
}

//这个链表的析构是不是正确的呀?首先头结点好像没有析构,其次最后一步能不能直接delete head,head好像不是new出来的。
template <typename T>
List<T>::~List<T>()
{
    
    
	node<T>* p;
	while(head->next != NULL)
	{
    
    
		p = head->next;
		head->next = p->next;
		delete p;
		count--;
	}
	delete head;
}

template <typename T>
void List<T>::createList(T x)
{
    
    
	node<T>* rear = head;
	while (rear->next!=NULL)
		rear = rear->next;
	node<T>* p = new node<T>;
	p->data = x;
	p->next = NULL;
	rear->next = p;
	rear = p;//这一步似乎可要可不要
	count++;
}

template <typename T>
int List<T>::length()
{
    
    
	return count;
//如果没有设置count这个变量,也可以用下面这种方法:
	// int count=0;
	// while(head->next!=NULL)
	// {
    
    
	// 	head=head->next;
	// 	count++;
	// }
	// return count;
}

template <typename T>
void List<T>::insert(T x,int num)
{
    
    
	if (num>count+1)
	{
    
    
		cout << "No location to inserte!" << endl;
		return;
	}
	int n=0;
	node<T>* tem=head;
	while (num-1!=n)
	{
    
    
		tem=tem->next;
		n++;
	}
	node<T>* p=new node<T>;
	p->next=tem->next;
	tem->next=p;
	p->data=x;
	count++;
}

template <typename T>
void List<T>::del(int n)
{
    
    
	int num=count-n,k=0;
	node<T>* p=head;
	while(num!=k)
	{
    
    
		p=p->next;
		k++;
	}
	p->next=p->next->next;
	count--;
}

	// void reverse();
	// void connect();//两个有序的链表合并
	// T answer();//求中间结点n
template <typename T>
void List<T>::print()//打印结果
{
    
    
	node<T>* p = head->next;
	while(p->next!=NULL)
	{
    
    
		cout << p->data << " ";
		p=p->next;
	}	
	cout << p->data << endl;
}

template <typename T>
void List<T>::reverse1()
{
    
    
	node<T>* q=NULL;
	node<T>* temp;
	head=head->next;//这一步操作是避免原来head->next->data随机取值导致逆置的链表多出一个随机数。
	while(head!=NULL)
	{
    
    
		temp=head;
		head=head->next;
		temp->next=q;
		q=temp;
	}
	node<T>* new_head=new node<T>;//构建一个头结点,否则逆置之后的head->next指向第二个数,观察print()函数可知,打印的第一项是head->next。
	new_head->next=temp;
	head = new_head;
}

template <typename T>
void List<T>::reverse2()
{
    
    
	node<T>* p = head->next;
	node<T>* q;
	head->next=NULL;
	while(p)
	{
    
    
		q=p;
		p=p->next;
		q->next=head->next;
		head->next=q;
	}
}

int main()
{
    
    
	List<int> list;
	for(int i=0;i<5;i++)
		list.createList(i);
	cout << "The length of this list is " << list.length() << endl << "The list is as follows: ";
	list.print();
	cout << "-------------" << endl;
	list.insert(400,6);
	cout << "The length of this list is " << list.length() << endl << "The list is as follows: ";
	list.print();
	cout << "-------------" << endl;
	list.del(1);
	cout << "The length of this list is " << list.length() << endl << "The list is as follows: ";
	list.print();
	cout << "-------------" << endl;
	list.reverse1();
	cout << "The length of this list is " << list.length() << endl << "The list is as follows: ";
	list.print();
	cout << "-------------" << endl;
	list.reverse2();
	cout << "The length of this list is " << list.length() << endl << "The list is as follows: ";
	list.print();

	return 0;
}

The results of the operation are attached below

Insert picture description here
For related knowledge of linked lists, you can refer to my other blog, and attach its link
below: link .
If it is helpful to you, you may wish to like it and pay attention to it!

Guess you like

Origin blog.csdn.net/Freedom_cao/article/details/107501153