1. 程式人生 > >C++ 實現簡單的連結串列操作

C++ 實現簡單的連結串列操作

C++簡單鏈表 實現幾個基本操作  其中倒敘,排序方法 使用陣列來生成連結串列,導致 刪除方法 使用delete p1 出錯 如需修改可評論
#include <iostream>
#include <string.h>


using namespace std;

class Person
{
public:
	Person();
	~Person();
	void setID(int id);
	void setName(const char * name);
	int getID();
	char * getName() const;
	void setNext(Person * a);
	Person * getNext() const;
private:
	int ID;
	char * Name;
	Person * next;

};

Person::Person() :ID(0), next(nullptr), Name(NULL)
{

}

Person::~Person()
{
}

void Person::setID(int id) {

	ID = id;
}

void Person::setName(const char * name) {
	Name = new char[strlen(name) + 1];
	strcpy_s(Name, strlen(name) + 1, name);
}

int Person::getID() {
	return ID;
}

char * Person::getName() const {

	return Name;
}

Person * Person::getNext() const {
	return next;
}

void  Person::setNext(Person * a) {
	next = a;
}

class linklist
{
public:
	linklist();
	~linklist();
	void addLink(const int id, const char * Name);//新增一個節點
	void deletelist();//刪除連結串列
	void insetNode(int position, const int id, const char * Name);//插入節點
	void reserve(); //倒序
	void sort();//排序
	Person * find(const int id) const;//查詢一個節點
	void deleteNode(int position); //刪除固定的節點
	void showList() const;//顯示整個連結串列
	int getCount() const;//顯示節點數目
	Person * getHead() const;
private:
	Person * head;
	int count;
};

void linklist::sort() {
	Person * p1, *p2;
	p1 = head->getNext();
	p2 = new Person[count];
	for (int i = 0; i<count; i++) {
		if (i != count - 1) {
			p2[i].setNext(&p2[i + 1]);
		}
		else {
			p2[i].setNext(NULL);
		}

	}
	for (int i = 0; i<count; i++) {
		p2[i].setName(p1->getName());
		p2[i].setID(p1->getID());
		for (int j = 0; j<i; j++) {
			if (p2[i].getID()<p2[j].getID()) {
				Person temp;
				temp.setName(p2[j].getName());
				temp.setID(p2[j].getID());
				p2[j].setName(p2[i].getName());
				p2[j].setID(p2[i].getID());
				p2[i].setName(temp.getName());
				p2[i].setID(temp.getID());
			}
		}
		p1 = p1->getNext();
	}
	p1 = head->getNext();
	delete[] p1;
	this->head->setNext(p2);
}


Person * linklist::getHead() const {

	return head;
}

int linklist::getCount() const {

	return count;

}

void linklist::showList() const {
	Person  *p2;
	p2 = head->getNext();
	if (!p2) {
		cout << "list is empty" << endl;
	}
	while (p2->getNext() != NULL) {
		cout << "ID:" << p2->getID() << endl;
		cout << "Name:" << p2->getName() << endl;
		p2 = p2->getNext();
	}
	cout << "ID:" << p2->getID() << endl;
	cout << "Name:" << p2->getName() << endl;
}

linklist::linklist() :count(0)
{
	head = new Person;
	head->setNext(NULL);
	head->setName("head");
	head->setID(0);
}

linklist::~linklist()
{
	delete head;
}

void linklist::addLink(const int id, const char * Name) {

	Person * p1, *p2;
	p2 = new Person;
	p1 = head;
	while (p1->getNext() != NULL)
	{
		p1 = p1->getNext();
	}
	p2->setID(id);
	p2->setName(Name);
	p2->setNext(NULL);
	p1->setNext(p2);
	count++;
}

void linklist::deletelist() {
	Person * p1, *p2;
	p1 = head->getNext();
	head->setNext(NULL);
	while (p1 != NULL)
	{
		p2 = p1->getNext();
		p1->setNext(NULL);
		delete p1;
		p1 = p2;
	}
	count = 0;
}

void linklist::insetNode(int position, const int id, const char * Name) {
	count++;
	Person * p1, *p2 = NULL;
	p1 = head;
	Person * insert_person = new Person;
	if (position > count || position < 1) {
		cout << "輸入位置錯誤" << endl;
		return;
	}
	else {

		for (int i = 0; i < position; i++) {
			p2 = p1;
			p1 = p1->getNext();
		}

		insert_person->setID(id);
		insert_person->setName(Name);
		insert_person->setNext(p1);
		p2->setNext(insert_person);

	}
	cout << "insert sucessful" << endl;
}

void linklist::reserve() {
	Person * p1, *p2;
	Person * reser = new Person[count];
	p1 = head->getNext();
	for (int i = 0; i < count; i++) {
		if (i != count - 1)
			reser[i].setNext(&reser[i + 1]);
		else {
			reser[i].setNext(NULL);
		}
		reser[count - i - 1].setID(p1->getID());
		reser[count - i - 1].setName(p1->getName());
		p1 = p1->getNext();
	}
	p1 = head->getNext();
	head->setNext(NULL);
	while (p1 != NULL)
	{
		p2 = p1->getNext();
		delete p1;
		p1 = p2;
	}
	head->setNext(reser);
}



Person * linklist::find(int  id) const {

	Person * temp;
	temp = head->getNext();
	while (temp->getID() != id&&temp->getNext() != NULL)
	{
		temp = temp->getNext();
	}

	if (temp->getNext() == NULL) {

		return NULL;
	}
	return temp;

}

void linklist::deleteNode(int position) {
	Person * temp, *p1 = NULL;
	count--;
	temp = head;
	if (position > count && position < 0)
	{
		cout << "Worry position " << endl;
		return;
	}
	for (int i = 0; i < position; i++)
	{
		p1 = temp;
		temp = temp->getNext();
	}
	p1->setNext(temp->getNext());
	delete temp;
}

int main(void) {

	linklist a;
	a.addLink(1, "a");
	a.addLink(2, "b");
	a.addLink(3, "c");
	a.showList();
	a.reserve();
	a.showList();
	cout << "FOUND IT" << endl;
	cout << "Name:" << a.find(2)->getName() << endl;
	cout << "ID:" << a.find(2)->getID() << endl;
	a.insetNode(2, 10, "insert");
	a.showList();
	a.deleteNode(2);
	a.showList();
	a.sort();
	a.showList();
	return 0;
}