数据结构C线性表现实
Posted gavinpan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构C线性表现实相关的知识,希望对你有一定的参考价值。
linearList.h
#ifndef _INC_STDIO_8787 #define _INC_STDIO_8787 #include <stdio.h> #include <malloc.h> #define LIST_INIT_SIZE 100 // 线性表存储空间的初始分配量 #define LIST_INCREMENT 10 // 线性表存储空间的分配增量 typedef int ElemType; // 数据元素的类型 typedef struct ElemType *elem; // 存储空间的集地址 int length; // 当前线性表的长度 int listsize; // 当前分配的存储容量 LinearList; int init_list(LinearList *list); //初始化线性表 void clear_list(LinearList *list); void destroy_list(LinearList* list); int list_empty(LinearList* list); int list_length(LinearList* list); void print_list(LinearList* list); int locate_elem(LinearList* list, ElemType* x); int prior_elem(LinearList* list, ElemType* cur_elem, ElemType* pre_elem); int get_elem(LinearList* list, int index, ElemType* e); int next_elem(LinearList* list, ElemType* cur_elem, ElemType* next_elem); int insert_elem(LinearList* list, int index, ElemType* e); int delete_elem(LinearList* list, int index, ElemType* e); int append_elem(LinearList* list,ElemType* e); int pop_elem(LinearList* list, ElemType* e); void union_list(LinearList* list_a, LinearList* list_b, LinearList* list_c); void intersect_list(LinearList* list_a, LinearList* list_b, LinearList* list_c); void except_list(LinearList* list_a,LinearList* list_b, LinearList* list_c); #endif
linearList.c
#include "linearList.h" int init_list(LinearList *list) list->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!list->elem) return -1; list->length = 0; list->listsize = LIST_INIT_SIZE; return 0; void clear_list(LinearList *list) list->length = 0; void destroy_list(LinearList* list) free(list); int list_empty(LinearList* list) return (list->length == 0); int list_length(LinearList* list) return list->length; int locate_elem(LinearList* list, ElemType* x) int pos = -1; int i; for (i = 0; i < list->length; i++) if (list->elem[i] == *x) pos = i; return pos; int prior_elem(LinearList* list, ElemType* cur_elem, ElemType* pre_elem) int pos = -1; pos = locate_elem(list, cur_elem); if(pos <= 0) return -1; *pre_elem = list->elem[pos-1]; return 0; int insert_elem(LinearList* list, int index, ElemType* e) ElemType *q, *p; if (index < 0 || index >= list->length) return -1; if (list->length >= list->listsize) ElemType *newbase = (ElemType*)realloc(list->elem, (list->listsize + LIST_INCREMENT)*sizeof(ElemType)); if (!newbase) return -1; list->elem = newbase; list->listsize += LIST_INCREMENT; q = &(list->elem[index]); for (p = &(list->elem[list->length-1]); p >= q; p--) *(p+1) = *p; *q = *e; ++list->length; return 0; int append_elem(LinearList* list,ElemType* e) if (list->length >= list->listsize) ElemType *newbase = (ElemType*)realloc(list->elem, (list->listsize + LIST_INCREMENT)*sizeof(int)); if (!newbase) return -1; list->elem = newbase; list->listsize += LIST_INCREMENT; list->elem[list->length] = *e; ++list->length; return 0; void print_list(LinearList* list) int i; for (i=0; i < list->length; i++) printf("%d \n", list->elem[i]); int get_elem(LinearList* list, int index, ElemType* e) if (index<0 || index >= list->length) return -1; *e = list->elem[index]; return 0; int next_elem(LinearList* list, ElemType* cur_elem, ElemType* next_elem) int pos = -1; pos = locate_elem(list, cur_elem); if(pos == -1 || pos == (list->length - 1)) return -1; *next_elem = list->elem[pos+1]; return 0; int delete_elem(LinearList* list, int index, ElemType* e) ElemType *q, *p; if (index < 0 || index >= list->length) return -1; p = &(list->elem[index]); *e = *p; q = list->elem + list->length -1; for (++p; p < q; ++p) *(p - 1) = *p; --list->length; return 0; int pop_elem(LinearList* list, ElemType* e) if (list_empty(list)) return -1; *e = list->elem[list->length - 1]; --list->length; return 0; void union_list(LinearList* list_a, LinearList* list_b, LinearList* list_c) //并集,C=A∪B int i,a_length,b_length; ElemType elem; a_length = list_length(list_a); b_length = list_length(list_b); for(i=0;i<a_length;i++) get_elem(list_a, i, &elem); append_elem(list_c,&elem); for(i=0;i<b_length;i++) get_elem(list_b, i, &elem); if(locate_elem(list_a, &elem) == -1) append_elem(list_c,&elem); void intersect_list(LinearList* list_a, LinearList* list_b, LinearList* list_c) //交集,C=A∩B int i,a_length; ElemType elem; a_length = list_length(list_a); for(i=0;i<a_length;i++) get_elem(list_a, i, &elem); if(locate_elem(list_b, &elem) != -1) append_elem(list_c,&elem); void except_list(LinearList* list_a,LinearList* list_b, LinearList* list_c) //差集,C=A-B(属于A而不属于B) int i,a_length; ElemType elem; a_length = list_length(list_a); for(i=0;i<a_length;i++) get_elem(list_a, i, &elem); if(locate_elem(list_b, &elem) == -1) append_elem(list_c,&elem);
index.c
#include "linearList.h" void main() int i; ElemType elem; LinearList *list_a = (LinearList *)malloc(sizeof(LinearList)); LinearList *list_b = (LinearList *)malloc(sizeof(LinearList)); LinearList *list_c = (LinearList *)malloc(sizeof(LinearList)); init_list(list_a); init_list(list_b); init_list(list_c); for (i = 0; i < 10; i++) append_elem(list_a,&i); for (i = 0; i < 20; i+=2) append_elem(list_b,&i); print_list(list_a); print_list(list_b); pop_elem(list_a, &elem); print_list(list_a); printf("pop: %d \n",elem); delete_elem(list_a, 2, &elem); print_list(list_a); printf("delete: %d \n",elem); insert_elem(list_a, 2, &elem); printf("insert: %d \n",elem); print_list(list_a); get_elem(list_a, 5, &elem); printf("get elem at 5: %d \n",elem); printf("locate : elem %d at %d \n",elem,locate_elem(list_a,&elem)); printf("list_a length : %d \n",list_length(list_a)); print_list(list_a); print_list(list_b); union_list(list_a,list_b,list_c); print_list(list_c); clear_list(list_c); intersect_list(list_a,list_b,list_c); print_list(list_c); clear_list(list_c); except_list(list_a,list_b,list_c); print_list(list_c); destroy_list(list_a); destroy_list(list_b); destroy_list(list_c);
以上是关于数据结构C线性表现实的主要内容,如果未能解决你的问题,请参考以下文章
C/C++语言数据结构快速入门(代码解析+内容解析)数据结构基本内容和线性表