【C/C++】链表
2024-09-06 08:36:40
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data; // 数据
node* next; // 指针
};
node* create(int Array[])
{
node *p, *pre, *head; // head头节点, pre当前节点的前驱节点,p当前节点
head = new node;
head->next = NULL;
pre = head;
for (int i = 0; i < 5; i++)
{
// 创建新节点
p = new node;
// 将Array[i]赋给新建的节点作为数据域,也可以scanf输入
p->data = Array[i];
p->next = NULL;
pre->next = p;
// 更新
pre = p;
}
return head;
}
int search(node* head, int x)
{
int count = 0;
node* p = head->next;
while (p != NULL)
{
if(p->data == x)
{
count++;
}
p = p->next;
}
return count;
}
void insert(node* head, int pos, int x)
{
node* p = head; // 保护头指针
// 找到位置
for(int i = 0; i < pos - 1; i++)
{
p = p->next; // pos - 1是为了找到位置的前一个节点
}
node* q = new node;
q->data = x;
q->next = p->next;
p->next = q;
}
void del(node* head, int x)
{
node* p = head->next;
node* pre = head;
while(p != NULL)
{
if(p->data == x)
{
pre->next = p->next;
delete(p);
}
else
{
pre = p;
p = p->next;
}
}
}
int main()
{
int Array[5] = {5, 3, 6, 1, 2};
node* L = create(Array);
L = L->next;
while(L != NULL)
{
printf("%d", L->data);
L = L->next;
}
system("pause");
}
最新文章
- Web系统大规模并发——电商秒杀与抢购
- MVC -- 后台RedirectToAction传递实体类与字符串
- 【nodejs笔记——小知识点汇总】
- FreeBSD从零开始---安装后配置(三)
- 通过ksoap2-android来调用Web Service操作的实例
- ubuntu搭建svn服务器(转)
- SQL基础篇---函数及其函数配套使用的关键字
- live555
- BZOJ_1625_ [Usaco2007_Dec]_宝石手镯_(01背包)
- MySql数据库的基本原理及指令
- MySQL复制表-SELECT INTO FROM
- vtime.hpp
- 使用fastreport以代码方式创建报表
- selenium之关于 chromedriver的安装和使用
- nginx mp3
- 通过cookie验证用户登录
- java 判断上传文件大小
- 开源日志收集Exceptionless简单使用
- Java 正则表达式的实际应用
- 自己平时收集的css、html笔记(适合初级前端攻城狮)
热门文章
- logstash的filter之grok
- [noi1755]Trie
- 听说你想把对象存储当 HDFS 用,我们这里有个方案...
- 入门JavaScript正则表达式
- 8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化
- iNeuOS工业互联网操作系统,分布式云端控制安全策略和增加实时日志功能
- [NOI Online #3 提高组] 魔法值
- Congratulations, FYMS-OIers!
- LeeCode刷题笔记
- Mysql优化,ICP、BNL算法、BKA算法、MMR算法