#pragma once
//静态链表,利用顺序表模拟链表
//有效数据链的头为0下标,带头的循环链表
//空闲数据连的头为1下标,带头的循环链表
//
#include <stdbool.h>
#define SIZE 10
typedef struct SNode
{
int data;
int next; //下一个节点的地址
}SNode, *PList; //PList == SNode *
typedef SNode SList[SIZE];
//初始化
void InitSList(PList pl);
//插入
bool Insert(PList pl, int val);
//查找
int Search(PList pl, int val);
//判空
bool IsEmpty(PList pl);
//删除
bool Delete(PList pl, int key);
int GetLength(PList pl);
//显示
void Show(PList pl);
#include <stdio.h>
#include "slist.h"
/*
//静态链表,利用顺序表模拟链表
//有效数据链的头为0下标,带头的循环链表
//空闲数据连的头为1下标,带头的循环链表
//
#define SIZE 10
typedef struct SNode
{
int data;
int netx; //下一个节点的地址
}SNode, *PList; //PList == SNode *
typedef SNode SList[SIZE];
*/
//初始化
void InitSList(PList pl)
{
for(int i = 0; i < SIZE; i++)
{
pl[i].next = i+1;
}
pl[0].next = 0;
pl[SIZE-1].next = 1;
}
//判满
static bool IsFull(PList pl)
{
return pl[1].next == 1;
}
//判空
bool IsEmpty(PList pl)
{
return pl[0].next == 0;
}
//插入o(1)
bool Insert(PList pl, int val)
{
if(IsFull(pl))
{
return false;
}
int p = pl[1].next; //先找到空闲节点
pl[p].data = val; //赋值
pl[1].next = pl[p].next; //将p从空闲链中删除
pl[p].next = pl[0].next; //将p插入到有效链
pl[0].next = p;
return true;
}
//查找
int Search(PList pl, int val);
//判空
bool IsEmpty(PList pl);
//删除
bool Delete(PList pl, int key);
int GetLength(PList pl);
//显示
void Show(PList pl)
{
for(int p = pl[0].next; p != 0; p = pl[p].next)
{
printf("%d ", pl[p].data);
}
printf("\n");
}
#include <stdio.h>
#include "slist.h"
int main()
{
SList s;
InitSList(s);
for(int i = 0; i < 10; i++)
{
Insert(s, i);
}
Show(s);
return 0;
}