顺序表相关操作(C语言)

一、函数介绍

1、打印顺序表

void outputlist(Slistptr plist){
	int i;
	for( i=0; i<plist->length; i++){
		printf("%d ",plist->data[i]);
	}
	printf("\n");
} 

2、顺序表初始化

Slistptr slistInit(int pdata[],int plength){
	Slistptr resultptr=(Slistptr)malloc(sizeof(struct Slist));
	int i;
	for(i=0;i<plength;i++){
		resultptr->data[i]=pdata[i];
	}
	resultptr->length=plength;
	
	return resultptr;
} 

3、指定位置的插入

void SlistInsert(Slistptr plistptr,int Position,int value){
	//1、Space check. 
	if(plistptr->length>=list_max_length){
		printf("list is full.\n");
		return;
	}
	//2、Position check.
	if(Position<0){
		printf("Position is illegal.\n");
		return;
	}
	if(Position>plistptr->length){
		printf("The position is bigger than the list length.\n");
		return;
	}
	//3、Move the remaining part.
	int i;
	for(i=plistptr->length;i>Position;i--){
		plistptr->data[i]=plistptr->data[i-1];
	} 
	//4、Insert.
	plistptr->data[Position]=value;
	//5、Update the length.
	plistptr->length++;
} 

4、插入功能测试

void InsertTest(){
	int i;
	int temparray[5]={1,4,3,6,8};
	
	printf("---- sequentialInsertTest begins. ----\n");
	//初始化 
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	
	//在第一个位置插入
	printf("After instering,the list is:");
	SlistInsert(templist,0,9);
	outputlist(templist);
	
	//在末尾插入
	printf("After instering,the list is:");
	SlistInsert(templist,6,7);
	outputlist(templist);
	
	//插入位置不在范围
	printf("After instering,the list is:");
	SlistInsert(templist,8,70);
	printf("The list is:"); 
	outputlist(templist);
	
	//连续插入三个 
	for(i=0;i<5;i++){
		printf("Inserting %d.\n",i+10);
		SlistInsert(templist,0,i+10);
		outputlist(templist);
	}
	
	printf("---- sequentialInsertTest ends. ----\n\n");
	outputMemory(templist);	
} 

5、指定位置的删除

int SlistDelete(Slistptr plistptr,int Position){
	//1、Position check.
	if(Position<0){
		printf("Invalid position.\n");
		return -1;
	} 
	if(Position>=plistptr->length){
		printf("The position is beyond the list length.\n");
		return -1;
	} 
	
	//2、Move the remaining part.
	int i;
	int temp=plistptr->data[Position];
	for(i=Position;i<plistptr->length;i++){
		plistptr->data[i]=plistptr->data[i+1];
	} 
	
	//3、Update the length.
	plistptr->length--;
	
	//4、Return the temp;
	return temp; 
}

6、删除功能测试

