第一次写的有bug,下面已经将代码改正
-
在删除节点后,应该将
p
的next
指针指向被删除节点q
的下一个节点。这是因为在链表中删除一个节点后,应该使前一个节点指向被删除节点的下一个节点,而不是将前一个节点的数据域设置为被删除节点的数据域。 -
在删除节点后,应该将
q
的next
指针置为NULL
,以避免悬挂指针。 -
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;
}