当前位置:网站首页>线性结构:顺序表和链表
线性结构:顺序表和链表
2022-07-30 19:57:00 【@布响丸辣】
一、顺序表
#include <stdio.h>
#include <stdlib.h>
typedef struct TABLE
{
int* pHead;
int length;
int size;
}Table;
#define LENGTH 5
Table InitTable()
{
Table t;
t.pHead = (int*)malloc(sizeof(int) * LENGTH);
if (!t.pHead)
{
printf("初始化失败\n");
exit(0);
}
t.length = LENGTH;
t.size = 0;
return t;
}
//遍历
void DisplayTable(Table t)
{
if (t.pHead == NULL || t.length == 0)
{
return 0;
}
for (int i = 0; i < t.size; i++)
{
printf("%d ", t.pHead[i]);
}
printf("\n");
}
//插入
void InsertTable(Table* pT, int value, int n)
{
//判断插入位置
if (!pT || !pT->pHead)
{
return;
}
if (n-1 > pT->size || n < 1)
{
return;
}
//判断空间是否够用
if (pT->size == pT->length)
{
//不够
pT->pHead = (int*)realloc(pT->pHead, sizeof(int) * (pT->length + 1));
pT->length++;
if (pT->pHead == NULL)
{
printf("扩容失败\n");
exit(0);
}
}
//插入
for (int i = pT->size; i >= n; i--)
{
pT->pHead[i] = pT->pHead[i - 1];
}
pT->pHead[n - 1] = value;
pT->size++;
}
//删除
void DeleteTable(Table* pT, int n)
{
if (n > pT->size || n < 1)
{
printf("删除位置有误\n");
return;
}
for (int i = n - 1; i < pT->size - 1; i++)
{
pT->pHead[i] = pT->pHead[i + 1];
}
pT->size--;
}
int main()
{
Table t = InitTable();
for (int i = 0; i < 5; i++)
{
InsertTable(&t, i + 10, i + 1);
}
DisplayTable(t);
InsertTable(&t, 15, 6);
DisplayTable(t);
InsertTable(&t, 5, 3);
DisplayTable(t);
DeleteTable(&t, 5);
DisplayTable(t);
return 0;
}
输出:
10 11 12 13 14
10 11 12 13 14 15
10 11 5 12 13 14 15
10 11 5 12 14 15
D:\数据结构\线性表\顺序表和链表\x64\Debug\顺序表和链表.exe (进程 20564)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
二、链表
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int value;
struct NODE* pNext;
}Node;
typedef struct LIST
{
Node* pHead;
Node* pEnd;
int size;
}List;
List InitList()
{
List lst;
lst.pHead = NULL;
lst.pEnd = NULL;
lst.size = 0;
return lst;
}
void AddNode(List* plst,int value)
{
if (plst == NULL)
{
printf("链表不存在\n");
return;
}
Node* pTemp = (Node*)malloc(sizeof(Node));
pTemp->value = value;
pTemp->pNext = NULL;
if (plst->pHead == NULL)
{
plst->pHead = pTemp;
}
else
{
(plst->pEnd)->pNext = pTemp;
}
plst->pEnd = pTemp;
plst->size++;
}
void ShowList(List lst)
{
while (lst.pHead)
{
printf("%d ", lst.pHead->value);
lst.pHead = lst.pHead->pNext;
}
printf("\n");
}
void ReversalShowList(Node* pHead)
{
if (!pHead)
{
return;
}
ReversalShowList(pHead->pNext);
printf("%d ", pHead->value);
}
void Reverse(List* plst)
{
if (plst->pHead == NULL || plst->pHead->pNext == NULL)
{
return;
}
Node* pBegin = NULL;
Node* pMid = plst->pHead;
Node* pEnd = plst->pHead->pNext;
plst->pHead = plst->pEnd;
plst->pEnd = pMid;
while (pEnd != NULL)
{
pMid->pNext = pBegin;
pBegin = pMid;
pMid = pEnd;
pEnd = pEnd->pNext;
}
pMid->pNext = pBegin;
}
void Reverse1(List* plst)
{
if (plst == NULL || plst->pHead == NULL || plst->pHead->pNext == NULL)
{
return;
}
Node* pNewHead = NULL;
plst->pEnd = plst->pHead;
while (plst->pHead != NULL)
{
Node* pTemp = plst->pHead;
plst->pHead = plst->pHead->pNext;
pTemp->pNext = pNewHead;
pNewHead = pTemp;
}
plst->pHead = pNewHead;
}
int main()
{
List lst = InitList();
AddNode(&lst, 1);
AddNode(&lst, 2);
AddNode(&lst, 3);
AddNode(&lst, 4);
ShowList(lst);
//ReversalShowList(lst.pHead);
Reverse(&lst);
ShowList(lst);
Reverse1(&lst);
ShowList(lst);
return 0;
}
输出:
1 2 3 4
4 3 2 1
1 2 3 4
D:\数据结构\线性表\顺序表和链表\x64\Debug\顺序表和链表.exe (进程 18412)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
边栏推荐
猜你喜欢

多线程的互斥锁应用RAII机制

网络层协议------IP协议

并发与并行的区别

The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
![[Node implements data encryption]](/img/8b/f9ff44c39fc9e80b2f2d2626a74076.png)
[Node implements data encryption]

Cesium加载离线地图和离线地形

4年测试经验去面试10分钟就被赶出来了,面试官说我还不如应届生?都这么卷吗...

Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接

阿里面试这些微服务还不会?那还是别去了,基本等通知

MySQL database - views and indexes
随机推荐
湖仓一体电商项目(四):项目数据种类与采集
TensorFlow2: Overview
These services can't ali interview?Then don't go to, the basic notification, etc
技术很牛逼,还需要“向上管理”吗?
明解C语言第五章习题
ELK日志分析系统
Mac安装PHP开发环境
基于人脸的常见表情识别——模型搭建、训练与测试
The technology is very powerful, do you still need to "manage up"?
Linux下载安装mysql5.7版本教程最全详解
HarmonyOS笔记-----------(三)
MySql密码
推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
基于人脸的常见表情识别(2)——数据获取与整理
历史上的今天:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
推荐系统:开源项目/工具【谷歌:TensorFlow Recommenders】【Facebook:TorchRec】【百度:Graph4Rec】【阿里:DeepRec和EasyRec】
Is the iPhone really thirteen incense?The two generations of products are completely compared, perhaps the previous generation is more worth buying
Niuke.com - Huawei Question Bank (100~108)
360杜跃进:太空安全风险加剧,需打造一体化防御体系
Redisson 的分布式锁找不到?