单链表带头结点(C/C++)

第一次写的有bug,下面已经将代码改正

  1. 在删除节点后,应该将 pnext 指针指向被删除节点 q 的下一个节点。这是因为在链表中删除一个节点后,应该使前一个节点指向被删除节点的下一个节点,而不是将前一个节点的数据域设置为被删除节点的数据域。

  2. 在删除节点后,应该将 qnext 指针置为 NULL,以避免悬挂指针。

  3. bool ListDelete(LinkList& L, int i, int& e)
    {
        if (i < 1)
        {
            return false;
        }
        LNode* p = GetElem(L, i - 1); // 获取前一个元素
        if (!p || !(p->next))
        {
            return false;
        }
        LNode* q = p->next; // 待删除节点
        e = q->data;
        p->next = q->next; // 修改前一个节点的指针
        free(q); // 释放被删除节点的内存
        return true;
    }
    

 改正前

#include<iostream>
#include<algorithm>

using namespace std;

typedef struct LNode {

	int data;
	struct LNode* next;
}LNode,*LinkList;
//初始化操作
bool InitList(LinkList& L)
{
	L = (LNode*)malloc(sizeof(LNode));//分配一个空的头节点。
	if (!L)
	{
		return false;
	}
	L->next = NULL;
	return true;

}
//头插法
LinkList List_HeadInsert(LinkList& L)
{
	int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	cout << "请输入数值:";
	cin >> x;
	LNode* s;
	while (x != 999)
	{
		s= (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		cin >> x;
	}

	return L;
}
//尾插法
LinkList List_TailInsert(LinkList& L)
{
	int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	LNode* s, * r;
	r = L;
	cin >> x;
	while (x != 999)
	{
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;

        
		cin >> x;
	}
	r->next = NULL;
	return L;


}
//按值查找,找到数据域为e的节点
LNode* LocateElem(LinkList L, int e)
{
	LNode* p = L -> next;
	while (p && p->data != e)
	{
		p = p->next;
	}
	return p;
}
//统计单链表的长度
int Length(LinkList L)
{
	int len = 0;
	LNode* p = L;
	while(p->next)
	{
		len++;
		p = p->next;
	}

	return len;
}
//按位查找,返回第i个元素(带头节点)
LNode* GetElem(LinkList L, int i)
{
	if (i < 0)
	{
		return NULL;

	}
	int j = 0;
	LNode* p = L;
	while (p && j < i)
	{
		p = p->next;
		j++;
	}
	return p;
}
//后插操作,在节点p之后插入元素e
bool InsertNextNode(LNode* p,int e)
{
	if (!p)
	{
		return false;
	}
	LNode* s = (LNode*)malloc(sizeof(LNode));
	if (!s)
	{
		return false;
	}
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}
//带头节点的插入操作,在第i个位置插入元素e
bool ListInsert(LinkList& L, int i, int e)
{
	if (i < 1)
	{
		return false;

	}
	LNode* p = GetElem(L, i - 1);//找到前一个元素
	if (!p)
	{
		return false;
	}
	return InsertNextNode(p, e);





}
//前插操作,在节点p之前插入节点s
bool InsertPriorNode(LNode* p, LNode* s)
{
	if (!p||!s)
	{
		return false;
	}
	s->next = p->next;
	p->next = s;
	swap(s->data, p->data);


}
//前插操作,在P节点前插入元素e
bool InsertPriorNodee(LNode* p, int e)
{
	if (!p)
	{
		return false;
	}
	LNode* s = (LNode*)malloc(sizeof(LNode));
	if (!s)
	{
		return false;
	}
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;


	return true;

}
//删除位序为i的节点,e是i节点的值
bool ListDelete(LinkList& L, int i, int& e)
{
	if (i < 1)
	{
		return false;
	}
	LNode* p= GetElem(L, i - 1);//获取删除前一个元素的值;
	if (!p || !(p->next))
	{
		return false;
	}
	LNode* q = p->next;
	e = q->data;
	p->data = q->data;
	free(q);
	return true;



}
//删除指定节点p
bool deleteNode(LNode* p)//把后驱的值赋给p,再让p跳过后驱,释放后驱。
{
	if (p->next == NULL)
	{
		return false;
	}
	LNode* q = p->next;
	p->data = q->data;
	p->next = q->next;
	free(q);
	return true;

}
//打印头结点单链表 
void print(LinkList L)
{
	LNode* s = L;
	while (s->next != NULL)
	{
		s = s->next;//一直向后指
		cout << s->data << " ";
	}
	cout << endl;


}
int main()
{
	LinkList L;
	InitList(L);//初始化
	List_HeadInsert(L);//头插法
	cout<<"打印当前链表";
	print(L);//打印链表
	cout << endl;
	cout << "链表的长度" << Length(L) << endl;
    cout << "链表的第二个元素" << GetElem(L, 2)->data << endl;
	int e;
	ListDelete(L, 3, e);
	cout << "删除的第三个元素是" << e;
	cout << endl;
	cout<<"打印当前链表"<<endl;
	print(L);
	ListInsert(L,3,e);
	cout<<"当前插入的第3个元素是"<<e<<endl;
	cout<<"打印当前链表"<<endl;
	print(L);
	LNode* s = LocateElem(L, 5);//按值查找数据域等于e的节点,
		return 0;
}

改正后

#include<iostream>
#include<algorithm>

using namespace std;

typedef struct LNode {

	int data;
	struct LNode* next;
}LNode,*LinkList;
//初始化操作
bool InitList(LinkList& L)
{
	L = (LNode*)malloc(sizeof(LNode));//分配一个空的头节点。
	if (!L)
	{
		return false;
	}
	L->next = NULL;
	return true;

}
//头插法
LinkList List_HeadInsert(LinkList& L)
{
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	cout << "请输入数值:";
	LNode* s;
	int x;
	while (cin>>x)
	{
		s= (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
	}

	return L;
}
//尾插法
LinkList List_TailInsert(LinkList& L)
{
	int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	LNode* s, * r;
	r = L;
	cin >> x;
	while (x != 999)
	{
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;

        
		cin >> x;
	}
	r->next = NULL;
	return L;


}
//按值查找,找到数据域为e的节点
LNode* LocateElem(LinkList L, int e)
{
	LNode* p = L -> next;
	while (p && p->data != e)
	{
		p = p->next;
	}
	return p;
}
//统计单链表的长度
int Length(LinkList L)
{
	int len = 0;
	LNode* p = L;
	while(p->next)
	{
		len++;
		p = p->next;
	}

	return len;
}
//按位查找,返回第i个元素(带头节点)
LNode* GetElem(LinkList L, int i)
{
	if (i < 0)
	{
		return NULL;

	}
	int j = 0;
	LNode* p = L;
	while (p && j < i)
	{
		p = p->next;
		j++;
	}
	return p;
}
//后插操作,在节点p之后插入元素e
bool InsertNextNode(LNode* p,int e)
{
	if (!p)
	{
		return false;
	}
	LNode* s = (LNode*)malloc(sizeof(LNode));
	if (!s)
	{
		return false;
	}
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}
//带头节点的插入操作,在第i个位置插入元素e
bool ListInsert(LinkList& L, int i, int e)
{
	if (i < 1)
	{
		return false;

	}
	LNode* p = GetElem(L, i - 1);//找到前一个元素
	if (!p)
	{
		return false;
	}
	return InsertNextNode(p, e);





}
//前插操作,在节点p之前插入节点s
bool InsertPriorNode(LNode* p, LNode* s)
{
	if (!p||!s)
	{
		return false;
	}
	s->next = p->next;
	p->next = s;
	swap(s->data, p->data);


}
//前插操作,在P节点前插入元素e
bool InsertPriorNodee(LNode* p, int e)
{
	if (!p)
	{
		return false;
	}
	LNode* s = (LNode*)malloc(sizeof(LNode));
	if (!s)
	{
		return false;
	}
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;


	return true;

}
//删除位序为i的节点,e是i节点的值
bool ListDelete(LinkList& L, int i, int& e)
{
	if (i < 1)
	{
		return false;
	}
	LNode* p = GetElem(L, i - 1); // 获取前一个元素
	if (!p || !(p->next))
	{
		return false;
	}
	LNode* q = p->next; // 待删除节点
	e = q->data;
	p->next = q->next; // 修改前一个节点的指针
	free(q); // 释放被删除节点的内存
	return true;
}

//删除指定节点p
bool deleteNode(LNode* p)//把后驱的值赋给p,再让p跳过后驱,释放后驱。
{
	if (p->next == NULL)
	{
		return false;
	}
	LNode* q = p->next;
	p->data = q->data;
	p->next = q->next;
	free(q);
	return true;

}
//打印头结点单链表 
void print(LinkList L)
{
	LNode* s = L;
	while (s->next != NULL)
	{
		s = s->next;//一直向后指
		cout << s->data << " ";
	}
	cout << endl;


}
int main()
{
	LinkList L;
	//InitList(L);//初始化
	List_HeadInsert(L);//头插法
	print(L);//打印链表
	cout << endl;
	cout << "链表的长度" << Length(L) << endl;
	cout << "链表的第二个元素" << GetElem(L, 2)->data << endl;
	int e;
	ListDelete(L, 3, e);
	cout << "删除的第三个元素是" << e;
	cout << endl;
	cout << "打印当前链表";
	print(L);
	cout << endl;
	LNode* s = LocateElem(L, 5);//按值查找数据域等于e的节点,
		return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值