数据结构链表题目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;
}



搞到现在搞不还我真的有点难受

#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_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 = new ListNode;
	ListNode*pEnd;
	cin >> len;
	cin >> pS->data;
	head->next = pS;
	pEnd = pS;
	int q = len;
	//delete pS;
	while (--q) {
		pS = new ListNode;
		cin >> pS->data;
		pEnd->next = pS;
		pEnd = pS;
		//delete pS;
	}
	pEnd->next = NULL;
	delete pS;
	cout << head->next->data << endl;
	cout << head->next->next->data << endl;
	cout << head->next->next->next->data << endl;
	cout << pEnd->data;*/
	
	ListNode *temp = head;
	ListNode *ps;
	cin>>len;
	for(int i=0; i<len; i++){
	ps = new ListNode;
	cin>>ps->data;
	ps->next = temp->next;
	temp->next = ps;
	temp = temp->next;
	delete []ps;
	}
}
void LinkList::LL_display() {
	cout << len << " ";
	ListNode *p = head->next;
	while (p != 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;
}

做了一些修改,有很大的进步:

#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_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	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;
}
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;
		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 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;
	int num;
	cin >> num;
	int number;
	for (int i = 0; i < num; i++) {
		cin >> number;
		sa.LL_insert(i + 1, number);
		if (i == num-1)
			break;//什么情况?
	}
	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;

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

	return 0;
}

目前的输入、输出、插入都没有问题,但是删除的情况下对于超出链表范围的数据没有报错,并且影响下一步运行

终于我排除了所有问题,但是在for循环处还有一点奇怪

#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_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	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;
}
ListNode* LinkList::LL_index(int i) {
	if (i > len || i < 1) {
		return NULL;
	}
	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);
	if(num==NULL)
		return error;
	else	
		return num->data;
	
}

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;
		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 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 main() {
	LinkList sa;
	int num;
	cin >> num;
	int number;
	for (int i = 0; i < num; i++) {
		cin >> number;
		sa.LL_insert(i + 1, number);
		if (i == num-1)
			break;//什么情况?
	}
	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;

	cin >> i >> item;
	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 == -1)
		cout << "error" << endl;
	else
		cout << k << endl;
	cin >> i;
	k = sa.LL_get(i);
	if (k == -1)
		cout << "error" << endl;
	else
		cout << k << 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;
public:
	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_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;
}
ListNode* LinkList::LL_index(int i) {
	if (i > len || i < 1) {
		return NULL;
	}
	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);
	if(num==NULL)
		return error;
	else	
		return num->data;
	
}

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;
		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 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 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 i, item, k;

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

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

	return 0;
}

答案错误50%,有希望,还差一点点,加油。

终于提交成功!

特殊的数据没有考虑到,对错误输入范围的界定不正确,并且没考虑负数的情况

#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_get(int i);
	int LL_insert(int i, int item);
	int LL_del(int i);
	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;
}
ListNode* LinkList::LL_index(int i) {
	if (i > len || i < 1) {
		return NULL;
	}
	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) {
	if (i > len || i < 1) {
		return error;
	}
	ListNode*num = LL_index(i);
	if(num==NULL)
		return error;
	else	
		return num->data;
	
}

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 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 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 i, item, k;

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

	cin >> i >> item;
	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 == -1)
		cout << "error" << endl;
	else
		cout << k << endl;

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

	return 0;
}

好开心!不过这罚时也是没谁了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值