1. 程式人生 > >資料結構連結串列題目解析

資料結構連結串列題目解析

1.連結串列的查詢插入刪除

有問題的程式碼:

#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;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = NULL;
	pEnd = pS;
	while (pS->data) {
		if (head->next == NULL)
			head->next = pS;
		else
			pEnd->next = pS;
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		cout << head->next<<" ";
	}
	pEnd->next = NULL;
	delete pS;
	cout << head->next << " "<<endl;//沒有出去
	//return;
}
void LinkList::LL_display() {
	cout << len << " ";
	ListNode *p;
	p = head->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	return 0;
}



在輸入資料後沒反應,判斷是while迴圈沒有走出來,以至於在 LL_Create() 函式中沒有出去

修改:用 --len 來作為 while 的判斷條件,更加簡潔清晰:

#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;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = NULL;
	pEnd = pS;
	while (--len) {
		if (head->next == NULL)
			head->next = pS;
		else
			pEnd->next = pS;
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		cout << head->next<<" ";
	}
	pEnd->next = NULL;
	delete pS;
	cout << head->next << " "<<endl;//沒有出去
	//return;
}
void LinkList::LL_display() {
	cout << len << " ";
	ListNode *p;
	p = head->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	return 0;
}



此時發現輸出語句出了問題

其實還是賦值語句的while迴圈出了問題,為什麼認為這道題和課本上結構部分的連結串列是一樣的呢?一定要吸取教訓

#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;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	int q = len;
	while (--q) {
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
	}
	
	cout <<"++++"<< head->next->next->next->data<<endl;
	pEnd->next = NULL;
	delete pS;
	return;
}
void LinkList::LL_display() {
	cout << len << " ";
	ListNode *p;
	p = head->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
		cout << " &*(*& ";
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	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;
	}
};

class LinkList {
public:
	ListNode * head;
	int len;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	int q = len;
	while (--q) {
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
	}
	pS->next = NULL;
	pEnd = pS;
	delete pS;
	return;
}
void LinkList::LL_display() {
	cout << len << " ";
	int q = len;
	ListNode *p;
	p = head->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	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;
	}
};

class LinkList {
public:
	ListNode * head;
	int len;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	int q = len;
	while (--q) {
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
	}
	pS->next = NULL;
	pEnd = pS;
	delete pS;
	return;
}
void LinkList::LL_display() {
	cout << len << " ";
	int q = len;
	ListNode *p;
	p = head->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	return 0;
}



發現在輸入的時候最後一個沒有成功輸入資料

void LinkList::LL_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	int q = len;
	while (--q) {
		pEnd = pS;
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
	}
	pS->next = NULL;
	pEnd = pS;
	delete pS;
	cout << endl << pEnd->data << endl;
	return;
}

改了好久還是有問題,太煩了

#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;
	LinkList();
	~LinkList();
	ListNode*LL_index(int i);
	int LL_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	void LL_Create();
	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_Create() {
	ListNode*pS;
	ListNode*pEnd;
	pS = new ListNode;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	//delete pS;
	int q = len;
	while (--q) {
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
		pEnd = pS;
	}
	pEnd->next = NULL;
	cout << head->next->data<<endl;
	cout << head->next->next->data << endl;
	cout << head->next->next->next->data << endl;
	cout << pEnd->data;
	delete pS;
	return;
}
void LinkList::LL_display() {
	cout << len << " ";
	ListNode *p;
	p = head->next;
	while (p->next!=NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LinkList::LL_index(int i) {
	int j=1;
	for (ListNode*pG = head->next; pG->next,j<=i; pG = pG->next,j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LinkList::LL_get(int i) {
	ListNode*num = LL_index(i);
	return num->data;
	return error;
}

int LinkList::LL_insert(int i,int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i==1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return ok;
	}
	int j = 0;
	ListNode*pG = head->next;
	while (pG&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI->next = pG->next;
	pG->next = pI;
	pI->data = item;
	len++;
	return ok;
}
int LinkList::LL_del(int i) {
	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 main() {
	LinkList sa;
	sa.LL_Create();
	sa.LL_display();
	int i, item, k;

	cin >> i >> item;
	k=sa.LL_insert(i,item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	k = sa.LL_insert(i, item);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k=sa.LL_del(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	k=sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;
	cin >> i;
	k = sa.LL_get(i);
	if (k == 0)
		sa.LL_display();
	else
		cout << "error" << endl;

	return 0;
}