美文网首页
线性表之顺序表实现

线性表之顺序表实现

作者: 温柔倾怀 | 来源:发表于2019-09-29 08:29 被阅读0次

初始化顺序表

  • main.c
#include <stdio.h>
#include "SeqList.h"
/*
 * 线性表之顺序表实现
 *
 * */
int main() {
    printf("Hello, World!\n");
    SeqList mylist;  //定义一个顺序表
    InitSeqList(&mylist);   //因为要对结构有所改变,使用地址的形式进行传递

    return 0;
}
  • SeqList.h
//
// Created by wenrou on 2019-09-20.
//
/*
 * 结构数据函数的声明
 *
 * */

#ifndef XIANSHUNXUBIAO_SEQLIST_H
#define XIANSHUNXUBIAO_SEQLIST_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define SEQLIST_INIT_SIZE 8

typedef int ElemType;

typedef struct SeqList{
    ElemType *base; //指向一个真实所开辟的顺序表空间
    int capacity;   //容量
    int size;   //表的长度大小
}SeqList;

void InitSeqList(SeqList *list);

#endif //XIANSHUNXUBIAO_SEQLIST_H
  • SeqList.c
//
// Created by wenrou on 2019-09-20.
//
#include "SeqList.h"

void InitSeqList(SeqList *list){
    /*
     * 初始化和外部就是为结构体各元素进行初始化
     *      对base进行开辟空间
     *      对容量和大小进行填充
     * */
    list->base = (ElemType *)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
    assert(list->base != NULL);
    list->capacity=SEQLIST_INIT_SIZE;
    list->size=0;
}

功能实现

  • main.c
#include <stdio.h>
#include "SeqList.h"
/*
 * 线性表之顺序表实现
 *
 * */
int main() {
    printf("Hello, World!\n");
    SeqList mylist;  //定义一个顺序表
    InitSeqList(&mylist);   //因为要对结构有所改变,使用地址的形式进行传递

    //功能菜单
    int pos=0,len = 0;
    ElemType Item;
    int select=1;
    while(select){
        printf("***********************************\n");
        printf("* [1] push_back   [2] push_front  *\n");
        printf("* [3] show_list   [4] pop_back    *\n");
        printf("* [5] pop_front   [6] insert_pos  *\n");
        printf("* [7] find        [8] length      *\n");
        printf("* [9] delete_pos  [10] delete_val *\n");
        printf("* [11] sort       [12] resver     *\n");
        printf("* [13] clear      [14] destory    *\n");
        printf("*          [0] exit_system        *\n");
        printf("***********************************\n");
        printf("请选择你要进行的操作: ");
        scanf("%d",&select);
        if (select==0){
            break;
        }
        switch (select){
//            case 0:
//                break;
            case 1:
//                push_back
                printf("请输入您要插入的数据(-1结束):");
                while (scanf("%d",&Item),Item!=-1){
                    push_back(&mylist,Item);
                }
                break;
            case 2:
//                push_front
                printf("请输入您要插入的数据(-1结束):");
                while (scanf("%d",&Item),Item!=-1){
                    push_front(&mylist,Item);
                }

                break;
            case 3:
//                show_list
                show_list(&mylist);
                break;

            case 4:
//                pop_back
                pop_back(&mylist);
                break;
            case 5:
//                pop_front
                pop_front(&mylist);
                break;
            case 6:
//                insert_pos
                printf("input the pos you want to insert: ");
                scanf("%d",&pos);
                printf("input the value you want to insert: ");
                scanf("%d",&Item);
                insert_pos(&mylist,pos,Item);
                break;
            case 7:
//                find
                printf("input the data you want to find: ");
                scanf("%d",&Item);
                pos = find(&mylist,Item);
                if (pos==-1){
                    printf("data not exist;\n");
                } else{
                    printf("the data on index %d \n",pos);
                }
                break;
            case 8:
//                length
                len = length(&mylist);
                printf("length is :%d \n",len);
                break;
            case 9:
//                delete_pos
                printf("input the pos your want to delete: ");
                scanf("%d",&pos);
                delete_pos(&mylist,pos);
                break;
            case 10:
//                delete_val
                printf("input the value you want to delete: ");
                scanf("%d",&Item);
                delete_val(&mylist,Item);
                break;
            case 11:
//                sort
                sort(&mylist);
                break;
            case 12:
//                resver
                resver(&mylist);
                break;
            case 13:
//                clear
                clear(&mylist);
                break;
            case 14:
//                destory
                destory(&mylist);
                break;
            default:
                printf("input error\n");
                break;
        }
    }
    destory(&mylist);
    return 0;
}
  • SeqList.c
//
// Created by wenrou on 2019-09-20.
//
#include "SeqList.h"

