当前位置:网站首页>线性结构:顺序表和链表
线性结构:顺序表和链表
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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
边栏推荐
- 利用go制作微信机器人
- The technology is very powerful, do you still need to "manage up"?
- M3SDA: Moment matching for multi-source domain adaptation
- 试写C语言三子棋
- [Node implements data encryption]
- 明解C语言第六章习题
- The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
- 推荐系统:AB测试(AB Test)
- 对int变量赋值的操作是原子的吗?
- Start foreground Activity
猜你喜欢

湖仓一体电商项目(四):项目数据种类与采集

MySQL数据库之JDBC编程

VBA runtime error '-2147217900 (80040e14): Automation error

推荐系统-排序层:排序层架构【用户、物品特征处理步骤】

These services can't ali interview?Then don't go to, the basic notification, etc

Database indexes: indexes are not a panacea

Common Expression Recognition Based on Face (1) - Basic Knowledge of Deep Learning

基于人脸的常见表情识别(1)——深度学习基础知识
![[PyTorchVideo Tutorial 01] Quickly implement video action recognition](/img/1a/696c5722bb94fabd688a8714ae2e8c.png)
[PyTorchVideo Tutorial 01] Quickly implement video action recognition

【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析
随机推荐
.eslintrc.js for musicApp
Linux下最新版MySQL 8.0的下载与安装(详细步骤)
MySQL eight-part text recitation version
mysql8 installation under linux
[Node implements data encryption]
PostgreSQL 14.4如何安装使用
How to install and use PostgreSQL 14.4
VBA runtime error '-2147217900 (80040e14): Automation error
并发与并行的区别
“数字化重构系统,搞定 CEO 是第一步”
How to build FTP server under win2003
ECCV2022 | 对比视觉Transformer的在线持续学习
Common Expression Recognition Based on Face (1) - Basic Knowledge of Deep Learning
Trial writing C language sanbang
Multi-threaded mutex application RAII mechanism
DCM 中间件家族迎来新成员
[c语言]二维数组动态分配内存
Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
win2003下FTP服务器如何搭建