单链表的基本操作

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

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

#include<iostream>
#include<cstdio>
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&&j<i){
		j++;
		current = current->next;
	}
	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 && j<i){
		j++;
		previous = current;
		current = current->next;
	}
	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&&j<i){
		j++;
		current = current->next;
	}
	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&&j<i){
		j++;
		previous = current;
		current = current->next;
	}
	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;
}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火浣熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值