sequence list 线性表

  • sequence list C code complementations briefly only for learing.

  • reference books:《零基础学数据结构》,《算法导论》,《数据结构与算法分析-c描述》,《数据结构》-严蔚敏

  • Qt creator4.3.1 and Visual studio community 2015 both be tested.


/*seq_list.h:sequence list header.
 *Author    :ZMG.
 *E-mail    :
 *Date      :2017-8-20
 *Version   :0.1
 
 *ALL rights reserved!
 *Code style try to refer to GNU linux kernel. :)
 */
 
 
#ifndef SEQ_LIST_H
#define SEQ_LIST_H
 
 
//include some essential headers.
#include <stdio.h>
#include <stdlib.h>
 
//define sequence list structure type.
#define list_size  1000
#define data_type  int
 
typedef struct {
    data_type list[list_size];  //also: data_type * list,then define int list_size.
    int length;
}seq_list;
 
 
//functions declaration below
void init_seq_list(seq_list *L); //initialize sequence list.
void destroy_seq_list(seq_list *L); //destroy sequncent listist.
void clear_seq_list(seq_list *L); //empty sequence list.
void is_seq_list_empty(seq_list L); //sequence list is empty or not.
int get_seq_list_length(seq_list L); //sequence list's length.
int get_seq_list_element(seq_list L, int i, data_type *e); //get sequence list element.
int locate_seq_list_element(seq_list L, data_type e); //locate sequence list element.
int insert_seq_list_element(seq_list *L, int i, data_type e); //insert sequence list element.
int delete_seq_list_element(seq_list *L, int i, data_type *e); //delete sequence list element.
 
 
#endif // SEQ_LIST_H


/*seq_list.c: sequence list implementation source file.
 *Author    :ZMG.
 *E-mail    :
 *Date      :2017-8-20
 *Version   :0.1
 
 *ALL rights reserved!
 *Code style try to refer to GNU linux kernel. :)
 */
 
 
#include "seq_list.h"
 
//some functions implementation.
void init_seq_list(seq_list *L) //initialize sequence list.
{
    L->length = 0;
}
 
void destroy_seq_list(seq_list *L) //destroy sequncent listist.
{
    free(L); //must have initialized the sequence list *L.
}
 
void clear_seq_list(seq_list *L) //empty sequence list.
{
    L->length = 0;
}
 
void is_seq_list_empty(seq_list L) //sequence list is empty or not.
{
    if (L.length == 0) {
        printf("sequence list is empty. \n");
    } else if (L.length != 0) {
        printf("sequence list is not empty. \n");
    }
}
 
int get_seq_list_length(seq_list L) //sequence list's length.
{
    return L.length;
}
 
int get_seq_list_element(seq_list L, int i, data_type *e) //get sequence list element.
{
    if (i < 1 || i > L.length)
        return -1;
        *e = L.list[i-1];
 
    return 1;
}
 
int locate_seq_list_element(seq_list L, data_type e) //locate sequence list element.
{
    int i = 0;
 
    for (i = 0; i < L.length; i++) {
        if (L.list[i] == e) {;}
    }
 
    return i-1;
}
 
int insert_seq_list_element(seq_list *L, int i, data_type e) //insert sequence list element.
{
    int j = 0;
 
    if (i < 1 || i > L->length + 1) {
        printf ("insert illegal.exit!");
        return -1;
    } else if (L->length >= list_size) {
        printf("the sequence list is full,can not insert element any more.exit! \n");
        return -1;
    } else {
        for (j = L->length; j >= i; j--)
            L->list[j] = L->list[j-1];  //let list[i-1] = list[i]
            L->list[i-1] = e;
            L->length = L->length + 1;
 
            return 1;
        }
}
 
int delete_seq_list_element(seq_list *L, int i, data_type *e) //delete sequence list element.
{
    int j = 0;
 
    if (L->length <= 0) {
        printf("sequence list is empty and cannot delete element any more.exit! \n");
        return 0;
    } else if (i < 1 || i > L->length) {
        printf("the location you delete is not applicable.exit! \n");
        return -1;
    } else {
        *e = L->list[i-1];
        for (j = i; j <= L->length-1; j++) {
            L->list[j-1] = L->list[j];  //let list[i] = list[i-1]
        }
        L->length = L->length - 1;
 
        return 1;
    }
}


/*main      :for testing.
 *Author    :ZMG.
 *E-mail    :
 *Date      :2017-8-20
 *Version   :0.1
 
 *ALL rights reserved!
 *Code style try to refer to GNU linux kernel. :)
 */
 
 
#include "seq_list.h"
 
int main(void)
{
    int i = 0;
    int j = 0;
    int flag = 0;
    data_type e = 0;
    int length = 0;
 
    seq_list A;
    seq_list B;
 
    init_seq_list(&A);
    init_seq_list(&B);
 
    //insert 1~10 to A
    for (i = 1; i <= 10; i++) {
        if (insert_seq_list_element(&A, i, i) == 0) {
            printf("the location you insert is illegal.exit! \n");
            return -1;
        }
    }
 
    //insert some data into B
    for (i = 1, j = 1; j <= 6; i += 2, j++) {
        if (insert_seq_list_element(&B, j, i * 2) == 0) {
            printf("the location you insert is illegal.exit! \n");
            return -1;
        }
    }
 
    printf("A: \n");
    for (i = 1; i <= A.length; i++) {
        flag = get_seq_list_element(A, i, &e);
 
        if (flag == 1)
            printf("%4d", e);
    }
 
    printf("\n");
 
    length = A.length;
    printf("A.length = %4d \n", length);
 
    printf("B: \n");
    for (i = 1; i <= B.length; i++) {
        flag = get_seq_list_element(B, i, &e);
 
        if (flag == 1)
            printf("%4d", e);
    }
 
    printf("\n");
 
    length = B.length;
    printf("B.length = %4d \n", length);
     
    //code other functons if you want to test bolow.
 
    system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值