线性顺序表-C语言实现

第一次用CSDN写东西 所以先来点简单的吧

本次,用C语言实现线性顺序表
当是复习C语言了吧 这次还是学到不少的
我有预感 大一时踩过的那些坑 大二还是会再踩一次…

  • main.h
// 导入必要的头文件
// Created by bing on 2019/10/16.
//
#ifndef DATA_STRUCTURE_MAIN_H
#define DATA_STRUCTURE_MAIN_H
#endif //DATA_STRUCTURE_MAIN_H
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
  • linear_list.h
// 实现各种功能
// Created by bing on 2019/10/16.
//

#ifndef DATA_STRUCTURE_LINEAR_LIST_H
#define DATA_STRUCTURE_LINEAR_LIST_H

#endif //DATA_STRUCTURE_LINEAR_LIST_H
#include "main.h"
#define TRUE 1
#define OK 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef struct{
	int id;
	char name[10];
	char sex[2];
	double score;
}Elemtype;
Status setter(Elemtype *tmp,int id_, char name_[10], char sex_[10], double score_) {
	tmp->id = id_;
	// 更安全的复制
//	strcpy_s(tmp->name,10, name_);
//	strcpy_s(tmp->sex,2, sex_);
	// 适用性更广的复制
	strcpy(tmp->name, name_);
	strcpy(tmp->sex, sex_);
	tmp->score = score_;
	return OK;
}
typedef struct {
	Elemtype *data;
	int length;
	int listsize;
}SqList;
// 定义对线性表的操作
Status elem_equal(Elemtype e1, Elemtype e2){
    if (e1.id==e2.id && e1.score == e2.score && (strcmp(e1.sex, e2.sex) == 0) && (strcmp(e1.name, e2.name) == 0)){
        return TRUE;
    }else{
        return FALSE;
    }
}
Status InitList(SqList *tmp) {
	tmp->data = (Elemtype*)malloc(LIST_INIT_SIZE * sizeof(Elemtype));
	if (!tmp->data) {
		exit(OVERFLOW);
	}
	tmp->length = 0;
	tmp->listsize = LIST_INIT_SIZE;
	return OK;
} // Init SqList
Status ListInsert_Sq(SqList* tmp, int pos, Elemtype *e) {
	// 在顺序线性表的第i个元素之前插入e
	// 判断pos的合法性
	if (pos < 0 || pos > tmp->length) {
		return ERROR;
	}
	// 是否扩容
	if (tmp->length >= tmp->listsize) {
	    // _Size 指的是最终申请到的空间大小
		Elemtype *newbase = (Elemtype*)realloc(tmp->data, (tmp->length+LISTINCREMENT) * sizeof(Elemtype));
		if (!newbase) {
			return OVERFLOW;
		}
		tmp->listsize += LISTINCREMENT;
		tmp->data = newbase;
	}
	// 插入 , pos之后的元素后移
	for (int i = tmp->length; i > pos; --i) {
		*(tmp->data + i + 1) = *(tmp->data + i);
	}
	tmp->data[pos] = *e;
	++tmp->length;
	return OK;
}
Status ListTraverse_Sq(SqList* tmp, int max){
    if (max>tmp->length){
        return ERROR;
    }
    for (int i = 0; i < max; ++i) {
		printf("%d, %s, %s, %.2lf\n", tmp->data[i].id, tmp->data[i].name, tmp->data[i].sex, tmp->data[i].score);
	}
    if (max<tmp->length){
	    printf("traversed %d element(s),totally %d element(s), max %d element(s)\n", max, tmp->length,tmp->listsize);
    }
    return OK;
}
Status ListDelete_Sq(SqList* tmp, int pos, Elemtype* e){
    if (pos<0 || pos>tmp->length-1){
        return ERROR;
    }
    // 取出元素
    *e = tmp->data[pos];
    // 移动元素
    for (int i = pos; i < tmp->length - 1; ++i) {
		*(tmp->data + i ) = *(tmp->data + i + 1);
	}
	--tmp->length;
	return OK;
}
Status DestroyList_Sq(SqList* tmp){
    free(tmp->data);
    tmp->length=0;
    tmp->listsize=0;
    return OK;
}
Status ClearList_Sq(SqList* tmp){
    tmp->length=0;
    // 需要恢复到最小空间吗? 感觉没有必要呢
    // 事实上 到此刻,表的逻辑清空已完成
    return OK;
}
Status ListEmpty(SqList* tmp){
    if(tmp->length==0){
        return TRUE;
    }else{
        return FALSE;
    }
}
int ListLength(SqList* tmp){
    return tmp->length;
}
Status GetElem(SqList* tmp, int i, Elemtype* e){
    if (i <0 || i> tmp->length){
        return ERROR;
    }else{
        *e = tmp->data[i];
        return OK;
    }
}
int LocateElem(SqList* tmp, Elemtype* e, Status (*compare)(Elemtype, Elemtype)){
    for(int i=0;i<tmp->length;++i){
        if ((*compare)(tmp->data[i], *e)==TRUE){
            return i;
        }
    }
    return OVERFLOW;
}

