1. 程式人生 > >資料結構連結串列題目2:交換位置、3:合併

資料結構連結串列題目2:交換位置、3:合併

利用查詢、提取資料、插入、刪除、輸出等函式,順利搞定第二題

#include<iostream>
#define ok 0
#define error -1
using namespace std;

class ListNode {
public:
	int data;
	ListNode*next;
	ListNode() {
		next = NULL;
	}
};

class LinkList {
public:
	ListNode * head;
	int len;
public:
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_display();
	int LL_get(int i);
	int swap(int pa, int pb);
};
LinkList::LinkList() {
	head = new ListNode();
	len = 0;
}
LinkList::~LinkList() {
	ListNode*p, *q;
	p = head;
	while (p != NULL) {
		q = p;
		p = p->next;
		delete q;
	}
	len = 0;
	head = NULL;
}
void LinkList::LL_display() {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
int LinkList::LL_insert(int i, int item) {
	if (i < 1 || i> len + 1)
		return error;
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		len++;
		return ok;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	len++;
	return ok;
}
ListNode* LinkList::LL_index(int i) {
	if (i > len || i < 0) {
		return NULL;
	}
	if (i == 0)
		return head;
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}



int LinkList::LL_del(int i) {
	if (i > len || i<1)
		return error;
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	len--;
	return ok;
}
int LinkList::LL_get(int i) {
	if (i > len || i < 1) {
		return error;
	}
	ListNode*num = LL_index(i);
	if (num == NULL)
		return error;
	else
		return num->data;

}
int LinkList::swap(int pa, int pb) {
	if (pa<1 || pb<1 || pa>len || pb>len) {
		return error;
	}
	int m = LL_get(pa);
	int n = LL_get(pb);
	LL_insert(pa, n);
	LL_del(pa + 1);
	LL_insert(pb, m);
	LL_del(pb + 1);
	return ok;
}


int main() {
	LinkList sa;
	int num;
	cin >> num;
	int number;
	for (int i = 0; i < num; i++) {
		cin >> number;
		sa.LL_insert(i + 1, number);
	}
	sa.LL_display();
	int pa, pb, k;
	cin >> pa >> pb;
	k=sa.swap(pa, pb);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> pa >> pb;
	k=sa.swap(pa, pb);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	return 0;

}

第三題,有點問題存在

#include<iostream>
#define ok 0
#define error -1

using namespace std;

class ListNode {
public:
	int data;
	ListNode*next;
	ListNode() {
		next = NULL;
	}
};

ListNode* Lc;
int LL_merge(ListNode *La, ListNode *Lb);//要放在類宣告的後面

class LinkList {
public:
	ListNode * head;
	int len;
public:
	LinkList();
	~LinkList();
	int LL_insert(int i, int item);
	void LL_display();
};
LinkList::LinkList() {
	head = new ListNode();
	len = 0;
}
LinkList::~LinkList() {
	ListNode*p, *q;
	p = head;
	while (p != NULL) {
		q = p;
		p = p->next;
		delete q;
	}
	len = 0;
	head = NULL;
}
void LinkList::LL_display() {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
int LinkList::LL_insert(int i, int item) {
	if (i < 1 || i> len + 1)
		return error;
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		len++;
		return ok;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	len++;
	return ok;
}


int LL_merge(ListNode *La, ListNode *Lb) {
	ListNode* pa = La->next;
	ListNode* pb = Lb->next;
	Lc = La;
	ListNode* pc = Lc;
	while (pa&&pb) {
		if (pa->data <= pb->data) {
			pc->next = pb;
			pc = pa;
			pa = pa->next;
		}
		else {
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
		if (pa != NULL)
			pc->next = pa;
		else
			pc->next = pb;
	}
	La = Lc;
	return ok;
}

int main() {
	LinkList sa;
	int num1;
	cin >> num1;
	int number1;
	for (int i = 0; i < num1; i++) {
		cin >> number1;
		sa.LL_insert(i + 1, number1);
	}
	sa.LL_display();
	LinkList ta;
	int num2;
	cin >> num2;
	int number2;
	for (int i = 0; i < num2; i++) {
		cin >> number2;
		ta.LL_insert(i + 1, number2);
	}
	ta.LL_display();
	LL_merge(sa.head, ta.head);
	sa.LL_display();
	return 0;
}

最後終於順利解決問題:

#include<iostream>
#define ok 0
#define error -1

using namespace std;

class ListNode {
public:
	int data;
	ListNode*next;
	ListNode() {
		next = NULL;
	}
};

ListNode* Lc;

int LL_merge(ListNode *La, ListNode *Lb);

class LinkList {
public:
	ListNode * head;
	int len;
public:
	LinkList();
	~LinkList();
	int LL_insert(int i, int item);
	void LL_display();
};
LinkList::LinkList() {
	head = new ListNode();
	len = 0;
}
LinkList::~LinkList() {
	ListNode*p, *q;
	p = head;
	while (p != NULL) {
		q = p;
		p = p->next;
		delete q;
	}
	len = 0;
	head = NULL;
}
void LinkList::LL_display() {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
int LinkList::LL_insert(int i, int item) {
	if (i < 1 || i> len + 1)
		return error;
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		len++;
		return ok;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	len++;
	return ok;
}


int LL_merge(ListNode *La, ListNode *Lb) {
	ListNode* pa = La->next;
	ListNode* pb = Lb->next;
	ListNode* pc = La;
	Lc = pc;
	while (pa&&pb) {
		if (pa->data <= pb->data) {
			pc->next = pa;
			pc = pa;
			pa = pa->next;
		}
		else {
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
		if (pa != NULL)
			pc->next = pa;
		else
			pc->next = pb;
	}
	return ok;
}

int main() {
	LinkList sa;
	int num1;
	cin >> num1;
	int number1;
	for (int i = 0; i < num1; i++) {
		cin >> number1;
		sa.LL_insert(i + 1, number1);
	}

	LinkList ta;
	int num2;
	cin >> num2;
	int number2;
	for (int i = 0; i < num2; i++) {
		cin >> number2;
		ta.LL_insert(i + 1, number2);
	}

	LL_merge(sa.head, ta.head);
	ListNode *p = Lc->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
	return 0;
}

只是編譯後不能順利結束,關閉編譯框時會無響應後關掉