数据结构学习之 静态链表(用顺序表模拟链表)

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值