Status PriorElem(SqList *tmp, Elemtype cur_e, Elemtype *e){
    int index = LocateElem(tmp, &cur_e, elem_equal);
    printf("index=%d\n", index);
    if(index!=OVERFLOW){
        if (index==0){
            // 0 号元素无前驱
            printf("1\n");
            return OVERFLOW;
        }else{
            *e = tmp->data[index-1];
            return OK;
        }
    }else{
        printf("91\n");
        return ERROR;
    }
}
Status NextElem(SqList *tmp, Elemtype cur_e, Elemtype *e){
    int index = LocateElem(tmp, &cur_e, elem_equal);
    if(index!=OVERFLOW){
        if (index==tmp->length){
            // 末尾元素无后继
            return OVERFLOW;
        }else{
            *e = tmp->data[index+1];
            return OK;
        }
    }else{
        return ERROR;
    }
}
  • main.c
#include "linear_list.h"


int main(int argc, char* argv[]) {
	SqList *L = (SqList*)malloc(sizeof(SqList));
	if (!L) {
		exit(OVERFLOW);
	}
	InitList(L);
	for (int i = 1; i <= 150; i++) {
		Elemtype* tmp = (Elemtype*)malloc(sizeof(Elemtype));
		if (!tmp) {
			exit(OVERFLOW);
		}
		setter(tmp, i, "12", "1", i+60);
		ListInsert_Sq(L, i-1, tmp);
	}
	Elemtype * tmp = (Elemtype*) malloc(sizeof(Elemtype));
	if (ListDelete_Sq(L, 8, tmp)){
	    printf("OK(Delete)!\n");
	}
	ListTraverse_Sq(L, 10);
	Elemtype *e = (Elemtype*)malloc(sizeof(Elemtype));
    GetElem(L,1,e);
    printf("the id of DATA[1] is %d,\n", e->id);
    if(PriorElem(L, *e, e))
        printf("the id of e's prior element=%d\n", e->id);
    else{
        printf("Unable to find the prior element of e or e has been the first element.\n");
    }
    if(NextElem(L, *e, e)){
        NextElem(L, *e, e);
        printf("the id of e's next element=%d\n",e->id);
    }else{
        printf("Unable to find the next element of e or e has been the last element.\n");
    }
	if (ClearList_Sq(L)){
	    if (ListEmpty(L)==0)
	        printf("Unable to clear the list!\n");
	    else
	        printf("OK!\n");
	}
	if (DestroyList_Sq(L)){
	    printf("Destroyed!\n");
	}
	return 0;
}

初次见面 请多关照
发现错误或不当的地方 还请不吝赐教

  • 简单的技术总结
    • strcmp(str1, str2)返回值为0时, 说明两者相同,反之,说明两者不同
    • VS直接放到Clion里的环境是不能用于调试的,这时,使用WSL+GCC+GDB是个不错的选择。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值