当前位置:网站首页>线性结构:顺序表和链表
线性结构:顺序表和链表
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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
边栏推荐
- How to install and use PostgreSQL 14.4
- Download Win11 how to change the default path?Download Win11 change the default path method
- ELK日志分析系统
- Niuke.com - Huawei Question Bank (100~108)
- MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
- MySQL mass production of data
- Database Tuning - Database Tuning
- 用jOOQ 3.17投射类型安全的嵌套表记录
- JUnit 5测试中的临时目录(附实例及代码)
- 推荐系统-模型:FNN模型(FM+MLP=FNN)
猜你喜欢
Encapsulates a console file selector based on inquirer
普通的int main(){}没有写return 0;会怎么样?
MySQL mass production of data
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
Install MySQL tutorial under Linux
Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
MySQL数据库————视图和索引
技术很牛逼,还需要“向上管理”吗?
倾斜文档扫描与字符识别(opencv,坐标变换分析)
随机推荐
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
银行数据资产转换能力弱?思迈特软件助力解决银行困境
Linux下最新版MySQL 8.0的下载与安装(详细步骤)
MySQL Functions (Classic Collection)
【PyTorchVideo教程01】快速实现视频动作识别
Zabbix 5.0 监控教程(一)
MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
Ordinary int main(){} does not write return 0; what will happen?
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
推荐系统:AB测试(AB Test)
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
Start background services across processes
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
JUnit 5测试中的临时目录(附实例及代码)
[Ask] SQL statement to calculate the sum of column 2 by deduplicating column 1?
对int变量赋值的操作是原子的吗?
推荐系统-模型:FNN模型(FM+MLP=FNN)
[hbuilder] cannot run some projects, open the terminal and cannot enter commands
Day31 LeetCode