数据结构:顺序表

该代码示例展示了如何在C语言中使用结构体创建顺序表,并实现了顺序表的初始化、在指定位置插入元素和删除元素的功能。通过linklistInit函数初始化顺序表,linklistInsert函数插入元素,linklistDelete函数删除元素,同时提供了测试用例来验证这些操作的正确性。
摘要由CSDN通过智能技术生成

今天学习了数据结构顺序表,现要求实现顺序表的建立以及简单的操作,代码如下:

#include<stdio.h>
#include<malloc.h>
#define NUMBER 10
typedef struct linklist{
	int length;
	int data[NUMBER];
}*linklistptr;
//定义结构体指针 
void outputList(linklistptr head){
	int i;
	for(i=0;i<head->length;i++){
		printf("%d ",head->data[i]);
	}
	printf("\r\n");
}//输出数据 
void outputMemory(linklistptr head){
	printf("The address of the structure: %ld\r\n",head);
	printf("The address of length: %ld\r\n",&head->length);
	printf("The address of data: %ld\r\n",&head->data);
	printf("The address of first data: %ld\r\n",&head->data[0]);
	printf("The address of second data: %ld\r\n",&head->data[1]);	
}//输出地址 
linklistptr linklistInit(int Data[],int Length){
	linklistptr x=(linklistptr)malloc(sizeof(struct linklist));
	int i;
	for(i=0;i<Length;i++){
		x->data[i]=Data[i];
	}
	x->length=Length;
	return x;
}//初始化顺序表 
void linklistInsert(linklistptr head,int position,int Data){
	if(head->length>=NUMBER){
		printf("Cannot insert element: list full.\r\n");
		return;
	}
	if(position<0){
		printf("Cannot insert element: negative position unsupported.");
		return;
	}
	if(position>head->length){
		printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n",position,head->length);
		return;
	}
	int i;
	for(i=head->length;i>position;i--){
		head->data[i]=head->data[i-1];
	}
	head->data[position]=Data;
	head->length++;
} //将数据添加到指定位置 
void linklistInsertTest(){
	int i;
	int simple[5]={3,5,2,7,4};
	printf("---- Insert Test begins.----\r\n");
	linklistptr tempList=linklistInit(simple,5);
	printf("After initialization,the list is: ");
	outputList(tempList);
	
	printf("Now insert to the first, the last is: ");
	linklistInsert(tempList,0,8);
	outputList(tempList); 
	
	printf("Now insert to the last,the list is: ");
	linklistInsert(tempList,6,9);
	outputList(tempList);
	
	printf("Now insert beyond the tail.\r\n");
	linklistInsert(tempList,8,9); 
	printf("The list is:");
	outputList(tempList);
	
	for(i=0;i<5;i++){
		printf("Inserting %d.\r\n",i+10);
		linklistInsert(tempList,0,i+10);
		outputList(tempList);
	}  
	printf("--- Insert Test ends.---\r\n");
}//测试样例 
int linklistDelete(linklistptr head,int position){
	if(position<0){
		printf("Invalid position: %d.\r\n",position);
		return -1;
	}
	if(position>=head->length){
		printf("Cannot dalete element: the position %d is beyond the list length %d.\r\n",position,head->length);
		return -1;
	}
	int Data=head->data[position],i;
	for(i=position;i<head->length;i++){
		head->data[i]=head->data[i+1];
	}
	head->length--;
	return Data;
}//将指定位置的数据删除 
void linklistDeleteTest(){
	int i;
	int simple[5]={3,5,2,7,4};
	printf("---- Delete Test begins.----\r\n");
	linklistptr tempList=linklistInit(simple,5);
	printf("After initialization,the list is: ");
	outputList(tempList);
	
	printf("Now delete the first, the last is: ");
	linklistDelete(tempList,0);
	outputList(tempList);
	
	printf("Now delete the last,the list is: ");
	linklistDelete(tempList,3);
	outputList(tempList);
	
	printf("Now delete the second,the list is:");
	linklistDelete(tempList,1);
	outputList(tempList);
	
	printf("Now delete the 5th,the list is:");
	linklistDelete(tempList,5);
	outputList(tempList);
	
	printf("Now delete the (-6)th,the list is:");
	linklistDelete(tempList,-6);
	outputList(tempList);
	
	printf("--- Delete Test ends.---\r\n");
	
	outputMemory(tempList);
}//测试样例 
/*int locata(linklistptr head,int Data){
	int i;
	for(i=0;i<head->length;i++){
		if(head->data[i]==Data){
			return i;
		}
	}
	return -1;
} 
int get(linklistptr head,int position){
	if(position<0){
		printf("Invalid position: %d.\r\n",position);
		return -1;
	}
	if(position>=head->length){
		printf("Cannot dalete element: the position %d is beyond the list length %d.\r\n",position,head->length);
		return -1;
	}
	return head->data[position]; 
}
void clear(linklistptr head){
	head->length=0;
}
*/

void main(){
	linklistInsertTest();
	linklistDeleteTest();
} //主函数 

代码运行结果如下:

 以上就是顺序表的建立以及对它进行的简单操作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值