数据结构(一)线性表-线性表的顺序表示 顺序表 C语言实现

目录

1.2.1 顺序表的定义

1.2.2 顺序表上基本操作的实现

 (1)顺序表的建立

 (2)顺序表元素的插入

 (3)顺序表元素的删除

 (4)顺序表的查找



1.2.1 顺序表的定义

 定义:顺序表是用一组地址连续的存储单元依次存储线性表中的数据元素,从而使得逻辑上相邻的两个元素在物理位置上也相邻。

 特点

  • 表中元素的逻辑顺序与物理顺序相同。
  • 随机访问,即通过首地址和元素序号可在时间O(1)内找到指定的元素。
  • 存储密度高,每个节点只存储数据元素。
  • 逻辑上相邻的元素物理上也相邻,所以插入和删除操作需要移动大量元素。

1.2.2 顺序表上基本操作的实现

 (1)顺序表的建立

// 头文件SeqList.h开始
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define  OK  1
#define  ERROR 0
#define  OVERFLOW  0
#define  LIST_INIT_SIZE  100                     	//  存储空间初始分配量
#define  LISTINCREMENT 10 //存储空间分配增量
typedef  struct
{
	int *elem;
	int  length;   //当前表长
	int listsize;                       	//  当前分配的存储容量

}SeqList;

int  InitList_Seq( SeqList  *L )                   //  构造一个空的顺序表
{
	L->elem = (int *)malloc(LIST_INIT_SIZE*sizeof (int));
	if (!L->elem)
	{ printf("Out of space!!\n");    	         //  存储分配失败
        exit(OVERFLOW);
	}
	L->length = 0;
	L->listsize = LIST_INIT_SIZE;
	return OK;
}

int Output_SeqList(SeqList  L)
{
     int i;
     for(i=0;i<L.length;i++)
	   printf ("%4d",L.elem[i]);
	 printf("\n");
     return OK;
}
// 头文件SeqList.h结束
// 主程序文件 CreateSeqList.c
int CreateList_Seq(SeqList L)
{//  建立顺序表
	int i;
	printf ("Input the datas: ");
	for(i=0;i<L.length;i++)
		scanf ("%d",&L.elem[i]);
	return OK;
}

void  main( )
{
	int n;
	SeqList L;
	InitList_Seq (&L);
	printf ("\n Input the length of the list L: ");	// 输入顺序表的长度
	scanf("%d",&n);
	L.length = n;
	CreateList_Seq (L);
	printf ("Output the datas: "); 					// 输出顺序表
	Output_SeqList(L);
	printf("\n");
	printf("the lenth of the List L: %d",L.length); 
}

 运行结果:

 (2)顺序表元素的插入

#include "SeqList.h"

int CreateList_Seq(SeqList L)                              //建立顺序表
{
	int i;

	printf("Input the datas: ");
	for(i=0;i<L.length;i++)
		scanf("%d",&L.elem[i]);
	return OK;
}

int ListInsert_Seq(SeqList *L,int i,int e)           在第 i 个元素之前插入元素至顺序表
{
	int *p,*q,*newbase;

	if((i<1)||(i>L->length+1))
		return ERROR;
	if(L->length>=L->listsize)                       // 当前空间已满,增加分配
	{
		newbase = (int *)realloc(L->elem,(L->length+LISTINCREMENT)*sizeof(int));
		if(!newbase)
			exit(OVERFLOW);
		L->elem = newbase;
		L->listsize += LISTINCREMENT;
	}
	
	q = &L->elem[i-1];	                     //  q 指向插入位置
	for(p=&L->elem[L->length-1];p>=q;p--)    // 第 n 至第 i 个元素依次后移一个位置
		*(p+1) = *p;
	*q = e;					                 // 插入新元素 e
	L->length++;                             /// 注意!要传值回去,形参L 必须用指针变量
    return OK;
}

int main()		//在顺序表的第i个位置插入元素e 
{
	int i,n,e;
	SeqList L;
	InitList_Seq(&L);
	printf("\nInput the length of the list L: ");
	scanf("%d",&n);
	L.length = n;
	CreateList_Seq(L);
    printf("Input the insert data: ");
	scanf("%d",&e);
	printf("Input the insert location: ");
	scanf("%d",&i);
	if(ListInsert_Seq(&L,i,e))
	{
		printf("Output the datas:");
		Output_SeqList(L);
	}
	else
		printf("Can't insert the data!\n");
	
	return 0;
}

运行结果:

 (3)顺序表元素的删除

  • 删除表中第i个元素并用变量*e返回其值。
#include "SeqList.h"
int CreateList_Seq(SeqList L)
{
	int i;
	printf("Input the datas: ");
	for(i=0;i<L.length;i++)
		scanf("%d",&L.elem[i]);
	return OK;
}

int ListDelete_Seq(SeqList *L, int i, int *e)  
{
	int *p,*q;
	if((i<1)||(i>L->length))
		return ERROR;
	p = &L->elem[i-1];p指向要删除元素的地址 
//	printf("p: %d",*p);
//	printf("\n"); 
	*e = *p;
	q = L->elem + L->length-1;令q指向最后一个元素的地址 
	for(++p; p<=q; p++)
		*(p-1) = *p;
	L->length--;
	return OK;
}

void main()
{
	int i,n,e;
	SeqList L;
	InitList_Seq(&L); 
	printf("\nInput the length of the list L: ");
	   scanf("%d",&n);
	L.length = n;
	CreateList_Seq(L);
	printf("Input the delete location: ");
	   scanf("%d",&i);
	if(ListDelete_Seq(&L,i,&e))
	{	printf("Delete the data %d:\n",e);
		printf("Output the datas: ");
		Output_SeqList(L);
	}
	else
		printf("Can't find the delete data!\n");

}

运行结果:

 (4)顺序表的查找

#include "SeqList.h"

int CreateList_Seq(SeqList L)
{
	int i;

	printf("Input the datas: ");
	for(i=0;i<L.length;i++)
		scanf("%d",&L.elem[i]);
	return OK;
}

int LocateElem(SeqList L,int e)
{
	int i;
	i = 0;
	while(i<=L.length-1 && L.elem[i] != e)
		++i;
	if(i<L.length)
		return i;
	else
		return -1;
}

void main()
{
	int i,n,e;
	SeqList L;
	InitList_Seq(&L);

	printf("\nInput the length of the list L: ");
	   scanf("%d",&n);
	L.length = n;
	CreateList_Seq(L);
	printf("Input the search data: ");
	scanf("%d",&e);
	if((i=LocateElem(L,e))>=0)
	   printf("The search data subscript is %d in the L\n",i);
	else
	   printf("There is no search data in the L!\n");
	printf("Output the datas: ");
	Output_SeqList(L);
}

运行结果: 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值