void InitSeqList(SeqList *list){
    /*
     * 初始化和外部就是为结构体各元素进行初始化
     *      对base进行开辟空间
     *      对容量和大小进行填充
     * */
    list->base = (ElemType *)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
    assert(list->base != NULL);
    list->capacity=SEQLIST_INIT_SIZE;
    list->size=0;
}

void  push_back(SeqList *list,ElemType item){
    /*
     * 尾插法
     *
     * */
    if (list->size<list->capacity){
        list->base[list->size]=item;
        list->size++;
    } else{
        printf("顺序表空间已满,%d未能插入\n",item);
    }

}

void show_list(SeqList *list){
    for (int i = 0; i < list->size; ++i) {
        printf("%d ",list->base[i]);
    }
    printf("\n");

}

void  push_front(SeqList *list,ElemType item){
    if (list->size<list->capacity){
        for (int i = list->size; i >0 ; --i) {
            list->base[i]=list->base[i-1];

        }
        list->base[0]=item;
        list->size++;

    } else{
        printf("顺序表空间已满,%d未能插入\n",item);
    }
}

void pop_back(SeqList *list){
    if (list->size==0){
        printf("SeqList is null");
        return;
    }
    list->size--;
}

void pop_front(SeqList *list){
    if (list->size==0){
        printf("SeqList is null\n");
        return;
    }
    for (int i = 0; i<list->size-1; ++i) {
        list->base[i]=list->base[i+1];
    }
    list->size--;

}

void insert_pos(SeqList *list, int pos,ElemType item){
    if(pos>0 && pos<=list->size){
        if (pos==0){
            push_front(list,item);
        }else if (pos==list->size){
            push_back(list,item);
        } else{
            for (int i = list->size; i > pos; --i) {
                list->base[i]=list->base[i-1];
            }
            list->base[pos]=item;
            list->size++;
        }

    } else{
        printf("insert error pos");
        return;
    }
}

int find(SeqList *list,ElemType item){
    /*
     * 假设顺序表没有重复的数据
     * */
    for (int i = 0; i < list->size; ++i) {
        if (item==list->base[i]){
            return i;
        }
    }
    return -1;
}

int length(SeqList *list){
    return list->size;
}

void delete_pos(SeqList *list, int pos){
    if (list->size<pos+1){
        printf("data not find ");
        return;
    }
    for (int i = pos; i < list->size; ++i) {
        list->base[i]=list->base[i+1];
    }
    list->size--;
}

void delete_val(SeqList *list, ElemType item){
    if(find(list,item)){
        delete_pos(list,find(list,item));
        list->size--;
    } else{
        printf("the data not find \n");
        return;
    }
}

void sort(SeqList *list){
    if(list->size==0){
        printf("struct is null;\n");
    }
    ElemType item;
    /*
     * 冒泡排序
     * */
    for (int i = 0; i < list->size-1; ++i) {
        for (int j = 0; j < list->size-i; ++j) {
            if (list->base[j]>list->base[j+1]){
                item = list->base[j];
                list->base[j]=list->base[j+1];
                list->base[j+1]=item;
            }
        }
    }

}

void resver(SeqList *list){
    /*
     *            tmp
     *   1     2      3      4      5
     *  low                         high
     *
     * */
    if (list->size==0||list->size==1){
        return;
    }
    ElemType tmp;
    int low = 0,high = list->size-1;
    while (low<high){
        tmp = list->base[low];
        list->base[low]=list->base[high];
        list->base[high]=tmp;
        low++;
        high--;
    }

}
void clear(SeqList *list){
    list->size=0;
}

void destory(SeqList *list){
    free(list->base);
    list->base=NULL;
    list->capacity=0;
    list->size=0;
}
  • SeqList.h
//
// Created by wenrou on 2019-09-20.
//
/*
 * 结构数据函数的声明
 *
 * */

#ifndef XIANSHUNXUBIAO_SEQLIST_H
#define XIANSHUNXUBIAO_SEQLIST_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define SEQLIST_INIT_SIZE 8

typedef int ElemType;

typedef struct SeqList{
    ElemType *base; //指向一个真实所开辟的顺序表空间
    int capacity;   //容量
    int size;   //表的长度大小
}SeqList;

void InitSeqList(SeqList *list);
void  push_back(SeqList *list,ElemType item);
void show_list(SeqList *list);

void  push_front(SeqList *list,ElemType item);
void pop_back(SeqList *list);
void pop_front(SeqList *list);
void insert_pos(SeqList *list, int pos,ElemType item);

int find(SeqList *list,ElemType item);
int length(SeqList *list);
void delete_pos(SeqList *list, int pos);
void delete_val(SeqList *list, ElemType item);
void sort(SeqList *list);
void resver(SeqList *list);
void clear(SeqList *list);
void destory(SeqList *list);

#endif //XIANSHUNXUBIAO_SEQLIST_H

相关文章

网友评论

      本文标题:线性表之顺序表实现

      本文链接:https://www.haomeiwen.com/subject/zvtsuctx.html