1. 程式人生 > 其它 >連結串列操作總結

連結串列操作總結

技術標籤:資料結構

宣告

一年前閒著無聊時,研究研究了連結串列,當時寫的時候也花了不少時間,當時我的思路如下

動態連結串列帶頭結點

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
}

	head->id =-1;
	head-
>next = NULL; node* pCur = head; node* pNew = NULL; int data; while (1) { cout <<"請輸入資料"; cin>>data; if (data == -1) { break; } pNew = (node*)malloc(sizeof(node)); if (pNew == NULL) { continue; } pNew->id = data; pNew->next = NULL; //連結串列建立關係
pCur->next = pNew; pNew->next = NULL; pCur = pNew; } return head; } int ListPrint(node* head) { if (head == NULL) { return -1; } //取出第一個有效結點,head的下一次結點 node* pCur = head->next; cout << "head->"; while (pCur != NULL) { cout <<pCur->id<<
"->"; pCur = pCur->next; } cout << "NULL\n"; return 0; } int main() { node* head = NULL; head = ListCreat(); ListPrint(head); cout << " "; system("pause"); return 0; }

結點的插入

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
//在值為x的結點前,插入值為y的結點,若x結點不存在,則插在表尾
int increasenode(node* head, int x, int y)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	node* pNew = (node*)malloc(sizeof(node));
	if (pNew == NULL) {
		return -2;
	}
	pNew->id = y;
	pNew->next = NULL;

	Pre->next = pNew;
	pNew->next = Cur;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x; int y;
	cout << "分別輸入x和y,在x的前面插入y" << endl;
	cin >> x >> y;
	increasenode(head, x, y);
	cout << "在x的前面插入y:";
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

靜態連結串列

#include <iostream>
using namespace std;
class node {
public:
	int id;
	char name[100];
	node* next;

};
int  main() {

	node s1 = { 1,"biaobiao",NULL };
	node s2 = { 2,"aha",NULL };
	node s3 = { 3,"leilei",NULL };
	s1.next = &s2;
	s2.next = &s3;
	s3.next = NULL;
	node* p = &s1;
	while (p != NULL) {
		cout << p->id << "   " << p->name << "   " << endl;
		p = p->next;
	}
}

連結串列反轉

#include<iostream>
using namespace std;
 class node {
public:
	int id;
	node* next;
};
int noderexerse(node* head) {
	if (head == NULL || head->next == NULL || head->next->next == NULL)
	{
		return -1;
	}
	node* pre = head->next;
	node* cur = pre->next;
	node* temp=NULL;
	while (cur != NULL)
	{
		temp=cur->next;
		cur->next = pre;

		pre = cur;
		cur = temp;
	}
	head->next->next = NULL;
	head->next = pre;
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}

		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	noderexerse(head);
	cout << "反轉後為:" << endl;
	ListPrint(head);
}

清空動態建立的連結串列

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
//清空連結串列
int destroynode(node* head) {
	if (head == NULL) {
		return -1;
	}
	node* temp = NULL;
	int i = 0;
	while (head != NULL)
	{
		temp = head->next;
		free(head);
		head = NULL;
		head = temp;
		i++;
	}
	cout << "釋放次數為:" << i<<endl;
	return 0;
}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	destroynode(head);
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;
}

刪除第一個為x的結點

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int freecode(node*head,int x)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	int flag = 0;//0沒有找到,1找到了
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			Pre->next = Cur->next;
			free(Cur);
			Cur = NULL;
			flag = 1;
			break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	if (flag == 0)
	{
		cout << "沒有值為" << x << "的結點" << endl;
		return -2;
	}


}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x;
	cout << "請輸入需要刪除的結點" << endl;
	cin >> x;
	freecode(head, x);
	cout << "刪除後結果為:" << endl;
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

刪除所有為x的結點

