数据结构-静态链表


一、静态链表定义

静态链表的数据储存在一块指定的内存空间中,数据元素在这块内存空间中随机储存。换句话说,静态链表是用数组来实现链式储存的,方便一些没有指针类型的高级语言来使用链式结构。

数据存放到数组中时,给每个数据元素都配备一个整形变量,该变量用于指明各个元素的直接后继元素所在数组中的位置下标,如图 :
在这里插入图片描述

二、操作

1.定义节点

typedef struct StaticLinkNode
{
	char data;
	int next; //存的是下一个数据在数组中的位置下标 
}*NodePtr;

typedef struct  StaticLinkList
{
	NodePtr nodes; //nodes是存储节点 
	int *used; //used表示空间使用情况,0表示空闲,1表示占用 
}*ListPtr;

2.初始化

a[0]存的是头节点,可以不赋值。刚开始没有元素,所以令a[0]->next=-1。
在这里插入图片描述

ListPtr initList()
{
	ListPtr tempPtr=(ListPtr)malloc(sizeof(struct StaticLinkList));
	
	tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkNode)*MAX_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*MAX_SIZE);
	
	tempPtr->nodes[0].data='\0';
	tempPtr->nodes[0].next=-1; //-1表示NULL 
	
	tempPtr->used[0]=1;
	int i;
	for(i=0;i<MAX_SIZE;i++)
	{
		tempPtr->used[i]=0;
	}
	
	return tempPtr;
}

3.打印链表

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

4.插入

这里是尾插,当每插入一个元素时,需要更改前一个节点的next,使他们链接起来。
在这里插入图片描述

void insertElement(ListPtr pListPtr,char pchar,int pos)
{
	int p,q,i;
	
	p=0;
	if(pos<0)
	{
		printf("插入位置不合法!\n");
		return; 
	}
	for(i=0;i<pos;i++)
	{
		p=pListPtr->nodes[p].next;
		if(p==-1)
		{
			printf("该位置超出列表范围!\n");
			return;
		}
	}
	
	for(i=1;i<MAX_SIZE;i++)
	{
		if(pListPtr->used[i]==0)
		{
			printf("位置(%d)已被分配空间!",i);
			pListPtr->used[i]=1;
			q=i;
			break;
		}
	
	}
	if(i==MAX_SIZE)
	{
		printf("空间已耗尽,无法插入!\n");
		return;
	}
	
	pListPtr->nodes[q].data=pchar;
	
	pListPtr->nodes[q].next=pListPtr->nodes[p].next;
	pListPtr->nodes[p].next=q;
	printf("插入成功!\n");
}

5.删除

void deleteElement(ListPtr pListPtr,char pchar)
{
	int p,q;
	p=0;
	while((pListPtr->nodes[p].next!=-1)&&(pListPtr->nodes[pListPtr->nodes[p].next].data!=pchar))
	{
		p=pListPtr->nodes[p].next; 
	} 
	if(pListPtr->nodes[p].next==-1)
	{
		printf("元素(%c)不存在于链表中!\n",pchar);
		return;
	}
	q=pListPtr->nodes[p].next;
	pListPtr->nodes[p].next=pListPtr->nodes[q].next;
	pListPtr->used[q]=0;
}

6.定位

void locateElement(ListPtr pListPtr,char pchar)
{
	int p,q,cnt=0;
	p=0;
	while((pListPtr->nodes[p].next!=-1) && (pListPtr->nodes[p].data!=pchar))
	{
		p=pListPtr->nodes[p].next;
		cnt++;
	}
	if(pListPtr->nodes[p].next==-1)
	{
		printf("元素(%c)不存在于链表中!\n",pchar);
		return;
	}
	printf("元素(%c)的位置:%d\n",pchar,cnt);
}

7.清空

void clear_list(ListPtr tempList)
{
	int q;
	for(q=0;tempList->nodes[q].next==0;q=tempList->nodes[q].next)
	{
		tempList->nodes[q].next=tempList->nodes[1].next;
		tempList->nodes[1].next=tempList->nodes[0].next;
		tempList->nodes[0].next=0; 
	}
	
	printf("完成清空!");
}

8.测试

