单链表的基本操作

       主要是链表的增删改查的基本功能,做了一个简单的集锦。

       大一学的链表,大二的我回来总结一哈,弥补过去不努力的自己。即使是现在写,自己也忘记了不少,代码调试也花了很多时间,所以建议各位程序猿多多练习,不要学着后面,忘了前面。

#include
#include
using namespace std;
class NODE
{
public:
	int data;
	NODE *next;
};
class LIST
{
private:
	NODE *head;
public:
	LIST()
	{
		head = new NODE;
		head->next = NULL;
	}
	int length();
	bool isempty()
	{
		return head->data == NULL ? true : false;
	}
	bool get_data(int i, int &x);//获得链表指定位置i的值
	bool get_succ(int i, int &x);////定义取后继元素函数
	bool getprior(int i, int &x);
	bool replace_data(int data, int i);
	bool insert_data(int data, int i);//插入数据
	bool delete_data(int i);
	bool find_data(int x, int &result);
	void print_list();
	~LIST()
	{
		NODE *p;
		while (head)
		{
			p = head;
			head = head->next;
			delete p;
		}
	}
};

int LIST::length()
{
	int counter = 0;
	NODE *current;
	current = head->next;
	while (current)
	{
		counter++;
		current = current->next;
	}
	return counter;
}

bool LIST::get_data(int i, int &x)
{
	NODE *current;
	int j = 1;
	if ((i < 1) || (i > length()))
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	current = head->next;
	while (current != NULL&&j < i)
	{
		j++;
		current = current->next;
	}
	x = current->data;
	return true;
}

bool LIST::get_succ(int i, int&x)
{
	NODE * current;
	int j = 1;
	if ((i < 1) && (i > length()))
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	current = head->next;
	while (current->next != NULL&&jnext;
	}
	if (current->next != NULL)
	{
		x = current->next->data;
		return true;
	}
	else
	{
		cout << "第" << i << "个元素无后继!" << endl;
		return false;
	}
}

bool LIST::getprior(int i, int &x)
{
	NODE *current, *previous;
	int j = 1;
	if (i<1 && i>length())
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	previous = head;//previous作为指向第i-1个节点的指针
	current = head->next;
	while (current != NULL && jnext;
	}
	if (previous != head)
	{
		x = previous->data;
		return true;
	}
	else
	{
		cout << "第" << i << "个元素无前驱,不能读取!" << endl;
		return false;
	}
}

bool LIST::replace_data(int data, int i)
{
	NODE *current = head;
	int j = 1;
	if (i<1 || i>length())
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	current = head->next;
	while (current != NULL&&jnext;
	}
	current->data = data;
	return true;
}

bool LIST::insert_data(int data, int i)
{
	NODE *current, *previous, *newnode;
	if (i<1 || i>length() + 1)
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	newnode = new NODE;
	if (newnode == NULL)
	{
		cout << "空间已满,申请失败!" << endl;
		return false;
	}
	int j = 1;
	newnode->data = data;
	newnode->next = NULL;
	previous = head;
	current = head->next;
	while (current != NULL&&jnext;
	}
	newnode->next = current;
	previous->next = newnode;
	return true;
}

bool LIST::delete_data(int i)
{
	NODE *current, *previous;
	if (isempty())
	{
		cout << "表已空,不能删除!" << endl;
		return false;
	}
	if (i<1 || i>length())
	{
		cout << "读取位置错误,请重新检查!" << endl;
		return false;
	}
	int j = 1;
	previous = current = NULL;
	current = head->next;

	while (current != NULL&&j < i)
	{
		j++;
		previous = current;
		current = current->next;
	}
	if (current->next != NULL)
	{
		previous->next = current->next;
		free(current);
		return true;
	}
	else
	{
		previous->next = NULL;
		return true;
	}

}

bool LIST::find_data(int x, int &result)
{
	int i = 1;
	NODE * current;
	current = head->next;
	while (current != NULL)
	{
		if (current->data == x)
		{
			result = i;
			return true;
		}
	}
	cout << "链表中没有" << x << "这个值!" << endl;
	return false;
}

void LIST::print_list()
{
	NODE *current;
	current = head->next;
	cout << "链表中的全部元素:" << endl;
	while (current != NULL)
	{
		cout << current->data << " ";
		current = current->next;
	}
	cout << endl;
}
int main()
{
	int i;
	LIST non_seqlist1;
	for (i = 1; i <= 5; i++)
	{
		non_seqlist1.insert_data(i * 100, i);
	}
	non_seqlist1.print_list();
	non_seqlist1.insert_data(404, 4);
	non_seqlist1.print_list();
	non_seqlist1.insert_data(809, -4);
	non_seqlist1.print_list();
	non_seqlist1.delete_data(8);
	non_seqlist1.print_list();
	non_seqlist1.delete_data(3);
	non_seqlist1.print_list();
	non_seqlist1.delete_data(5);
	non_seqlist1.print_list();
	int i_data = -1;
	non_seqlist1.replace_data(0, i_data);
	non_seqlist1.replace_data(5, i_data);
	non_seqlist1.print_list();
	non_seqlist1.replace_data(1, i_data);
	non_seqlist1.replace_data(4, i_data);
	non_seqlist1.print_list();
	LIST non_seqlist2;
	for (int i = 1; i < 5; i++)
	{
		non_seqlist2.insert_data(i * 100, 1);
	}
	non_seqlist2.print_list();
	int f_data = -1;
	for (int i = 0; i <= 6; i++)
	{
		if (non_seqlist2.getprior(i, f_data))
			cout << f_data << " ";
	}
	f_data = -1;
	for (i = 0; i < +6; i++)
	{
		if (non_seqlist2.get_succ(i, f_data))
		{
			cout << f_data << " ";
		}
	}
	return 0;
}

代码就到这里,不懂的地方可以留言给我。

你可能感兴趣的:(C++程序设计)