void DeleteTest(){
	int temparray[5]={3,6,2,8,50};
	
	printf("---- sequentialDeleteTest begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	
	//删除第一个
	printf("After deleting,the list is:");
	SlistDelete(templist,0);
	outputlist(templist);
	
	//删除最后一个
	printf("After deleting,the list is:");
	SlistDelete(templist,3);
	outputlist(templist);
	
	//删除第二个
	printf("After deleting,the list is:");
	SlistDelete(templist,1);
	outputlist(templist);
	
	//超出范围示例 
	printf("Now delete the 5th, the list is: ");
	SlistDelete(templist,5);
	printf("The list is:");
	outputlist(templist);
	
	printf("Now delete the -5th, the list is: ");
	SlistDelete(templist,-5);
	printf("The list is:");
	outputlist(templist);
	
	printf("---- sequentialDeleteTest ends. ----\n\n");
	outputMemory(templist);	
}

7、获得指定元素在表中的位置

void Elementlocate(Slistptr plistptr,int value){
	int i;
	for(i=0;i<plistptr->length;i++)
	{
		if(plistptr->data[i]==value){
			printf("The value(%d)‘s location:%d\n",value,i+1);
			return;
		}
	}
	printf("The value(%d) does not exist in the list.\n",value);
}

8、获得指定元素在表中的位置函数测试

void Elementlocate_text(){
	int temparray[5]={3,6,2,8,50};
	printf("---- Elementlocate_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	Elementlocate(templist,8);
	printf("---- Elementlocate_text ends. ----\n\n");
}

9、获得指定位置上的元素

int getElement(Slistptr plistptr,int position){
	if(position<0){
		printf("Invalid position.\n");
		return -1;
	}
	if(position>=plistptr->length){
		printf("The position is beyond the list length.\n");
		return -1;
	}
	
	return plistptr->data[position-1];
}

10、获得指定位置上的元素测试

void getElement_text(){
	int temparray[5]={3,6,2,8,50};
	int temp;
	printf("---- getElement_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	temp=getElement(templist,2);
	if(temp!=-1){
		printf("The position’s number is:%d\n",temp);
	}

	printf("---- getElement_text ends. ----\n\n");
}

11、获得指定元素的前驱

void PriorElement(Slistptr plistptr,int target){
	int i,temp;
	for(i=0;i<plistptr->length;i++){
		if(plistptr->data[i]==target&&i!=0){
			temp=plistptr->data[i-1];
			printf("Target(%d)’s previous number is:%d\n",target,temp);
			return;
			}
		if(plistptr->data[i]==target&&i==0){
			printf("Target(%d) does not have the previous number.\n",target);
			return;
		}
	}
	printf("Target(%d) does not belong the list.\n",target);
}

12、获得指定元素的前驱函数测试

void PriorElement_text(){
	int temparray[5]={3,6,2,8,50};
	printf("---- PriorElement_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	PriorElement(templist,3);
	PriorElement(templist,8);
	PriorElement(templist,10);
	printf("---- PriorElement_text ends. ----\n\n");
}

13、表的清空

void clearlist(Slistptr plistptr){
	plistptr->length=0;
}

14、返回变量地址

void outputMemory(Slistptr plistptr) {
    printf("The address of the structure: %ld\n", plistptr);
    printf("The address of actualLength: %ld\n", &plistptr->length);
    printf("The address of data: %ld\n", &plistptr->data);
    printf("The address of actual data: %ld\r\n", &plistptr->data[0]);
    printf("The address of second data: %ld\n\n", &plistptr->data[1]);
}

二、完整代码

代码如下:

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

#define list_max_length 10
 
 
typedef struct Slist{
	int length;
	int data[list_max_length];
}*Slistptr;

void outputlist(Slistptr plist){
	int i;
	for( i=0; i<plist->length; i++){
		printf("%d ",plist->data[i]);
	}
	printf("\n");
}    //打印顺序表

void outputMemory(Slistptr plistptr) {
    printf("The address of the structure: %ld\n", plistptr);
    printf("The address of actualLength: %ld\n", &plistptr->length);
    printf("The address of data: %ld\n", &plistptr->data);
    printf("The address of actual data: %ld\r\n", &plistptr->data[0]);
    printf("The address of second data: %ld\n\n", &plistptr->data[1]);
}     //返回变量地址

Slistptr slistInit(int pdata[],int plength){
	Slistptr resultptr=(Slistptr)malloc(sizeof(struct Slist));
	int i;
	for(i=0;i<plength;i++){
		resultptr->data[i]=pdata[i];
	}
	resultptr->length=plength;
	
	return resultptr;
}    //顺序表初始化

void SlistInsert(Slistptr plistptr,int Position,int value){
	//1、Space check. 
	if(plistptr->length>=list_max_length){
		printf("list is full.\n");
		return;
	}
	//2、Position check.
	if(Position<0){
		printf("Position is illegal.\n");
		return;
	}
	if(Position>plistptr->length){
		printf("The position is bigger than the list length.\n");
		return;
	}
	//3、Move the remaining part.
	int i;
	for(i=plistptr->length;i>Position;i--){
		plistptr->data[i]=plistptr->data[i-1];
	} 
	//4、Insert.
	plistptr->data[Position]=value;
	//5、Update the length.
	plistptr->length++;
}     //顺序表的插入

void InsertTest(){
	int i;
	int temparray[5]={1,4,3,6,8};
	
	printf("---- sequentialInsertTest begins. ----\n");
	//初始化 
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	
	//在第一个位置插入
	printf("After instering,the list is:");
	SlistInsert(templist,0,9);
	outputlist(templist);
	
	//在末尾插入
	printf("After instering,the list is:");
	SlistInsert(templist,6,7);
	outputlist(templist);
	
	//插入位置不在范围
	printf("After instering,the list is:");
	SlistInsert(templist,8,70);
	printf("The list is:"); 
	outputlist(templist);
	
	//连续插入三个 
	for(i=0;i<5;i++){
		printf("Inserting %d.\n",i+10);
		SlistInsert(templist,0,i+10);
		outputlist(templist);
	}
	
	printf("---- sequentialInsertTest ends. ----\n\n");
	outputMemory(templist);	
} 


int SlistDelete(Slistptr plistptr,int Position){
	//1、Position check.
	if(Position<0){
		printf("Invalid position.\n");
		return -1;
	} 
	if(Position>=plistptr->length){
		printf("The position is beyond the list length.\n");
		return -1;
	} 
	
	//2、Move the remaining part.
	int i;
	int temp=plistptr->data[Position];
	for(i=Position;i<plistptr->length;i++){
		plistptr->data[i]=plistptr->data[i+1];
	} 
	
	//3、Update the length.
	plistptr->length--;
	
	//4、Return the temp;
	return temp; 
}      //顺序表的删除

void DeleteTest(){
	int temparray[5]={3,6,2,8,50};
	
	printf("---- sequentialDeleteTest begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	
	//删除第一个
	printf("After deleting,the list is:");
	SlistDelete(templist,0);
	outputlist(templist);
	
	//删除最后一个
	printf("After deleting,the list is:");
	SlistDelete(templist,3);
	outputlist(templist);
	
	//删除第二个
	printf("After deleting,the list is:");
	SlistDelete(templist,1);
	outputlist(templist);
	
	//超出范围示例 
	printf("Now delete the 5th, the list is: ");
	SlistDelete(templist,5);
	printf("The list is:");
	outputlist(templist);
	
	printf("Now delete the -5th, the list is: ");
	SlistDelete(templist,-5);
	printf("The list is:");
	outputlist(templist);
	
	printf("---- sequentialDeleteTest ends. ----\n\n");
	outputMemory(templist);	
}

void Elementlocate(Slistptr plistptr,int value){
	int i;
	for(i=0;i<plistptr->length;i++)
	{
		if(plistptr->data[i]==value){
			printf("The value(%d)‘s location:%d\n",value,i+1);
			return;
		}
	}
	printf("The value(%d) does not exist in the list.\n",value);
}      //获得指定元素在表中的位置

void Elementlocate_text(){
	int temparray[5]={3,6,2,8,50};
	printf("---- Elementlocate_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	Elementlocate(templist,8);
	printf("---- Elementlocate_text ends. ----\n\n");
}

int getElement(Slistptr plistptr,int position){
	if(position<0){
		printf("Invalid position.\n");
		return -1;
	}
	if(position>=plistptr->length){
		printf("The position is beyond the list length.\n");
		return -1;
	}      //获得指定位置上的元素
	
	return plistptr->data[position-1];
}

void getElement_text(){
	int temparray[5]={3,6,2,8,50};
	int temp;
	printf("---- getElement_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	temp=getElement(templist,2);
	if(temp!=-1){
		printf("The position’s number is:%d\n",temp);
	}

	printf("---- getElement_text ends. ----\n\n");
}

void clearlist(Slistptr plistptr){
	plistptr->length=0;
}

void PriorElement(Slistptr plistptr,int target){
	int i,temp;
	for(i=0;i<plistptr->length;i++){
		if(plistptr->data[i]==target&&i!=0){
			temp=plistptr->data[i-1];
			printf("Target(%d)’s previous number is:%d\n",target,temp);
			return;
			}
		if(plistptr->data[i]==target&&i==0){
			printf("Target(%d) does not have the previous number.\n",target);
			return;
		}
	}
	printf("Target(%d) does not belong the list.\n",target);
}      //获得指定元素的前驱

void PriorElement_text(){
	int temparray[5]={3,6,2,8,50};
	printf("---- PriorElement_text begins. ----\n");
	//初始化
	Slistptr templist=slistInit(temparray,5);
	printf("The list is:");
	outputlist(templist);
	PriorElement(templist,3);
	PriorElement(templist,8);
	PriorElement(templist,10);
	printf("---- PriorElement_text ends. ----\n\n");
}
void main(){
	InsertTest();
	DeleteTest();
	Elementlocate_text();
	PriorElement_text();
	getElement_text();
}

运行结果:

---- sequentialInsertTest begins. ----
The list is:1 4 3 6 8
After instering,the list is:9 1 4 3 6 8
After instering,the list is:9 1 4 3 6 8 7
After instering,the list is:The position is bigger than the list length.
The list is:9 1 4 3 6 8 7
Inserting 10.
10 9 1 4 3 6 8 7
Inserting 11.
11 10 9 1 4 3 6 8 7
Inserting 12.
12 11 10 9 1 4 3 6 8 7
Inserting 13.
list is full.
12 11 10 9 1 4 3 6 8 7
Inserting 14.
list is full.
12 11 10 9 1 4 3 6 8 7
---- sequentialInsertTest ends. ----

The address of the structure: 13004192
The address of actualLength: 13004192
The address of data: 13004196
The address of actual data: 13004196
The address of second data: 13004200

---- sequentialDeleteTest begins. ----
The list is:3 6 2 8 50
After deleting,the list is:6 2 8 50
After deleting,the list is:6 2 8
After deleting,the list is:6 8
Now delete the 5th, the list is: The position is beyond the list length.
The list is:6 8
Now delete the -5th, the list is: Invalid position.
The list is:6 8
---- sequentialDeleteTest ends. ----

The address of the structure: 13004256
The address of actualLength: 13004256
The address of data: 13004260
The address of actual data: 13004260
The address of second data: 13004264

---- Elementlocate_text begins. ----
The list is:3 6 2 8 50
The value(8)‘s location:4
---- Elementlocate_text ends. ----

---- PriorElement_text begins. ----
The list is:3 6 2 8 50
Target(3) does not have the previous number.
Target(8)’s previous number is:2
Target(10) does not belong the list.
---- PriorElement_text ends. ----

---- getElement_text begins. ----
The list is:3 6 2 8 50
The position’s number is:6
---- getElement_text ends. ----
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值