C++ Implementation of Singly Linked List

Because I will graduate to work in one year, I feel that the hand of coding is a bit raw, especially for data structures and algorithms, so I have never blogged and started blogging. The blog is updated as it happens, and it is only for recording purposes. You are welcome to point out mistakes and places that do not conform to general programming habits.

Let's write the simplest linked list first. The following is the header file:

//List.h
class LNode
{
public:
	int data; //data field
	LNode * next; //Pointer field
};

class List
{
public:
	List();
	~List();
	void CreateList();
	void Insert(int data, int index, bool flag = true);
	void Delete(int index);
	void printList();
	void getData(int index);
	void exchange(int index1, int index2);
	int getLength();
private:
	LNode *head;//head node
};

The specific implementation file and the main function of the test:

//List.cpp
#include<iostream>
#include"List.h"
using namespace std;

List::List()
{
	head = new LNode;
	head->next = NULL;
	cout << "Object created successfully!" << endl;
}

List::~List()
{
	LNode *p = head, *s;
	while (p->next != NULL){
		s = p->next;
		p = s->next;
		delete s;
	}
	delete p;
	delete head;
}

int List::getLength()
{
	int len ​​= 0;
	LNode *p = head;
	while (p->next!=NULL)
	{
		len ++;
		p = p->next;
	}
	return len;
}

void List::CreateList()
{
	int data_num,data;
	cout << "Start to create a linked list (tail insertion), please enter the number of data: ";
	cin >> data_num;
	for (int i = 0; i < data_num;i++)
	{
		cout << "Please enter the first" << i + 1 << "data: ";
		cin >> data;
		Insert(data, getLength() + 2, false);//I didn't use the serious tail insertion here, because there is already an Insert function
	}
	cout << "CreateList: Create complete" << endl;
}

//Insert the element data into the index position
void List::Insert(int data, int index,bool flag)
{
	LNode *p = head, *s;
	if (index<=0)
	{
		cout << "Insert failed, please enter a number greater than 0!" << endl;
		return;
	}
	if (getLength() + 1 < index)
	{
		while (p->next != NULL)
		{
			p = p->next;
		}
		s = (LNode *)new LNode[1];
		s->data = data;
		s->next = NULL;
		p->next = s;
		if (flag != false)
		{
			cout << "Insert: The length of the linked list is " << getLength()-1 << ", cannot be inserted into the "<< index << " position;";
			cout << "Now insert the data" << data << "Insert the end of the linked list!" << endl;
		}
	}
	else
	{
		for (int i = 0; i < index-1; i++)
		{
			p = p->next;
		}
		s = (LNode *)new LNode[1];
		s->data = data;
		s->next = p->next;
		p->next = s;
		cout << "数据" << data <<"插入" << "第" << index <<"位置成功!" << endl;
	}
}

// delete element
void List::Delete(int index)
{
	LNode *p = head, *s;
	int i = 0;
	if (index <= 0 || index>getLength())
	{
		cout << "The element to be deleted is not in the linked list" << endl;
		return;
	}
	while (i<index-1)
	{
		i++;
		p = p->next;
	}
	
	s = p->next;
	p->next = s->next;
	cout << "The first" << index << "The element was deleted successfully!" << endl;
	delete s;
}

//print linked list
void List::printList()
{
	LNode *p = head;
	cout << "printList: ";
	if (p->next==NULL)
	{
		cout << "空链表" << endl;
		return;
	}
	p = p->next;
	while (p!=NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}

// swap elements
void List::exchange(int index1, int index2)
{
	LNode *p = head, *s1, *s2;
	s1 = (LNode *)new LNode[1];
	s2 = (LNode *)new LNode[1];
	int data;
	int min = (index1 > index2) ? index2 : index1;
	int max = (index1 > index2) ? index1 : index2;
	if (min<=0 || max>getLength())
	{
		cout << "The element to be exchanged exceeds the range of the linked list" << endl;
		return;
	}
	p = p->next;
	for (int i = 1; i <= max;i++)
	{
		if (i==min)
		{
			s1 = p;
		}
		if (i==max)
		{
			s2 = p;
		}
		p = p->next;
	}
	data = s1->data;
	s1->data = s2->data;
	s2->data = data;
	cout << "th" << index1 << "element and " << "th" << index2 << "element exchanged successfully" << endl;
}

// print the index-th element
void List::getData(int index)
{
	LNode *p = head;
	if (index <= 0 || index>getLength())
	{
		cout << "The element to be searched is not in the linked list" << endl;
		return;
	}
	for (int i = 0; i < index;i++)
	{
		p = p->next;
	}
	cout << "第" << index << "元素为" << p->data << endl;
}
intmain()
{
	List list;
	list.CreateList();
	list.printList();
	list.Insert(10, 2);
	list.Insert(20, 10);
	list.printList();
	list.Delete(2);
	list.printList();
	list.getData(1);
	list.getData(2);
	list.getData(3);
	list.getData(4);
	list.getData(10);
	list.exchange(1,2);
	list.printList();
	list.exchange(4, 3);
	list.printList();
	return 0;
}

The results are as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326482881&siteId=291194637