静态链表
同时拥有数组和链表的缺点。
容量固定不变。
不能随机存取。
优点:增、删操作不需要大量移动元素。
![](https://uploadfiles.nowcoder.com/images/20220815/318889480_1660553763718/D9FDAE9918A39C99254A9D8D179628E5)
#include
using namespace std;
#define Maxsize 10
typedef struct {
int data;
int next;
}SLinkList;
bool InitSLinkList(SLinkList s[]) {
s[0].next = NULL;
for (int i = 1; i < Maxsize-1; i++) {
s[i].next = i + 1;
}
s[Maxsize - 1].next = -2;
return true;
}
bool InsertSLinkList(SLinkList *s,int e) {
if (s[1].next == -2) return false;
int p;
p = s[1].next;
s[p].data = e;
s[1].next = s[p].next;
if (s[0].next == NULL)
s[0].next = p;
s[p].next = NULL;
return true;
}
int main() {
SLinkList s[Maxsize];
InitSLinkList(s);
printf("%d\n", s[2].next);
InsertSLinkList(s, 3);
InsertSLinkList(s, 7);
printf("%d %d", s[3].data,s[0].next);
return 0;
}
容量固定不变。
不能随机存取。
优点:增、删操作不需要大量移动元素。
#include
using namespace std;
#define Maxsize 10
typedef struct {
int data;
int next;
}SLinkList;
bool InitSLinkList(SLinkList s[]) {
s[0].next = NULL;
for (int i = 1; i < Maxsize-1; i++) {
s[i].next = i + 1;
}
s[Maxsize - 1].next = -2;
return true;
}
bool InsertSLinkList(SLinkList *s,int e) {
if (s[1].next == -2) return false;
int p;
p = s[1].next;
s[p].data = e;
s[1].next = s[p].next;
if (s[0].next == NULL)
s[0].next = p;
s[p].next = NULL;
return true;
}
int main() {
SLinkList s[Maxsize];
InitSLinkList(s);
printf("%d\n", s[2].next);
InsertSLinkList(s, 3);
InsertSLinkList(s, 7);
printf("%d %d", s[3].data,s[0].next);
return 0;
}
全部评论
相关推荐
![](https://static.nowcoder.com/fe/file/oss/icon_job.png)
点赞 评论 收藏
分享
06-11 19:06
西安工业大学 算法工程师 点赞 评论 收藏
分享
点赞 评论 收藏
分享
![](https://static.nowcoder.com/fe/file/oss/icon_job.png)
点赞 评论 收藏
分享