数据结构c语言4:静态链表

创建结构体
typedef struct StaticLinkedNode{
	char data;
	int next;
}*NodePtr;

typedef struct StaticLinkedList{
	NodePtr node;
	int* used;
}*ListPtr;
初始化
ListPtr initLinkedList(){
	//分配空间
	ListPtr tempPtr=(ListPtr)malloc(sizeof(struct StaticLinkedList)); 
	
	tempPtr->node=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*D_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*D_SIZE);
	
	//创建头结点
	tempPtr->node[0].data='\0';
	tempPtr->node[0].next=-1;
	
	//空间使用情况
	tempPtr->used[0]=1;
	int i;
	for(i=1;i<D_SIZE;i++){
		tempPtr->used[i]=0;
	}
	
	return tempPtr; 
}

在这里插入图片描述

打印链表

```c
void printList(ListPtr paraListPtr){
	int p=0;
	while(p!=-1){
		printf("%c",paraListPtr->node[p].data);
		p=paraListPtr->node[p].next;
	}
	printf("\r\n");
}
插入元素到指定位置
void insertElement(ListPtr paraListPtr,char paraChar,int paraPosition){
	int p=0,q,i;
	
	//找位置 
	for(i=0;i<paraPosition;i++){
		p=paraListPtr->node[p].next;
		if(p==-1){
			printf("此位置不存在\r\n");
			return;	
		}
	}
	
	//创建新结点 
	for(i=1;i<D_SIZE;i++){
		if(paraListPtr->used[i]==0){
			printf("Space at %d allocated.\r\n", i);
			paraListPtr->used[i]=1;
			q=i;
			break;	
		}
	}
	
	if(i==D_SIZE){
		printf("No space\r\n");
		return;
	}
	
	paraListPtr->node[q].data=paraChar;
	
	//插入
	printf("Linking\r\n");
	paraListPtr->node[q].next=paraListPtr->node[p].next;
	paraListPtr->node[p].next=q;
} 

在这里插入图片描述

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

在这里插入图片描述

完整代码
#include<stdio.h>
#include<malloc.h>

#define D_SIZE 5

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

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

//初始化
ListPtr initLinkedList(){
	//分配空间
	ListPtr tempPtr=(ListPtr)malloc(sizeof(struct StaticLinkedList)); 
	
	tempPtr->node=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*D_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*D_SIZE);
	
	//创建头结点
	tempPtr->node[0].data='\0';
	tempPtr->node[0].next=-1;
	
	//空间使用情况
	tempPtr->used[0]=1;
	int i;
	for(i=1;i<D_SIZE;i++){
		tempPtr->used[i]=0;
	}
	
	return tempPtr; 
}

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

//插入元素到指定位置
void insertElement(ListPtr paraListPtr,char paraChar,int paraPosition){
	int p=0,q,i;
	
	//找位置 
	for(i=0;i<paraPosition;i++){
		p=paraListPtr->node[p].next;
		if(p==-1){
			printf("此位置不存在\r\n");
			return;	
		}
	}
	
	//创建新结点 
	for(i=1;i<D_SIZE;i++){
		if(paraListPtr->used[i]==0){
			printf("Space at %d allocated.\r\n", i);
			paraListPtr->used[i]=1;
			q=i;
			break;	
		}
	}
	
	if(i==D_SIZE){
		printf("No space\r\n");
		return;
	}
	
	paraListPtr->node[q].data=paraChar;
	
	//插入
	printf("Linking\r\n");
	paraListPtr->node[q].next=paraListPtr->node[p].next;
	paraListPtr->node[p].next=q;
} 

//删除元素
void deleteElement(ListPtr paraListPtr,char paraChar){
	int p,q;
	p=0;
	
	while((paraListPtr->node[p].next!=-1)&&(paraListPtr->node[paraListPtr->node[p].next].data!= paraChar)){
		p=paraListPtr->node[p].next;
	}
	
	if(paraListPtr->node[p].next==-1){
		printf("cannot delete %c\r\n",paraChar);
		return;
	}
	
	q=paraListPtr->node[p].next;
	paraListPtr->node[p].next=paraListPtr->node[paraListPtr->node[p].next].next;
	paraListPtr->used[q]=0;
} 

//测试
void test(){
	//建空表
	ListPtr tempList = initLinkedList();
	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); 
}

void main(){
	test();
} 

运行结果

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值