sequence list 线性表
来源:互联网 发布:淘宝天猫店铺托管 编辑:程序博客网 时间:2024/05/18 00:58
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 belowvoid 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");}
阅读全文
0 0
- sequence list 线性表
- sequence list 线性表
- 数据结构_线性表顺序sequence List
- 顺序表(sequence list)
- 线性表(list)
- list线性表赋值
- 线性表(Linear List)
- 线性表List
- 线性表list
- Sequence list
- Sequence List
- List C++链式线性表
- c++实现list线性表
- 线性表(List) 一
- 线性表(List) 二
- 【数据结构学习】线性表List
- C#数据结构--线性表--List
- 2.1.6 —线性表—Longest Consecutive Sequence
- 最长上升子序列
- C#winform只能输入数字后不能复制粘贴问题
- VS一些常见问题处理(收集ing)
- csu1113模拟,STL
- java里绝对路径和相对路径的区别
- sequence list 线性表
- C语言结构体位段特性实现断言宏
- lower_bound以及upper_bound
- C 复习第一课 各个基础类型保存 以及表达式中一些表达方式
- python_装饰器二(对有/不定长参数函数的装饰)
- Java并发编程:同步容器
- SpringBoot21-springboot的数据访问-引入Docker
- PAT A 1115. Counting Nodes in a BST (30)
- C# 打印DataTable 修正版 适应宽度超出一页 数据量超出一页的情况