void insert_delete_locate_clear_list_text()
{
	ListPtr tempList=initList();
	printList(tempList);
	
	
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	
	insertElement(tempList, 'M', 6);
	insertElement(tempList, 'o', 4);
	printList(tempList);
	
	printf("Delete 'e'.\r\n");
	deleteElement(tempList, 'e');
	printList(tempList);
	printf("Delete 'a'.\r\n");
	deleteElement(tempList, 'a');
	printList(tempList);
	printf("Delete 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);
	insertElement(tempList, 'o', 2);
	printList(tempList);
	
	locateElement(tempList,'8');
	locateElement(tempList,'H');
	
	clear_list(tempList);
}

三.总代码

#include<stdio.h>
#include<malloc.h>
#define MAX_SIZE 6

typedef struct StaticLinkNode
{
	char data;
	int next; //存的是下一个数据在数组中的位置下标 
}*NodePtr;

typedef struct  StaticLinkList
{
	NodePtr nodes; //nodes是存储节点 
	int *used; //used表示空间使用情况,0表示空闲,1表示占用 
}*ListPtr;

ListPtr initList()
{
	ListPtr tempPtr=(ListPtr)malloc(sizeof(struct StaticLinkList));
	
	tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkNode)*MAX_SIZE);
	tempPtr->used=(int*)malloc(sizeof(int)*MAX_SIZE);
	
	tempPtr->nodes[0].data='\0';
	tempPtr->nodes[0].next=-1; //-1表示NULL 
	
	tempPtr->used[0]=1;
	int i;
	for(i=0;i<MAX_SIZE;i++)
	{
		tempPtr->used[i]=0;
	}
	
	return tempPtr;
}

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

void insertElement(ListPtr pListPtr,char pchar,int pos)
{
	int p,q,i;
	
	p=0;
	if(pos<0)
	{
		printf("插入位置不合法!\n");
		return; 
	}
	for(i=0;i<pos;i++)
	{
		p=pListPtr->nodes[p].next;
		if(p==-1)
		{
			printf("该位置超出列表范围!\n");
			return;
		}
	}
	
	for(i=1;i<MAX_SIZE;i++)
	{
		if(pListPtr->used[i]==0)
		{
			printf("位置(%d)已被分配空间!",i);
			pListPtr->used[i]=1;
			q=i;
			break;
		}
	
	}
	if(i==MAX_SIZE)
	{
		printf("空间已耗尽,无法插入!\n");
		return;
	}
	
	pListPtr->nodes[q].data=pchar;
	
	pListPtr->nodes[q].next=pListPtr->nodes[p].next;
	pListPtr->nodes[p].next=q;
	printf("插入成功!\n");
}

void deleteElement(ListPtr pListPtr,char pchar)
{
	int p,q;
	p=0;
	while((pListPtr->nodes[p].next!=-1)&&(pListPtr->nodes[pListPtr->nodes[p].next].data!=pchar))
	{
		p=pListPtr->nodes[p].next; 
	} 
	if(pListPtr->nodes[p].next==-1)
	{
		printf("元素(%c)不存在于链表中!\n",pchar);
		return;
	}
	q=pListPtr->nodes[p].next;
	pListPtr->nodes[p].next=pListPtr->nodes[q].next;
	pListPtr->used[q]=0;
}

void locateElement(ListPtr pListPtr,char pchar)
{
	int p,q,cnt=0;
	p=0;
	while((pListPtr->nodes[p].next!=-1) && (pListPtr->nodes[p].data!=pchar))
	{
		p=pListPtr->nodes[p].next;
		cnt++;
	}
	if(pListPtr->nodes[p].next==-1)
	{
		printf("元素(%c)不存在于链表中!\n",pchar);
		return;
	}
	printf("元素(%c)的位置:%d\n",pchar,cnt);
}

void insert_delete_locate_clear_list_text()
{
	ListPtr tempList=initList();
	printList(tempList);
	
	
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	
	insertElement(tempList, 'M', 6);
	insertElement(tempList, 'o', 4);
	printList(tempList);
	
	printf("Delete 'e'.\r\n");
	deleteElement(tempList, 'e');
	printList(tempList);
	printf("Delete 'a'.\r\n");
	deleteElement(tempList, 'a');
	printList(tempList);
	printf("Delete 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);
	insertElement(tempList, 'o', 2);
	printList(tempList);
	
	locateElement(tempList,'8');
	locateElement(tempList,'H');
	
	clear_list(tempList);
}

void clear_list(ListPtr tempList)
{
	int q;
	for(q=0;tempList->nodes[q].next==0;q=tempList->nodes[q].next)
	{
		tempList->nodes[q].next=tempList->nodes[1].next;
		tempList->nodes[1].next=tempList->nodes[0].next;
		tempList->nodes[0].next=0; 
	}
	
	printf("完成清空!");
}
int main()
{
	insert_delete_locate_clear_list_text();
	return 0;
}

运行结果:


位置(1)已被分配空间!插入成功!
位置(2)已被分配空间!插入成功!
位置(3)已被分配空间!插入成功!
位置(4)已被分配空间!插入成功!
位置(5)已被分配空间!插入成功!
该位置超出列表范围!
空间已耗尽,无法插入!
 Hello
Delete 'e'.
 Hllo
Delete 'a'.
元素(a)不存在于链表中!
 Hllo
Delete 'o'.
 Hll
位置(2)已被分配空间!插入成功!
 Hlol
元素(8)不存在于链表中!
元素(H)的位置:1
完成清空!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值