zoukankan      html  css  js  c++  java
  • 顺序表(C实现代码)SeqList头文件

    #ifndef SeqList_hpp
    #define SeqList_hpp
    #define MAXLEN 100
    #define OK 1
    #define ERROR 0
    #include <memory.h>
    #include <stdio.h>
    typedef int Status;
    typedef int ElemType;
    typedef struct{
        ElemType data[100];
        int length = 0;
    }SeqList;//顺序表
    //顺序表函数声明(构造/初始化->读取->查找->插入->删除->清空)
    void InitList(SeqList *);
    void CreateList(SeqList *, int);
    int GetLength(SeqList *);
    Status GetElem(SeqList *, int , ElemType *); //按序号查找第i个元素(从0开始标序)
    int Locate(SeqList *, ElemType );
    int InsertElem(SeqList *, int , ElemType );
    Status DeleteAt(SeqList *,int );
    Status DeleteElem(SeqList *,ElemType );
    void Erase(SeqList *);
    void Print(SeqList *);
    #endif /* SeqList_hpp */
    #include "SeqList.hpp"
    void InitList(SeqList *L){L->length = 0;}
    void CreateList(SeqList *L, int n){
        int i;
        if(n>MAXLEN) {printf("OVERFLOW!
    ");return;}
        printf("请输入%d个元素构建线性表:",n);
        for(i = 0;i < n;i++)
            scanf("%d",&L->data[i]);
        L->length = n;
        return;
    }
    int GetLength(SeqList *L){return L->length;}
    Status GetElem(SeqList *L, int i, ElemType *x) //按序号查找第i个元素(从0开始标序)
    {
        if(i<0||i>=L->length) {printf("OVERFLOW!
    ");return ERROR;}
        else {*x = L->data[i]; return OK;}
    }//O(1)
    int Locate(SeqList *L, ElemType x){ //按值查找返回序号,返回-1表示未找到
        int i;
        for(i=0;i<L->length;i++)
            if(L->data[i]==x) return i;
        return -1;
    }//O(n)
    int InsertElem(SeqList *L, int i, ElemType x)//按位插入
    {
        int j;
        if(L->length>=MAXLEN){printf("顺序表已满!
    "); return ERROR;}
        if(i<0||i>L->length){printf("插入位置出错!
    "); return ERROR;}
        if(i == L->length){
            L->data[i] = x;
            L->length++;
            return OK;
        }
        for(j=L->length;j>i;j--)
            L->data[j] =L->data[j-1];
        L->data[i] = x;
        L->length++;
        return OK;
    } //O(n)
    Status DeleteAt(SeqList *L, int index){//删除位置错误返回0,否则返回1
        if(index<0||index>=L->length) {printf("删除位置错误!
    ");return ERROR;}
        if(index == L->length-1){L->data[index] = L->length = 0;return OK;}
        for(int i = index;i<L->length-1;i++)
            L->data[i] = L->data[i+1];
        L->length--;
        return OK;
    }
    Status DeleteElem(SeqList *L, ElemType x){
        int i;
        int t=0;
        for(i=0;i<L->length;i++){
            if(L->data[i]==x) {t=1;DeleteAt(L,i);L->length--;}
        }
        return t;
    }
    void Erase(SeqList *L){memset(L,0,sizeof(L));};
    void Print(SeqList *L){
        int i;
        for(i = 0; i < L->length; i++)
            printf("%d, ",L->data[i]);
        printf("
    ");
    }

    代码测试:

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include "SeqList.hpp" //包含SeqList
    using namespace std;
    int main()
    {
        SeqList L;
        InitList(&L);
        CreateList(&L, 5);
        Print(&L);
        InsertElem(&L, 3, 0);
        printf("插入后:");
        Print(&L);
        DeleteAt(&L, 2);
        printf("删除后:");
        Print(&L);
        int e;
        GetElem(&L, 2, &e);
        printf("index = 2 ,ELem = %d
    ",e);
        printf("0的位置为%d.
    ",Locate(&L, 0));
        InsertElem(&L,0,7);
        printf("插入后:");
        Print(&L);
        Erase(&L);
        //Print(&L);
        printf("清空后,长度为:%d
    ",GetLength(&L));
    }

    测试结果:

    请输入5个元素构建线性表:1 2 3 4 5

    1, 2, 3, 4, 5, 

    插入后:1, 2, 3, 0, 4, 5, 

    删除后:1, 2, 0, 4, 5, 

    index = 2 ,ELem = 0

    0的位置为2.

    插入后:7, 1, 2, 0, 4, 5, 

    清空后,长度为:0

  • 相关阅读:
    TSQL入门(msdn)
    在代码中,获取Entity Framework生成的TSQL查询语句
    Code First(一)
    UDPClient的用法
    Building Applications that Can Talk(转)
    Asynchronous Web and Network Calls on the Client in WPF(摘录)
    DropBox能正常使用了
    显示GIF图标报错:“A generic error occurred in GDI+.”
    第 2 篇Scrum 冲刺博客
    第 1 篇 Scrum 冲刺博客
  • 原文地址:https://www.cnblogs.com/raiuny/p/12586660.html
Copyright © 2011-2022 走看看