当前位置:网站首页>C language linear list -- static linked list
C language linear list -- static linked list
2022-06-09 13:02:00 【Autumn dusk in Mountain Residence LS】
1.3 The linear table -- Static list
main.cpp
//
// main.cpp
// 3_StaticList
//
// Created by LS on 2022/5/16.
// Copyright 2022 LSC001. All rights reserved.
//
#include <iostream>
#include "staticlist.hpp"
using namespace std;
int main() {
StaticList SL;
InitSList(SL);
for(int i=0;i<5;++i)
{
Insert(SL,'A'+i);
}
showSList(SL);
Delete(SL); // head delete
showSList(SL);
cout << "Hello, World!\n";
return 0;
}
staticlist.hpp
//
// staticlist.hpp
// 3_StaticList
//
// Created by LS on 2022/5/16.
// Copyright 2022 LSC001. All rights reserved.
//
#ifndef staticlist_hpp
#define staticlist_hpp
#include <stdio.h>
#define ElemType char
#define MAX_SIZE 20
typedef struct ListNode
{
ElemType data;
int cur;
}ListNode;
typedef ListNode StaticList[MAX_SIZE];
int Malloc_SL(StaticList &space);
void Free_SL(StaticList &space,int k);
void InitSList(StaticList &space);
void Insert(StaticList &space,ElemType x);
void Delete(StaticList &space);
void showSList(StaticList space);
#endif /* staticlist_hpp */
/* staticlist_hpp
Static linked list is described by array ( simulation ) The linked list of .
*/
staticlist.hpp
//
// staticlist.hpp
// 3_StaticList
//
// Created by LS on 2022/5/16.
// Copyright 2022 LSC001. All rights reserved.
//
#ifndef staticlist_hpp
#define staticlist_hpp
#include <stdio.h>
#define ElemType char
#define MAX_SIZE 20
typedef struct ListNode
{
ElemType data;
int cur;
}ListNode;
typedef ListNode StaticList[MAX_SIZE];
int Malloc_SL(StaticList &space);
void Free_SL(StaticList &space,int k);
void InitSList(StaticList &space);
void Insert(StaticList &space,ElemType x);
void Delete(StaticList &space);
void showSList(StaticList space);
#endif /* staticlist_hpp */
/* staticlist_hpp
Static linked list is described by array ( simulation ) The linked list of .
*/
staticlist.cpp
//
// staticlist.cpp
// 3_StaticList
//
// Created by LS on 2022/5/16.
// Copyright 2022 LSC001. All rights reserved.
//
#include "staticlist.hpp"
void InitSList(StaticList &space)
{
space[0].cur = -1; // head
for(int i=1;i<MAX_SIZE-1;++i) // pool
{
space[i].cur = i+1;
}
space[MAX_SIZE-1].cur = 0; // tail
}
int Malloc_SL(StaticList &space)
{ // from pool To apply for space
int i = space[1].cur;
if(space[1].cur !=0)
space[1].cur = space[i].cur;
return i;
}
void Free_SL(StaticList &space,int k)
{
space[k].cur = space[1].cur;
space[1].cur = k;
}
void Insert(StaticList &space,ElemType x)
{
int i = Malloc_SL(space);
if(i==0)
{
printf("NO SPACE!");
return;
}
space[i].data=x;
if(space[0].cur==-1) // wether is head
{
space[i].cur=-1;
//space[0].cur= i;
}else // head insert
{
space[i].cur=space[0].cur;
//space[0].cur=i;
}
space[0].cur=i;
}
void Delete(StaticList &space)
{
int i = space[0].cur; // The node to be deleted
space[0].cur = space[i].cur;
Free_SL(space,i);
// space[i].cur = space[1].cur;
// space[1].cur = i;
}
void showSList(StaticList space)
{
int i=space[0].cur;
while (i!= -1) {
printf("%c-->",space[i].data);
i = space[i].cur;
}
printf("NULL\n");
}
/*
0 head -1
1 pool 2
2 3
3 4
. .
. .
. .
12 13
13 0
*/
边栏推荐
- Qpprocess class Usage Summary
- [interpretation of redis underlying mechanism: Linux operating system file descriptor FD]
- 流媒体压测st-load
- Image matrix transformation in gdi+
- GDI+ 中图片的绘制
- Keil5mdk installation (free)
- QProcess 类使用总结
- navicat使用说明
- Gaussdb (for redis) new feature release: prefix search thousand fold promotion and cluster multi tenancy isolation
- CK、ES、RediSearch 对比,谁的性能更胜一筹
猜你喜欢
随机推荐
Warning: Accessing non-existent property ‘column‘ of module exports inside circular depen
Compress uploaded pictures with JS
Gaussdb (for redis) new feature release: prefix search thousand fold promotion and cluster multi tenancy isolation
LeetCode-732. 我的日程安排表 III,差分数组
数据库的安装--mysql
Taoist friend, what is the relationship between your use of redis in the project and the three Virgin Mary?
Basic image transformation in gdi+
Zabbix6.0 new feature GEOMAP map marker can you use it?
[redis advanced]
[new] control move + drag size + animation drag in WPF window
这个MySQL数据库多表查询的第三问怎么做呀?
[NOIP2015 提高组] 运输计划
Stack based buffer overflow for secure encoding
Image matrix transformation in gdi+
cache 跟 TLB
数据库day-3
Redisssion use
Basic knowledge of modal analysis that engineers should know
CK、ES、RediSearch 对比,谁的性能更胜一筹
【 日志系统 】







![[STM32] Hal library ADC](/img/a3/10f1e60ccf4001c5a9c3e2a9121019.png)