静态链表的有关操作

本文详细介绍了静态链表的概念、创建、初始化、打印、插入、删除、查找元素等基本操作。通过示例代码展示了如何在C语言中实现静态链表,并提供了测试用例及其结果,帮助读者理解静态链表的工作原理。

一、静态链表的意义

静态链表,就是用数组描述的链表。静态链表在初始化时要确定数组的长度,所以需要预先分配空间,其空间大小一般是静态的,故名静态链表

二、创建静态链表

代码演示:

typedef struct StaticLinkedNode{
	char data;
	int next;      //游标
}*NodePtr; 

typedef struct StaticLinkedList{
	NodePtr nodes;![请添加图片描述](https://img-blog.csdnimg.cn/8fc4adde65c245c28eda9bbd8f1dbfd7.png)

	int *used;
}*ListPtr;

*1、next代表游标
2、用used指针判断空间是否已经分配 *

三、初始化静态链表

请添加图片描述
注意:
初始化要让头结点数据为空,且使其游标为-1,当然也要标注该空间已经分分配,以及标注后面的空间未分配

代码演示

ListPtr initLinkedList(){
	ListPtr tempPtr = (ListPtr)malloc(sizeof (struct StaticLinkedList));
	tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
	tempPtr->nodes[0].data='\0' ;
	tempPtr->nodes[0].next=-1;
	tempPtr->used[0]=1;
	for(int i=0;i<DEFAULT_SIZE;i++){
		tempPtr->used[i]=0;
	}
	return tempPtr;
}

注:这里要注意分配的空间需要乘最大结点数

四、打印

代码演示:

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

注:需要理解 p=paraListPtr->nodes[p].next ,非常重要!!!

五、插入

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

注:
1、首先需要找到插入的位置,这点和之前一样
2、然后需要找到未分配的空间,从之前的used[0]开始,used[0]已经分配,之后每分配一个空间都需标注为已经分配
3、注意游标的变化!(如下图,看两次图的变化)

请添加图片描述

六、删除

代码演示:

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

注:删除之后各个元素下标不会改变

七、查找第x位元素

代码演示:

void getElement(ListPtr paraListPtr,int paraPosition){
	int p=0;
	char q;
	for(int i=0;i<paraPosition;i++){
		p=paraListPtr->nodes[p].next;
	}
	q=paraListPtr->nodes[p].data;
	printf("第%d位是%c",paraPosition,q);
}

八、查找元素x在第几位

代码演示:

void locateElement(ListPtr paraListPtr,char paraChar){
	int p=0,num = 0;
	while(paraListPtr->nodes[p].data!=paraChar){
		num++;
		p=paraListPtr->nodes[p].next;
	}
	printf("%c在第%d位",paraChar,num);
}

注:这里不能直接用下标做位数,原因看删除操作注释

九、测试

void Test(){
	ListPtr List=initLinkedList();
	insertElement(List,'H',0);
	insertElement(List,'E',1);
	insertElement(List,'L',2);
	insertElement(List,'a',3);
	insertElement(List,'O',4);
	printList(List);
	deleteElement(List,'E');
	printList(List);
	getElement(List,2);
	printf("\r\n");
	locateElement(List,'L');
}

十、此时链表

请添加图片描述

十一、总代码

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

#define DEFAULT_SIZE 5

typedef struct StaticLinkedNode{
	char data;
	int next;      //游标
}*NodePtr; 

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

ListPtr initLinkedList(){
	ListPtr tempPtr = (ListPtr)malloc(sizeof (struct StaticLinkedList));
	tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
	tempPtr->nodes[0].data='\0' ;
	tempPtr->nodes[0].next=-1;
	tempPtr->used[0]=1;
	for(int i=0;i<DEFAULT_SIZE;i++){
		tempPtr->used[i]=0;
	}
	return tempPtr;
}

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

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

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

void getElement(ListPtr paraListPtr,int paraPosition){
	int p=0;
	char q;
	for(int i=0;i<paraPosition;i++){
		p=paraListPtr->nodes[p].next;
	}
	q=paraListPtr->nodes[p].data;
	printf("第%d位是%c",paraPosition,q);
}

void locateElement(ListPtr paraListPtr,char paraChar){
	int p=0,num = 0;
	while(paraListPtr->nodes[p].data!=paraChar){
		num++;
		p=paraListPtr->nodes[p].next;
	}
	printf("%c在第%d位",paraChar,num);
}

void Test(){
	ListPtr List=initLinkedList();
	insertElement(List,'H',0);
	insertElement(List,'E',1);
	insertElement(List,'L',2);
	insertElement(List,'a',3);
	insertElement(List,'O',4);
	printList(List);
	deleteElement(List,'E');
	printList(List);
	getElement(List,2);
	printf("\r\n");
	locateElement(List,'L');
}

int main(){
	Test();
}

十二、测试结果

Space at 1 allocated.
Space at 2 allocated.
Space at 3 allocated.
Space at 4 allocated.
No space
 HELa
 HLa
第2位是L
L在第2

游标下标的关系需要非常注意,随时注意空间的分配情况

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值