数据结构-单链表的读取，插入与删除

```struct ListNode
{
int value;
ListNode *next;
};```

单链表读取

```#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;
}```

单链表插入

```#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);
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;
}```

单链表删除

```#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);
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;
}```

145 篇文章59 人订阅

0 条评论

相关文章

Leetcode 86 Partition List

Given a linked list and a value x, partition it such that all nodes less than x...

19760

9920

LeetCode 25. Reverse Nodes in k-Group分析代码

10820

Given a singly linked list, determine if it is a palindrome. Follow up: Could yo...

203100

You are given two non-empty linked lists representing two non-negative integers....

9710

7610

Leetcode 61 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative...

215100

LeetCode 2 & 455 Add Two Numbers I&II

You are given two non-empty linked lists representing two non-negative integers....

9200

9420