C语言线性表的基本操作

#include<stdio.h>
#include<stdlib.h>

#define MAX 1000
#define TRUE 1
#define FALSE 0

typedef struct{
	int elem[MAX];
	int length;
}SeqList;

//线性表初始化,构造一个空的线性表L 
void InitList(SeqList *L){
	L->length=0;
}

//创建线性表
void CreatList(SeqList *L) {
	int x,n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&x);
		L->elem[i]=x;
		L->length++;
	}
}

//求线性表长度,返回L中数据元素个数
int ListLength(SeqList *L){
	return L->length;	
}

//用x返回线性表中第i个元素值
int GetElem(SeqList *L,int i){
	int x;
	x=L->elem[i];
	return x;
}

//按值查找,确定x在表中的位置
int LocationElem(SeqList *L,int x){
	for(int i=0;i<L->length;i++){
		if(x==L->elem[i]) {
			return i;
			break;
		}
	}
}

//插入操作,在i位置前插入一个新元素x,L长度加1
bool ListInsert(SeqList *L,int i,int x){
	if(L->length==MAX-1){
		printf("表满");
		return FALSE; 
	}
	if(i<1 || i>L->length+1){
		printf("位置错");
		return FALSE; 
	}
	for(int j=L->length;j>=i;j--)//循环到 L->length
		L->elem[j+1]=L->elem[j];
	L->elem[i]=x;
	L->length++;
	return TRUE;
}

//删除操作,删除第i个元素,长度减一
bool ListDelet(SeqList *L,int i){
	if(i<1 || i>L->length+1){
		printf("位置错");
		return FALSE; 
	}
	for(int j=i;j<L->length-1;j++){//循环到 L->length-1
		L->elem[j]=L->elem[j+1];
	}
	L->length--;
	return TRUE;
}

//判断空,空返回TRUE,非空返回FALSE
bool ListEmpty(SeqList *L){
	if(L->length==0) return TRUE;
	return FALSE;
}

//将线性表置空
void ClearList(SeqList *L){
	L->length=0;
}

//销毁线性表L
bool DestroyList(SeqList *L){
	if(L)
    {
        free(L);
        L->length=0;
        return TRUE;
    }
    else
        return FALSE;
}

//顺序表合并
void Merge(SeqList *A,SeqList *B,SeqList *L){
	int i=1,j=1,k=1;
	while(i<=A->length && j<=B->length){
		if(A->elem[i]<=B->elem[j])
			L->elem[k++]=A->elem[i++];
		else
			L->elem[k++]=B->elem[j++];
	}
	while(i<=A->length)
		L->elem[k++]=A->elem[i++];
	while(j<=B->length)
		L->elem[k++]=B->elem[j++];
	L->length=A->length+B->length;
}

main(){
	SeqList *L;
	L = (SeqList *)malloc(sizeof(SeqList));//用指针必须先申请空间 

	CreatList(L);

	int a=ListLength(L);
	printf("%d\n",a);
	
	int b=GetElem(L,3);
	printf("%d\n",b);
	
	int c=LocationElem(L,3);
	printf("%d\n",c);
}
以下是 C 语言线性表基本操作代码: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 // 线性表的最大长度 typedef int ElemType; // 定义线性表元素类型为整型 typedef struct { ElemType data[MAXSIZE]; // 存储线性表元素的数组 int length; // 线性表的当前长度 } SqList; // 顺序存储结构的线性表 // 初始化线性表 void InitList(SqList *L) { L->length = 0; // 线性表长度为0 } // 判断线性表是否为空 int ListEmpty(SqList L) { return L.length == 0; } // 获取线性表长度 int ListLength(SqList L) { return L.length; } // 获取线性表中指定位置的元素 int GetElem(SqList L, int i, ElemType *e) { if (i < 1 || i > L.length) { return 0; // i值不合法 } *e = L.data[i - 1]; // 将第i个元素的值赋给e return 1; } // 在线性表中查找指定元素的位置 int LocateElem(SqList L, ElemType e) { for (int i = 0; i < L.length; i++) { if (L.data[i] == e) { return i + 1; // 返回元素在线性表中的位置 } } return 0; // 没有找到元素 } // 在线性表中插入元素 int ListInsert(SqList *L, int i, ElemType e) { if (i < 1 || i > L->length + 1) { return 0; // i值不合法 } if (L->length >= MAXSIZE) { return 0; // 线性表已满 } for (int j = L->length; j >= i; j--) { L->data[j] = L->data[j - 1]; // 将第i个元素及之后的元素后移 } L->data[i - 1] = e; // 将新元素插入到第i个位置 L->length++; // 线性表长度加1 return 1; } // 在线性表中删除元素 int ListDelete(SqList *L, int i, ElemType *e) { if (i < 1 || i > L->length) { return 0; // i值不合法 } *e = L->data[i - 1]; // 将第i个元素的值赋给e for (int j = i; j < L->length; j++) { L->data[j - 1] = L->data[j]; // 将第i个元素之后的元素前移 } L->length--; // 线性表长度减1 return 1; } // 打印线性表中的所有元素 void PrintList(SqList L) { for (int i = 0; i < L.length; i++) { printf("%d ", L.data[i]); } printf("\n"); } // 测试线性表基本操作 int main() { SqList L; InitList(&L); ListInsert(&L, 1, 1); ListInsert(&L, 2, 2); ListInsert(&L, 3, 3); ListInsert(&L, 4, 4); ListInsert(&L, 5, 5); printf("线性表中的元素为:"); PrintList(L); int e; GetElem(L, 3, &e); printf("线性表中第3个元素为:%d\n", e); printf("元素2在线性表中的位置为:%d\n", LocateElem(L, 2)); ListDelete(&L, 4, &e); printf("删除线性表中第4个元素后,线性表中的元素为:"); PrintList(L); printf("线性表的长度为:%d\n", ListLength(L)); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值