数据结构04-静态链表

目录

介绍

结构

初始化

打印

插入元素

删除元素

测试函数

主函数

测试结果

完整代码


介绍

静态链表是借助数组来描述线性表的链式存储结构,此时的数组的分量就是我们自己定义的结构体,数组中的一个分量表示一个结点,同时用int类型的元素代替指针描述结点在数组中的相对位置。此外,数组的第0个分量可以看成链表中的头结点,其游标即链表中的指针域则就指向链表的第一个结点。这种存储结构是需要预先分配一个较大的空间,但是在线性表的插入和删除时是不需要移动元素的,仅需要修改游标,因此是具有链式存储结构的主要优点的。

结构

typedef struct staticLinkedNode{
	char data;
	int next;
}*NodePtr;

typedef struct staticLinkedList{
	NodePtr nodes;
	int* used;
}*ListPtr;

初始化

ListPtr iniLinkedList(){
	ListPtr tempPtr = (ListPtr)malloc(sizeof(struct staticLinkedList));
	
	tempPtr->nodes = (NodePtr)malloc(sizeof(struct staticLinkedNode)*SIZE);
	tempPtr->used = (int*)malloc(sizeof(int)*SIZE);
	
	tempPtr->nodes[0].data = 0;
	tempPtr->nodes[0].next = -1;
	
	tempPtr->used[0] = 1;
	for(int i = 1; i < SIZE;i++){
		tempPtr->used[i] = 0;
	}
	return tempPtr;
}

打印

void printList(ListPtr pList){
	int p = 0;
	while(p != -1){
		printf("%c",pList->nodes[p].data);
		p = pList->nodes[p].next;
	}
	printf("\n");
}

插入元素

void insertElement(ListPtr pList,char pchar,int pos){
	int p,q,i;
	
	p = 0;
	for(int i = 0; i < pos;i++){
		p = pList->nodes[p].next;
		if(p == -1){
			printf("The position %d is beyond the scope of the list.\n",pos);
			return;
		}
	}
	
	for(i = 0;i < SIZE;i++){
		if(pList->used[i]==0){
			printf("Space at %d allocated.\n",i);
			pList->used[i] = 1;
			q = i;
			break;
		}
	}
	if(i == SIZE){
		printf("No space.\n");
		return;
	}
	
	pList->nodes[q].data = pchar;
	
	printf("Linking\n");
	pList->nodes[q].next = pList->nodes[p].next;
	pList->nodes[p].next = q;
}

 

删除元素

void deleteElement(ListPtr pList,char pchar){
	int p,q;
	p = 0;
	
	while((pList->nodes[p].next != -1) &&(pList->nodes[pList->nodes[p].next].data != pchar)){
		p = pList->nodes[p].next;
	}
	
	if(pList->nodes[p].next == -1){
		printf("Cannot delete %c\n",pchar);
		return;
	}
	
	q = pList->nodes[p].next;
	pList->nodes[p].next = pList->nodes[pList->nodes[p].next].next;
	
	pList->used[q] = 0;
}

 

测试函数

void insertdeleteTest(){
	ListPtr tempList = iniLinkedList();
	printList(tempList);

	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	printList(tempList);

	printf("Deleting 'e'.\r\n");
	deleteElement(tempList, 'e');
	printf("Deleting 'a'.\r\n");
	deleteElement(tempList, 'a');
	printf("Deleting 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'x', 1);
	printList(tempList);
}

主函数

int main(){
	insertdeleteTest();
	return 0;
}

测试结果


Space at 1 allocated.
Linking
Space at 2 allocated.
Linking
Space at 3 allocated.
Linking
Space at 4 allocated.
Linking
No space.
 Hell
Deleting 'e'.
Deleting 'a'.
Cannot delete a
Deleting 'o'.
Cannot delete o
 Hll
Space at 2 allocated.
Linking
 Hxll

完整代码

#include <stdio.h>
#include <malloc.h>

#define SIZE 5

typedef struct staticLinkedNode{
	char data;
	int next;
}*NodePtr;

typedef struct staticLinkedList{
	NodePtr nodes;
	int* used;
}*ListPtr;

ListPtr iniLinkedList(){
	ListPtr tempPtr = (ListPtr)malloc(sizeof(struct staticLinkedList));
	
	tempPtr->nodes = (NodePtr)malloc(sizeof(struct staticLinkedNode)*SIZE);
	tempPtr->used = (int*)malloc(sizeof(int)*SIZE);
	
	tempPtr->nodes[0].data = 0;
	tempPtr->nodes[0].next = -1;
	
	tempPtr->used[0] = 1;
	for(int i = 1; i < SIZE;i++){
		tempPtr->used[i] = 0;
	}
	return tempPtr;
}

void printList(ListPtr pList){
	int p = 0;
	while(p != -1){
		printf("%c",pList->nodes[p].data);
		p = pList->nodes[p].next;
	}
	printf("\n");
}

void insertElement(ListPtr pList,char pchar,int pos){
	int p,q,i;
	
	p = 0;
	for(int i = 0; i < pos;i++){
		p = pList->nodes[p].next;
		if(p == -1){
			printf("The position %d is beyond the scope of the list.\n",pos);
			return;
		}
	}
	
	for(i = 0;i < SIZE;i++){
		if(pList->used[i]==0){
			printf("Space at %d allocated.\n",i);
			pList->used[i] = 1;
			q = i;
			break;
		}
	}
	if(i == SIZE){
		printf("No space.\n");
		return;
	}
	
	pList->nodes[q].data = pchar;
	
	printf("Linking\n");
	pList->nodes[q].next = pList->nodes[p].next;
	pList->nodes[p].next = q;
}

void deleteElement(ListPtr pList,char pchar){
	int p,q;
	p = 0;
	
	while((pList->nodes[p].next != -1) &&(pList->nodes[pList->nodes[p].next].data != pchar)){
		p = pList->nodes[p].next;
	}
	
	if(pList->nodes[p].next == -1){
		printf("Cannot delete %c\n",pchar);
		return;
	}
	
	q = pList->nodes[p].next;
	pList->nodes[p].next = pList->nodes[pList->nodes[p].next].next;
	
	pList->used[q] = 0;
}

void insertdeleteTest(){
	ListPtr tempList = iniLinkedList();
	printList(tempList);

	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	printList(tempList);

	printf("Deleting 'e'.\r\n");
	deleteElement(tempList, 'e');
	printf("Deleting 'a'.\r\n");
	deleteElement(tempList, 'a');
	printf("Deleting 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'x', 1);
	printList(tempList);
}

int main(){
	insertdeleteTest();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值