数据结构-单链表的读取,插入与删除
2017-08-01 17:46
393 查看
链表定义:
需要注意的是,如果i大于链表的长度的话程序会异常,所以需要加上判断。
在上面的代码中,传入GetElem函数的是链表的头结点,这个代码和《大话数据结构》这本书里面的例程有些不同,如果有人也碰巧看那本书的话,可以留意一下。
这里的1-6只是结点里面存的数据,不决定结点的顺序。
(1)找到链表中第三个结点p
(2)创建一个新的结点q
(3)q先与原链表第四个结点链接
(4)原链表第三个结点与q链接
(3和4的顺序不能对调,所以图示就是这样)
我们假设一下,如果3,4对调了,也就是说先让第三个结点指向q,因为我们是可以在原链表上找到第三个结点的,但是一旦指向发生了变化,原链表断了,原链表的第四个结点就已经找不到了。但是不对调的话,在原链表没有断开之前就把q与第四个结点连起来了,又由于本来我们就找到了原链表的第三个结点,所以即便断开了,那又能怎么样呢??
步骤是这样:
(1)找到第三个结点p
(2)找到p后面的结点q(在图中是第四个)
(3)将p指向q后面的结点(在图中是第五个)
(4)释放q
struct ListNode { int value; ListNode *next; };
单链表读取
在顺序存储结构中,比如数组中,想要获取某一个位置的数据是非常容易的一件事,但是在链表中却要麻烦一些,因为链表的存储单元并不是连续的,而且我们只知道链表的头结点,也就是想知道第i个位置的数据,只能从头找下去,并没有什么其他的好方法。需要注意的是,如果i大于链表的长度的话程序会异常,所以需要加上判断。
#include<iostream>
using namespace std;
struct ListNode { int value; ListNode *next; };
ListNode* GetElem(ListNode * L,int i);
void CreateList(ListNode * L,int n);
int main()
{
ListNode * L = new ListNode;
L->next = nullptr;
CreateList(L,5);
ListNode* elem = GetElem(L,3);
if (elem==nullptr)
{
cout<<"error"<<endl;
}
else
{
cout<<elem->value<<endl;
}
getchar();
getchar();
return 0;
}
void CreateList(ListNode * L,int n)
{
cin>>L->value;//输入第一个结点的数据值
n--;
for (int i = 0; i < n; i++)
{
ListNode * p = new ListNode;
cin>>p->value;
p->next = nullptr;
L->next = p;
L = p;
}
}
ListNode* GetElem(ListNode * L,int i)
{
int j;
ListNode *p=L;
j =1;
while (p && j<i)
{
p = p->next;
j++;
}
if (!p || j>i)
{
return nullptr;
}
return p;
}
在上面的代码中,传入GetElem函数的是链表的头结点,这个代码和《大话数据结构》这本书里面的例程有些不同,如果有人也碰巧看那本书的话,可以留意一下。
单链表插入
相比于顺序存储结构,链表的读取确实麻烦了些,但是好在插入和删除方便。比如要在链表的第三个结点之后插入一个结点。这里的1-6只是结点里面存的数据,不决定结点的顺序。
(1)找到链表中第三个结点p
(2)创建一个新的结点q
ListNode *q=(ListNode*)malloc(sizeof(ListNode));q->value=4;
(3)q先与原链表第四个结点链接
q->next = p->next;
(4)原链表第三个结点与q链接
p->next = q;
(3和4的顺序不能对调,所以图示就是这样)
我们假设一下,如果3,4对调了,也就是说先让第三个结点指向q,因为我们是可以在原链表上找到第三个结点的,但是一旦指向发生了变化,原链表断了,原链表的第四个结点就已经找不到了。但是不对调的话,在原链表没有断开之前就把q与第四个结点连起来了,又由于本来我们就找到了原链表的第三个结点,所以即便断开了,那又能怎么样呢??
#include<iostream>
using namespace std;
struct ListNode { int value; ListNode *next; };
void showList(ListNode * L);
ListNode* ListInsert(ListNode * L,int i);
void CreateList(ListNode * L,int n);
int main()
{
ListNode * L = new ListNode;
L->next = nullptr;
CreateList(L,5);
ListNode* Head = ListInsert(L,3);
showList(Head);
getchar();
getchar();
return 0;
}
void CreateList(ListNode * L,int n)
{
cin>>L->value;//输入第一个结点的数据值
n--;
for (int i = 0; i < n; i++)
{
ListNode * p = new ListNode;
cin>>p->value;
p->next = nullptr;
L->next = p;
L = p;
}
}
ListNode* ListInsert(ListNode * L,int i)
{
int j;
ListNode *p=L;
j =1;
while (p && j<i)
{
p = p->next;
j++;
}
if (!p || j>i)
{
return nullptr;
}
ListNode *q=(ListNode*)malloc(sizeof(ListNode));
q->value=4;
//3
q->next = p->next;
//4
p->next = q;
return L;
}
void showList(ListNode * L)
{
ListNode * p = L;
while (p)
{
cout<<p->value<<' ';
p = p->next;
}
cout<<endl;
}
单链表删除
要删除一个链表中第三个结点后面的结点,逻辑与插入操作很类似,同样要考虑原链表断开后的情况:步骤是这样:
(1)找到第三个结点p
(2)找到p后面的结点q(在图中是第四个)
q= p->next;
(3)将p指向q后面的结点(在图中是第五个)
p->next = q->next;
(4)释放q
free(q);
#include<iostream>
using namespace std;
struct ListNode { int value; ListNode *next; };
void showList(ListNode * L);
ListNode* ListDelete(ListNode * L,int i);
void CreateList(ListNode * L,int n);
int main()
{
ListNode * L = new ListNode;
L->next = nullptr;
CreateList(L,5);
ListNode* Head = ListDelete(L,3);
showList(Head);
getchar();
getchar();
return 0;
}
void CreateList(ListNode * L,int n)
{
cin>>L->value;//输入第一个结点的数据值
n--;
for (int i = 0; i < n; i++)
{
ListNode * p = new ListNode;
cin>>p->value;
p->next = nullptr;
L->next = p;
L = p;
}
}
ListNode* ListDelete(ListNode * L,int i)
{
int j;
ListNode *q;
ListNode *p=L;
j =1;
while (p && j<i)
{
p = p->next;
j++;
}
if (!p || j>i)
{
return nullptr;
}
//3
q= p->next;
//4
p->next = q->next;
free(q);
return L;
}
void showList(ListNode * L)
{
ListNode * p = L;
while (p)
{
cout<<p->value<<' ';
p = p->next;
}
cout<<endl;
}
相关文章推荐
- 数据结构:程序加图示分析单链表的插入和删除操作
- 数据结构(三)——单链表的插入与删除
- 数据结构——单链表的创建、删除、遍历以及节点的插入、删除等操作
- C语言之数据结构的顺序单链表创建,插入和删除操作
- 数据结构:头插法、尾插法创建单链表,求链表长度、查找、插入、删除、合并
- 【数据结构】单链表的建立,测长,打印,删除,插入, 排序, 逆置
- 【数据结构】单链表(一)单链表的定义,插入,删除和查找操作
- 【数据结构】单链表的插入与删除
- 数据结构之线性表的顺序结构操作2-(删除,插入,查值等)
- 结构之美:删除单链表指定位置的数据
- 数据结构——单链表的创建、逆置、插入、有序表的建立、有序单链表合并等基础操作!!
- 【程序员面试宝典】数据结构基础一单链表:创建|求长|插入|删除|排序|打印|逆置
- 数据结构——链表之单链表重复元素的删除
- 单链表的数据插入删除查询
- 程序员面试宝典之数据结构基础----⑥双链表的建立,插入和删除
- 实战数据结构(2)_两个单链表间的删除操作
- 【数据结构】顺序线性表的插入、删除、合并实现
- 删去单链表中第i个节点的操作。删除单链表中数据域为x的前驱元素。数据结构。
- 数据结构——二叉树的插入构造和删除操作
- 数据结构——2 单链表插入和删除节点