/*
*诸如BASIC和FORTRAN等许多语言都不支持指针,如果需要链表而又不能使用指针,那么就必须使用另外的实现方法
*本程序功能:使用游标的方法来实现单链表的插入、删除、查找、打印等功能
*存储:为了模仿指针、malloc、free等特性,声明一个全局的结构体数组,数组名FREE_SPACE;
*对于数组中的任何单元,数组下标可以用来代表一个地址
*调用malloc时,每次从cursor_space中取出一个单元
*调用free时,将单元加入到cursor_space中,在这里cursor_space就模仿了malloc和free能够操作的内存空间
*/
#include<stdio.h>
#define CURSOR_SPACE_SIZE 1000
typedef int ptr_to_node; /*指向结点的指针,用结构体数组的下标表示*/
struct node /*链表的单个结点单元*/
{
int data;
int next;
};
struct node cursor_space[CURSOR_SPACE_SIZE]; /*CURSOR_SPACE的空间,用单元0作为表头,其中data表示可用的空间数*/
static void free_space_init() /*初始化可供malloc使用的内存空间*/
{
int i;
cursor_space[0].data = CURSOR_SPACE_SIZE - 1; /*用单元0作为表头,其中data表示可用的空间数*/
for(i=0; i<CURSOR_SPACE_SIZE - 1; i++)
cursor_space[i].next = i+1 ; /*每个单元指向下一个单元*/
cursor_space[i].next = 0; /*最后一个单元指向NULL,用0表示*/
}
ptr_to_node cursor_malloc()
{
ptr_to_node new_node = cursor_space[0].next; /*取出FREE_SPACE里的第一个结点*/
cursor_space[0].next = cursor_space[new_node].next;
cursor_space[0].data --;
return new_node;
}
void cursor_free(ptr_to_node free_node) /*将free_node结点插入到0结点之后*/
{
cursor_space[0].data ++;
cursor_space[free_node].next = cursor_space[0].next;
cursor_space[0].next = free_node;
}
/*插入结点,里面的值按升序存放*/
void cursor_insert(ptr_to_node head, int value)
{
ptr_to_node prev, curr, new;
new = cursor_malloc();
cursor_space[new].data = value;
prev = head, curr = cursor_space[head].next;
while(curr != 0 && cursor_space[curr].data < value)
{
prev = curr;
curr = cursor_space[curr].next;
}
/*允许插入相同的数据*/
cursor_space[prev].next = new;
cursor_space[new].next = curr;
}
/*删除结点*/
void cursor_delete(ptr_to_node head, int value)
{
ptr_to_node prev, curr, temp;
prev = head, curr = cursor_space[head].next;
while(curr != 0 && cursor_space[curr].data < value)
{
prev = curr; curr = cursor_space[curr].next;
}
if(curr != 0 && cursor_space[curr].data == value)
{
cursor_space[prev].next = cursor_space[curr].next;
cursor_free(curr);
}
}
/*查找value是否在链表中*/
int cursor_member(ptr_to_node head, int value)
{
ptr_to_node curr = cursor_space[head].next;
while(curr!=0 && cursor_space[curr].data < value)
curr = cursor_space[curr].next;
if(curr!=0 && cursor_space[curr].data == value) return 1;
else return 0;
}
/*打印链表*/
void cursor_print(ptr_to_node head)
{
ptr_to_node curr = cursor_space[head].next;
printf("list content:");
while(curr != 0)
{
printf("%d ", cursor_space[curr].data);
curr = cursor_space[curr].next;
}
printf("\n");
}
int main(int argc, char **argv)
{
free_space_init();
ptr_to_node head = cursor_malloc();
cursor_space[head].data = 0;
cursor_space[head].next = 0;
printf("remain space : %d\n", cursor_space[0].data);
cursor_insert(head, 4);
cursor_insert(head, 2);
cursor_insert(head, 3);
cursor_insert(head, 8);
cursor_insert(head, 4);
cursor_insert(head, 1);
cursor_insert(head, 0);
cursor_insert(head, 15);
cursor_insert(head, 14);
cursor_insert(head, 44);
cursor_print(head);
printf("remain space : %d\n", cursor_space[0].data);
printf("0?exist: %d\n", cursor_member(head,0));
printf("8?exist: %d\n", cursor_member(head,8));
printf("44?exist: %d\n", cursor_member(head,44));
printf("-1?exist: %d\n", cursor_member(head,-1));
printf("9?exist: %d\n", cursor_member(head,9));
printf("55?exist: %d\n", cursor_member(head,55));
cursor_delete(head, 0);
cursor_delete(head, 8);
cursor_delete(head, 44);
cursor_delete(head, -1);
cursor_delete(head, 9);
cursor_delete(head, 55);
cursor_print(head);
printf("remain space : %d\n", cursor_space[0].data);
}
单链表的游标实现:插入、删除、查找、打印等功能
最新推荐文章于 2022-06-25 22:29:30 发布