栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构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='';
	tempPtr->node[0].next=-1;
	
	//空间使用情况
	tempPtr->used[0]=1;
	int i;
	for(i=1;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("rn");
}
插入元素到指定位置
void insertElement(ListPtr paraListPtr,char paraChar,int paraPosition){
	int p=0,q,i;
	
	//找位置 
	for(i=0;i
		p=paraListPtr->node[p].next;
		if(p==-1){
			printf("此位置不存在rn");
			return;	
		}
	}
	
	//创建新结点 
	for(i=1;i
		if(paraListPtr->used[i]==0){
			printf("Space at %d allocated.rn", i);
			paraListPtr->used[i]=1;
			q=i;
			break;	
		}
	}
	
	if(i==D_SIZE){
		printf("No spacern");
		return;
	}
	
	paraListPtr->node[q].data=paraChar;
	
	//插入
	printf("Linkingrn");
	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 %crn",paraChar);
		return;
	}
	
	q=paraListPtr->node[p].next;
	paraListPtr->node[p].next=paraListPtr->node[paraListPtr->node[p].next].next;
	paraListPtr->used[q]=0;
} 

完整代码
#include
#include

#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='';
	tempPtr->node[0].next=-1;
	
	//空间使用情况
	tempPtr->used[0]=1;
	int i;
	for(i=1;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("rn");
}

//插入元素到指定位置
void insertElement(ListPtr paraListPtr,char paraChar,int paraPosition){
	int p=0,q,i;
	
	//找位置 
	for(i=0;i
		p=paraListPtr->node[p].next;
		if(p==-1){
			printf("此位置不存在rn");
			return;	
		}
	}
	
	//创建新结点 
	for(i=1;i
		if(paraListPtr->used[i]==0){
			printf("Space at %d allocated.rn", i);
			paraListPtr->used[i]=1;
			q=i;
			break;	
		}
	}
	
	if(i==D_SIZE){
		printf("No spacern");
		return;
	}
	
	paraListPtr->node[q].data=paraChar;
	
	//插入
	printf("Linkingrn");
	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 %crn",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'.rn");
	deleteElement(tempList, 'e');
	printf("Deleting 'a'.rn");
	deleteElement(tempList, 'a');
	printf("Deleting 'o'.rn");
	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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853437.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号