• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++线性表实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

List.h 

#pragma once
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#define LIST_INIT_SIZE 100
#define MAXSIZE 100

typedef int ElemType;

typedef struct Sqlist {
    ElemType * elem;
    int length;
    int listsize;
}Sqlist, *ListPtr;

typedef enum Status {
    success, fail, fatal, rangeerror,overflow
}Status;

Status List_Init(ListPtr L);

void List_Destroy(ListPtr L);

void List_Clear(ListPtr L);

bool List_Empty(ListPtr L);

int List_Size(ListPtr L);

Status List_Retrieve(ListPtr L, int pos, ElemType * elem);

Status List_Locate(ListPtr L, ElemType elem, int *pos);

Status List_Insert(ListPtr L, int pos, ElemType elem);

Status List_Remove(ListPtr L, int pos);

Status List_Prior(ListPtr L, int pos, ElemType * elem);

 

线性表.cpp

#include "stdafx.h"
#include<iostream>
#include "List.h"
using namespace std;

Status List_Retrieve(ListPtr L, int pos, ElemType * elem) {
    Status status = rangeerror;
    int len = L->length;
    if (1 <= pos&&pos <= len) {
        *elem = L->elem[pos];
        status = success;
    }
    return status;
}

Status List_Locate(ListPtr L, ElemType elem, int * pos) {
    Status status = rangeerror;
    int len = L->length;
    int i = 1;
    while (i <= len && L->elem[i]==elem) {
        i++;
    }
    if (i <= len) {
        *pos = i;
        status = success;
    }
    return status;
}

Status List_Insert(ListPtr L, int pos, ElemType elem) {
    Status status = rangeerror;
    int len = L->length,i;
    if (len > MAXSIZE)status = overflow;
    else if (1 <= pos && pos <= len + 1) {
        for (i = len; i >= pos; i--)
            L->elem[i + 1] = elem;
        L->elem[pos] = elem;
        L->length++;
        status = success;
    }
    return status;
}

Status List_Init(ListPtr L) {
    Status status = fatal;
    L->elem = (ElemType*)malloc((MAXSIZE + 1) * sizeof(ElemType));
    if (L->elem) {
        L->length = 0;
        status = success;
    }
    return status;
}

void List_Destroy(ListPtr L) {
    if (L->elem) {
        free(L->elem);
        L->elem = NULL;
    }
    L->length = 0;
}

void List_Clear(ListPtr L) {
    L->length = 0;
}

bool List_Empty(ListPtr L) {
    return L->length == 0;
}

Status List_Prior(ListPtr L, int pos, ElemType * elem) {
    Status status = fail;
    int len = L->length;
    if (2 <= pos && pos <= len) {
        *elem = L->elem[pos - 1];
        status = success;
    }
    return status;
}

Status List_Next(ListPtr L, int pos, ElemType * elem) {
    Status status;
    int len = L->length;
    if (1 <= pos && pos <= len - 1) {
        *elem = L->elem[pos+1];
        status = success;
    }
    return status;
}

int List_Size(ListPtr L) {
    return L->length;
}

Status List_Remove(ListPtr L, int pos) {
    Status status = rangeerror;
    int len = L->length, i;
    if (1 <= pos && pos <= len) {
        for (i = pos; i < len; i++) 
            L->elem[i] = L->elem[i + 1];
        L->length--;
        status = success;
    }
    return status;
}


Status List_Union(ListPtr La, ListPtr Lb) {
    ElemType elem;
    Status status= fail;
    int i, len = List_Size(Lb);
    for (i = 1; i <= len; i++) {
        List_Retrieve(Lb, i, &elem);
        if (status != success) {
            status = List_Insert(La, 1, elem);
            if (status != success)break;
        }
    }
    return status;
}



Status List_Merge(ListPtr La, ListPtr Lb, ListPtr Lc) {
    ElemType elem1, elem2;
    Status status=fail;
    status = List_Init(Lc);
    if(status != success)return status;
    int i = 1, j = 1, k = 1;
    int n = List_Size(La), m = List_Size(Lb);
    while (i <= n && j <= m) {
        List_Retrieve(La, i, &elem1), List_Retrieve(Lb, j, &elem2);
        if (elem1 < elem2) {
            status = List_Insert(Lc, k, elem1);
            i++;
        }
        else {
            status = List_Insert(Lc, k, elem2);
            j++;
        }
        if (status != success) return status;
        k++;
    }
    while (i <= n) {
        List_Retrieve(La, i, &elem1);
        status = List_Insert(Lc, k, elem1);
        if (status != success) return status;
        i++, k++;
    }
    while (j <= m) {
        List_Retrieve(Lb, j, &elem2);
        status = List_Insert(Lc, k, elem2);
        if (status != success) return status;
        j++, k++;
    }
    return status;
}


int main() {
    ListPtr La = new Sqlist, Lb = new Sqlist, Lc = new Sqlist;;
    List_Init(La);
    List_Init(Lb);
    List_Init(Lc);
    int arra[5] = { 2,4,6,7,8 };
    for (int i = 1; i <= 5; i++)
        List_Insert(La, i, arra[i-1]);
    
    for(int i=1;i<=5;i++)
        cout << La->elem[i] << " ";
    cout << endl;

    int arrb[5] = { 1,5,9,10,11 };
    for (int i = 1; i <= 5; i++)
        List_Insert(Lb, i, arrb[i - 1]);

    for (int i = 1; i <= 5; i++)
        cout << Lb->elem[i] << " ";
    cout<< endl;

    Status status = List_Merge(La, Lb, Lc);
    cout << status << endl;
    if (status != success)return EXIT_FAILURE;

    for (int i = 1; i <= 10; i++)
        cout << Lc->elem[i] << " ";
    cout << endl;


    system("pause");
    return EXIT_SUCCESS;
}

 

参考《数据结构与算法》 林劼

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#在一段数字区间内随机生成若干个互不相同的随机数发布时间:2022-07-18
下一篇:
C#DataGridView控件禁止拷贝数据发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap