TOP

数据结构C线性表现实(二)
2019-09-03 03:40:35 】 浏览:55
Tags:数据结构 线性 现实

_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(lis  
		
数据结构C线性表现实(二) https://www.cppentry.com/bencandy.php?fid=45&id=249508

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C_常用快捷键 下一篇STM32F4 阿波罗寄存器点亮LED灯