數據結構C線性表現實

来源:https://www.cnblogs.com/gavinpan/archive/2019/08/23/11385602.html
-Advertisement-
Play Games

linearList.h linearList.c index.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);
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 這次分享我們就來談談單例模式的使用,其實在本公眾號設計模式的第一篇分享就是單例模式,為什麼又要討論單例模式了?主要是那篇文章談的比較淺,只對單例模式的主要思想做了一個分享,這篇文章會從多個方面去分享單例模式的使用,下麵進入正題。 使用Java做程式的小伙伴都知道單例,尤其是使用spring框架做項目 ...
  • 本文將介紹微服務架構和相關的組件,介紹他們是什麼以及為什麼要使用微服務架構和這些組件。本文側重於簡明地表達微服務架構的全局圖景,因此不會涉及具體如何使用組件等細節。 為了防止不提供原網址的轉載,特在這裡加上原文鏈接: "https://www.cnblogs.com/skabyy/p/1139657 ...
  • 代碼參考:https://github.com/HCJ shadow/Zuul Gateway Cluster Nginx Zuul的路由轉發功能 前期準備 搭建Eureka服務註冊中心 服務提供者msc provider 5001【提供一個hello請求做測試】 創建gateway 7001 p ...
  • 一、複習 1.標識符(自己定義的,下劃線、美元符號) 2.駝峰命名(變數名,方法名首字母小寫) 3.關鍵字(就是固定的那幾個) 4.字面值(數據、有類型、八種基本類型從小到大,byte\char=short\int\long\float\double\boolean 5.成員變數(初始化在方法外且不 ...
  • 23:59 2019/8/23 成員變數: 對象的成員變數,或者普通成員變數; 類的成員變數,或者靜態成員變數 下麵是source code和.class->.java(反編譯後)的source code ...
  • 功能需求:在前端頁面中,for迴圈id會構不成連續的順序號,所以要找到一種偽列的方式來根據數據量定義序號 因此就用到了在前端頁面中的一個欄位 forloop.counter,完美解決 ...
  • 集合系列(一):集合框架概述 Java 集合是 Java API 用得最頻繁的一類,掌握 Java 集合的原理以及繼承結構非常有必要。總的來說,Java 容器可以劃分為 4 個部分: List 集合 Set 集合 Queue 集合 Map 集合 除了上面 4 種集合之外,還有一個專門的工具類: 工具 ...
  • 正則表達式: 它是字元串的一種匹配模式,用來處理字元串,可以極大地減輕處理一些複雜字元串的代碼量 字元組:它是在同一位置可能出現的各種字元組成了一個字元組,用[]表示,但是它的結果只能是一個數字或者一個大寫字母或小寫字母等 下麵測試以該網站為例http://tool.chinaz.com/regex ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...