3.线性表-cursor
fatal.h
#include <stdio.h>
#include <stdlib.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1)
cursor.h
typedef int ElementType;
#define SpaceSize 100
#ifndef _Cursor_H
#define _Cursor_H
typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
void InitializeCursorSpace(void);
List MakeEmpty(List L);
int IsEmpty(const List L);
int IsLast(const Position P, const List L);
Position Find(ElementType X, const List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, const List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
Position Header(const List L);
Position First(const List L);
Position Advance(const Position P);
ElementType Retrieve(const Position P);
#endif
cursor.c
#include "cursor.h"
#include <stdlib.h>
#include "fatal.h"
/* Place in the interface file */
struct Node
{
ElementType Element;
Position Next;
};
struct Node CursorSpace[SpaceSize];
static Position CursorAlloc(void)
{
Position P;
P = CursorSpace[0].Next;
CursorSpace[0].Next = CursorSpace[P].Next;
return P;
}
static void CursorFree(Position P)
{
CursorSpace[P].Next = CursorSpace[0].Next;
CursorSpace[0].Next = P;
}
void InitializeCursorSpace(void)
{
int i;
for (i = 0; i < SpaceSize; i++)
CursorSpace[i].Next = i + 1;
CursorSpace[SpaceSize - 1].Next = 0;
}
List MakeEmpty(List L)
{
if (L != 0)
DeleteList(L);
L = CursorAlloc();
if (L == 0)
FatalError("Out of memory!");
CursorSpace[L].Next = 0;
return L;
}
/* Return true if L is empty */
int IsEmpty(const List L)
{
return CursorSpace[L].Next == 0;
}
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */
int IsLast(const Position P, const List L)
{
return CursorSpace[P].Next == 0;
}
/* Return Position of X in L; 0 if not found */
/* Uses a header node */
Position Find(ElementType X, const List L)
{
Position P;
P = CursorSpace[L].Next;
while (P && CursorSpace[P].Element != X)
P = CursorSpace[P].Next;
return P;
}
/* Delete from a list */
/* Assume that the position is legal */
/* Assume use of a header node */
void Delete(ElementType X, List L)
{
Position P, TmpCell;
P = FindPrevious(X, L);
if (!IsLast(P, L)) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = CursorSpace[P].Next;
CursorSpace[P].Next = CursorSpace[TmpCell].Next;
CursorFree(TmpCell);
}
}
/* If X is not found, then Next field of returned value is 0 */
/* Assumes a header */
Position FindPrevious(ElementType X, const List L)
{
Position P;
P = L;
while (CursorSpace[P].Next &&
CursorSpace[CursorSpace[P].Next].Element != X)
P = CursorSpace[P].Next;
return P;
}
/* Insert (after legal position P) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */
void Insert(ElementType X, List L, Position P)
{
Position TmpCell;
TmpCell = CursorAlloc();
if (TmpCell == 0)
FatalError("Out of space!!!");
CursorSpace[TmpCell].Element = X;
CursorSpace[TmpCell].Next = CursorSpace[P].Next;
CursorSpace[P].Next = TmpCell;
}
/* Correct DeleteList algorithm */
void DeleteList(List L)
{
Position P, Tmp;
P = CursorSpace[L].Next; /* Header assumed */
CursorSpace[L].Next = 0;
while (P != 0)
{
Tmp = CursorSpace[P].Next;
CursorFree(P);
P = Tmp;
}
}
Position Header(const List L)
{
return L;
}
Position First(const List L)
{
return CursorSpace[L].Next;
}
Position Advance(const Position P)
{
return CursorSpace[P].Next;
}
ElementType Retrieve(const Position P)
{
return CursorSpace[P].Element;
}
testcurs.c
#include <stdio.h>
#include "cursor.h"
void PrintList(const List L)
{
Position P = Header(L);
if (IsEmpty(L))
printf("Empty list\n");
else
{
do
{
P = Advance(P);
printf("%d ", Retrieve(P));
} while (!IsLast(P, L));
printf("\n");
}
}
int main()
{
List L;
Position P;
int i;
InitializeCursorSpace();
L = MakeEmpty(0);
P = Header(L);
PrintList(L);
for (i = 0; i < 10; i++)
{
Insert(i, L, P);
PrintList(L);
P = Advance(P);
}
for (i = 0; i < 10; i += 2)
Delete(i, L);
for (i = 10; i < 15; i++)
{
Insert(i, L, P);
PrintList(L);
P = Advance(P);
}
for (i = 0; i < 10; i++)
if (Find(i, L) == 0)
printf("Element %d Find fails\n", i);
printf("Finished deletions\n");
PrintList(L);
DeleteList(L);
return 0;
}
函数调用关系图(Call graph)

