当前位置:网站首页>线性结构:顺序表和链表
线性结构:顺序表和链表
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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
边栏推荐
- Day31 LeetCode
- 湖仓一体电商项目(四):项目数据种类与采集
- Download Win11 how to change the default path?Download Win11 change the default path method
- 看完《二舅》,我更内耗了
- 阿里面试这些微服务还不会?那还是别去了,基本等通知
- Install MySQL tutorial under Linux
- MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- [hbuilder] cannot run some projects, open the terminal and cannot enter commands
- Mapped Statements collection does not contain value for的解决方法
猜你喜欢

推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】

MySQL数据库之JDBC编程

ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution

Cesium loads offline maps and offline terrain

如何优化OpenSumi终端性能?

TensorFlow2: Overview

MySQL函数(经典收藏)

Linux下安装MySQL教程

ECCV2022 | 对比视觉Transformer的在线持续学习

Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
随机推荐
推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】
MySQL eight-part text recitation version
Multi-threaded mutex application RAII mechanism
Recommendation system-model: FNN model (FM+MLP=FNN)
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
英文字母间隔突然增大(全角与半角转换)
[flink] Error finishing Could not instantiate the executor. Make sure a planner module is on the classpath
MySql密码
使用MULTISET来比较数据集的实例介绍
MySQL database - views and indexes
Typora设置标题自动标号
推荐系统-模型:FNN模型(FM+MLP=FNN)
linux下mysql8安装
Mapped Statements collection does not contain value for的解决方法
Niuke.com - Huawei Question Bank (100~108)
win2003下FTP服务器如何搭建
明解C语言第六章习题
MySQL八股文背诵版
HCIP --- 企业网的三层架构
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单