#include <iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int freecode(node* head, int x)
{
	if (head == NULL)
	{
		return -1;
	}
	node* Pre = head;
	node* Cur = head->next;
	int flag = 0;//0沒有找到,1找到了
	while (Cur != NULL)
	{
		if (Cur->id == x)
		{
			Pre->next = Cur->next;
			free(Cur);
			Cur = NULL;
			flag = 1;
			Cur = Pre->next;
			continue;
			//break;
		}
		Pre = Cur;
		Cur = Cur->next;
	}
	if (flag == 0)
	{
		cout << "沒有值為" << x << "的結點" << endl;
		return -2;
	}


}
node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	int x;
	cout << "請輸入需要刪除的結點" << endl;
	cin >> x;
	freecode(head, x);
	cout << "刪除後結果為:" << endl;
	ListPrint(head);
	cout << " ";
	system("pause");
	return 0;

}

升序連結串列插入

#include<iostream>
using namespace std;
 struct node {

	int id;
	node* next;
};
//假如原來連結串列是升序 的,升序後插入新節點
//不能插入結點後再排序,是升序插入新節點x
int  insertnode(node* head, int x) {
	
	 if (head == NULL)
	 {
		 return -1;
	 }
	 node* Pre = head;
	 node* Cur = head->next;
	 while (Cur != NULL)
	 {
		 if (Cur->id > x)
		 {
			 break;
		 }
		 Pre = Cur;
		 Cur = Cur->next;
	 }
	 node* pNew = (node*)malloc(sizeof(node));
	 if (pNew == NULL) {
		 return -2;
	 }
	 pNew->id = x;
	 pNew->next = NULL;

	 Pre->next = pNew;
	 pNew->next = Cur;

  }
int  nodesort(node* head)
{
	if (head == NULL || head->next == NULL)
	{
		return 0;
	}
	node* pre = NULL;
	node* cur = NULL;
	node temp;
	for (pre = head->next; pre->next != NULL; pre = pre->next)
	{
		for (cur = pre->next; cur != NULL; cur = cur->next)
		{
			if (pre->id > cur->id)
			{
				temp.id = cur->id;
				cur->id = pre->id;
				pre->id = temp.id;
				/*
				temp = *cur;
				*cur = *pre;
				*pre = temp;
				temp.next = cur->next;
				cur->next = pre->next;
				pre->next = temp.next;*/
			}
		}
	}
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	nodesort(head);
	int x;
	cout << "輸入想要升序插入的數:" << endl;
	cin >> x;
	cout << "升序插入" << x << "後:" << endl;
	insertnode(head, x);
	ListPrint(head);
}

用連結串列進行排序

#include<iostream>
using namespace std;
class node {
public:
	int id;
	node* next;
};
int  nodesort(node* head) {
	if (head == NULL || head->next == NULL)
	{
		return 0;
	}
	node* pre = NULL;
	node* cur = NULL;
	node temp;
	for (pre = head->next; pre->next != NULL; pre = pre->next)
	{
		for (cur = pre->next; cur != NULL; cur = cur->next)
		{
			if (pre->id > cur->id)
			{
				temp.id = cur->id;
				cur->id = pre->id;
				pre->id = temp.id;
				/*
				temp = *cur;
				*cur = *pre;
				*pre = temp;
				temp.next = cur->next;
				cur->next = pre->next;
				pre->next = temp.next;*/
			}
		}
	}
	return 0;
}

node* ListCreat() {
	node* head = NULL;

	head = (node*)malloc(sizeof(node));
	if (head == NULL)
	{
		return NULL;
	}

	head->id = -1;
	head->next = NULL;

	node* pCur = head;
	node* pNew = NULL;

	int data;
	while (1) {
		cout << "請輸入資料";
		cin >> data;
		if (data == -1) {
			break;
		}


		pNew = (node*)malloc(sizeof(node));
		if (pNew == NULL) {
			continue;
		}

		pNew->id = data;
		pNew->next = NULL;
		//連結串列建立關係
		pCur->next = pNew;

		pNew->next = NULL;

		pCur = pNew;
	}
	return head;
}
int  ListPrint(node* head) {

	if (head == NULL)
	{
		return -1;
	}
	//取出第一個有效結點,head的下一次結點
	node* pCur = head->next;
	cout << "head->";
	while (pCur != NULL)
	{
		cout << pCur->id << "->";
		pCur = pCur->next;
	}
	cout << "NULL\n";
	return 0;
}
int  main() {
	node* head = NULL;
	head = ListCreat();
	ListPrint(head);
	nodesort(head);
	cout << "排序後為:";
	ListPrint(head);
}