3.线性表-cursor的更多相关文章
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 顺序存储线性表_ArrayList
相信大家在日常开发过程中 List 应该使用的非常非常多,今天就来简单学习一下 List 的数据结构 顺序存储线性表. 一.什么是顺序存储线性表 顺序存储线性表是最基本.最简单.也是最常用的一种数据结 ...
- 线性表Linearlist
顺序存储,链式存储,索引存储,散列存储 基本运算 SLIST 1.置空表 void SetNull(&L) 2.求长度 int Length(L) 3.取元素 ...
- 数据结构(Java描述)之线性表
基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...
- JAVASE02-Unit04: 集合框架 、 集合操作 —— 线性表
Unit04: 集合框架 . 集合操作 -- 线性表 操作集合元素相关方法 package day04; import java.util.ArrayList; import java.util.Co ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- Java集合类学习笔记(各种线性表性能分析)
ArrayList.LinkedList是线性表的两种典型实现:基于数组的线性表和基于链的线性表. Queue代表了队列,Deque代表了双端队列. 一般来说,由于数组以一块连续内存区来保存所有的数组 ...
- 动态分配的顺序线性表的十五种操作—C语言实现
线性表 定义:是最常用的,也是最简单的数据结构,是长度为n个数据元素的有序的序列. 含有大量记录的线性表叫文件 记录:稍微复杂的线性表里,数据元素为若干个数据项组成,这时把一个数据元素叫记录 结构特点 ...
- Java Se :线性表
Java的集合框架分为两个系列,Collection和Map系列.在大学期间,学习数据结构时,好像学习了线性表.非线性表.树,哎,都给忘了.其实,在Collection系列内部又可以分为线性表.集合两 ...
随机推荐
- JQuery EasyUI DataGrid获取当前行或选中行
1.获取当前选中行,如果没有选中行,则返回 null var row = $('#gridID').datagrid('getSelected'); 2.获取当前所有选中行数据,返回元素记录的数组数据 ...
- PHP HTML代码反转义
后端为了防止xss的攻击,会过滤前端用户的输入的数据,这样虽然有效的避免xss攻击,但是会带来一个问题,要么全部过滤html留下不非法的数据,要么把HTML代码转义,但是转义之后又会直接在浏览器内显示 ...
- Java Native Interface 二 JNI中对Java基本类型和引用类型的处理
本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 Java编程里会使用到两种类型:基本类型(如 ...
- 学习JVM GarbageCollection
前言 Java和C++之间显著的一个区别就是对内存的管理.Java和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称 ...
- 初学Redis(1)——认识Redis
Redis官网对Redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...
- .NET LINQ标准查询运算符
标准查询运算符概述 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法. 大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了 IEnumerable<T> ...
- 腾讯QQ形象18年变迁史,最早的QQ企鹅形象居然长这样!
1999年,腾讯创建之初,这是当时QQ的形象,现在看起来很滑稽,又高又瘦.当时公司并没有专职的设计师,所以这个形象就凑合着用了. 后来,腾讯也觉着上一个形象做的不是特别好,于是在2000年的时候,公司 ...
- java 项目中几种O实体类的概念
经常会接触到vo,do,dto的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,vo对应于页面上需要显示的数据(表单),do对应 ...
- Swift - UIColor16进制编码与RGB格式互相转换
Swift UIColor 16进制编码转换RGB : 由于UI出图的时候,通常给的是16进制的编码颜色,我们在开发的时候需要将它转换为RGB格式,现在给出两种代码片段. 一.对UIColor进行扩展 ...
- 基于AgileEAS.NET SOA 平台SAAS架构技术的开源分销ERP系统-SmartERP.NET下载配置说明
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...