线性表的顺序存储

1、线性表的顺序存储,用如下结构来表示

//  list.h 线性表的动态分配顺序存储结构
 #define LIST_INIT_SIZE 10 // 线性表存储空间的初始分配量
 #define LIST_INCREMENT 2 // 线性表存储空间的分配增量
 typedef struct
 {
   ElemType *elem; // 存储空间基址
   int length; // 当前长度
   int listsize; // 当前分配的存储容量(以sizeof(ElemType)为单位)
 }SqList;
 /

2、相关函数实现

 

/
//functions.h 相关的操作函数
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>//realloc函数
#include "list.h"

/
//初始化一个线性表
void initlist(SqList *L)
{
	 L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
	 if(!L->elem)
		 exit(0);
	 L->length=0;
	 L->listsize=LIST_INIT_SIZE;
}

//输出建立链表后的相关信息
void print_list(SqList *L)
{
	printf("初始化后的线性链表的相关信息:\n");
	printf("L->elem=%d,L->length=%d,L->listsize=%d\n",L->elem,L->length,L->listsize);
}

//返回线性链表的长度
int getlength(SqList *L)
{
	return L->length;
}

//在第i个位置之前插入新元素e(位置从1开始,1,2,3.....)
void insertlist(SqList *L,int i,ElemType e)
{
	ElemType *newbase,*q,*p;
	if(i<1 ||i>L->length+1)
	{
		printf("插入的位置溢出!");
		exit(0);
	}

	//检查分配的空间是否够用
	if(L->length >= L->listsize)
	{
		newbase=(ElemType *)realloc(L->elem,(LIST_INIT_SIZE+LIST_INCREMENT)*sizeof(ElemType));
		if(!newbase)
			exit(0);
		L->elem=newbase;
		L->listsize+=LIST_INCREMENT;
	}

	q=L->elem+i-1;
	for( p=L->elem+L->length-1 ; p>=q ; p-- )
	{
		*(p+1)=*p;
	}
	*q=e;
	++L->length;

}

//删除一个元素
int deletelist(SqList *L,int i,ElemType *e)
{
	ElemType *p,*q;

	if(i<1 || i>L->length)
		printf("删除的位置溢出!");

	p=L->elem+i-1;
	e=p;
	q=L->elem+L->length-1;//
	for(p=p+1 ; p<=q ; ++p)
		*(p-1)=*p;
	--L->length;
	return *e;
}

//给定一个元素,找到它的直接前驱
ElemType priorelem(SqList *L,ElemType cur_e,ElemType *prior_e)
{
	// 操作结果:若cur_e是L的数据元素,且不是第一个,则返回它的前驱,
    //          否则操作失败
	ElemType *p=L->elem+1;//指向第2个元素(第一个元素没有前驱)
	ElemType *q=L->elem+L->length-1;//指向最后1个元素

	//判断是否为第一个元素
	if(cur_e==*(L->elem))
	{
		printf("%d是链表第一个元素,没有前驱!",cur_e);
		exit(0);
	}

	//
	while( (*p!=cur_e) && (p<=q) )
	{
		p++;

	}
	if(p>q)
	{
		printf("%d不是链表中的元素\n",cur_e);
		exit(0);//必须加上这个语句来终止后面代码的执行
	}
	else
		prior_e=--p;//prior指向cur_e的前驱

	//返回前驱
	return *prior_e;
}

//给定一个元素,找到它的后继
ElemType nextelem(SqList *L,ElemType cur_e,ElemType *next_e)
{
	// 操作结果:若cur_e是L的数据元素,且不是最后一个,则返回它的后继,
    //          否则操作失败
	ElemType *p=L->elem;//指向第1个元素
	ElemType *q=L->elem+L->length-2;//指向倒数第二个元素(最后一个元素没有后继)

	
	//判断是否为最后一个元素
	if(cur_e==*(q+1))
	{
		printf("%d是链表的最后一个元素,没有后继!",cur_e);
		exit(0);
	}

	//找后继
	while( (*q!=cur_e) && (q>=p) )
	{
		q--;

	}
	if(q<p)
	{
		printf("%d不是链表中的元素\n",cur_e);
		exit(0);
	}
	else
		next_e=++q;
	return *next_e;
}



//输出每个元素
void show_list(SqList *L)
{
	int i,length=L->length;
	for(i=0;i<length;i++)
		printf("%d ",*(L->elem+i));
}


3、主函数

typedef int ElemType;
#include"functions.h"

int main()
{
	SqList L;
	int *value_delete=0;
	ElemType *prior=0,*next=0,prior_elem=0,next_elem=0;
	int element;
	int L_length;

	int i;
	initlist(&L);
	print_list(&L);

	L_length=getlength(&L);
	printf("线性链表的长度:%d\n",L_length);


	//插入一个元素	
	for(i=1;i<11;i++)
		insertlist(&L,1,i);
	insertlist(&L,5,0);


	//删除一个元素
	printf("删除的元素为:%d",deletelist(&L,5,value_delete));
	//deletelist(&L,5);
	print_list(&L);
	show_list(&L);


	//找前驱
	printf("\n");
	scanf("%d",&element);
	prior_elem=priorelem(&L,element,prior);
	printf("%d的前驱为:%d\n",element,prior_elem);


	//找后继
	printf("\n");
	scanf("%d",&element);
	next_elem=nextelem(&L,element,next);
	printf("%d的后继为:%d\n",element,next_elem);

	return 0;

}



 

线性表顺序存储是一种将线性表元素按照顺序存储在连续的内存空间中的方法。在Java中,可以使用数组来实现线性表顺序存储。通过数组,我们可以在常数时间内访问线性表中的任意元素,这是因为数组的元素在内存中是连续存储的。 以下是线性表顺序存储的基本操作的实现代码的一个例子: ```java public class MyLinearList { private int[] array; private int size; public MyLinearList(int capacity) { array = new int[capacity]; size = 0; } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public int getElement(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index is out of range"); } return array[index]; } public void addElement(int element) { if (size == array.length) { throw new IllegalStateException("Linear list is full"); } array[size] = element; size++; } public void deleteElement(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index is out of range"); } for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; } size--; } public void modifyElement(int index, int newElement) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index is out of range"); } array[index] = newElement; } } ``` 以上是一个简单的线性表顺序存储的实现代码示例。它包含了线性表的基本操作,包括获取线性表的大小、判断线性表是否为空、获取指定索引位置的元素、向线性表中添加元素、删除指定索引位置的元素以及修改指定索引位置的元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值