一、基本概念
数据结构(Data Structure)概念: 数据结构是研究组成数据的数据元素的关系的学科。
数据结构研究目的 : 通过研究数据元素的关系,帮助我们在开发软件的时候更好的组件数据模型,让数据在内存中的操作更加的流畅高效。
数据 ->信息的载体,是能够输入到计算机中并且能被计算机识别、存储和处理的符号总称。
数据元素->数据的基本单位,又称之为记录(Record)。
数据项->数据元素由若干数据项组成,数据项是数据中的最小单位。
数据类型、数据类型->对数据元素取值范围与运算的限定。
算法(Algorithm)是解决特定问题的步骤的描述。在计算机中算法是一个有穷规则(或语句、指令)的有序集合。它确定了解决某一问题的一个运算序列。对于问题的初始输入,通过算法有限步的运行,产生一个或多个输出。
(1)有穷性 (2)确定性 (3)可行性 (4)输入 (5)输出
算法是解决问题的思想方法
程序是计算机语言的具体实现
算法设计:取决于选定的逻辑结构
算法实现:依赖于采用的存储结构
瑞士科学家沃思(N.Wirth)的著名公式: 数据结构+算法=程序
算法效率——用依据该算法编制的程序在计算机上执行所消耗的时间来度量。
问题的规模:输入数据量的大小,用n来表示。
算法的时间复杂度:算法消耗时间,它是问题规模的函数T(n)
线性表的定义——线性表就是零个或多个相同数据元素的有限序列。
二、代码实现(顺序表)
#ifndef __SEQLIST_H
#define __SEQLIST_H
#define MAXSIZE 100 //存放最大数据个数
typedef int data_t ;
/* 定义表 */
typedef struct {
data_t data[MAXSIZE] ;
int last ; //有效数据索引
}seqlist_t ;
/* 创建顺序表 */
seqlist_t *creat_seqlist(void) ;
/* 释放顺序表 */
void clear_seqlist(seqlist_t *L) ;
/* 判断顺序表是否为空 */
int is_empty_seqlist(seqlist_t *L) ;
/* 判断顺序表是否为满 */
int is_full_seqlist(seqlist_t *L) ;
/* 设置顺序表为空表 */
void set_empty_seqlist(seqlist_t *L) ;
/* 获得顺序表长度 */
int get_length_seqlist(seqlist_t *L) ;
/* 控制台打印顺序表 */
void show_seqlist(seqlist_t *L) ;
/* 顺序表插入数据 */
int insert_seqlist(seqlist_t *L, data_t x, int pos) ;
/* 顺序表删除数据 */
int delete_seqlist(seqlist_t *L, int pos) ;
/* 根据下标改变顺序表数据 */
int change_seqlist(seqlist_t *L, data_t x, int pos) ;
/* 查询顺序表指定数据第一次出现的位置 */
int search_seqlist(seqlist_t *L, data_t x) ;
#endif
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
/* 创建顺序表 */
seqlist_t *creat_seqlist(void)
{
seqlist_t *L = NULL ;
L = (seqlist_t *)malloc(sizeof(seqlist_t)) ;
if(L == NULL)
{
puts("no memory") ;
return NULL ;
}
L->last = -1 ;
return L ;
}
/* 释放顺序表 */
void clear_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL") ;
return ;
}
free(L) ;
return ;
}
/* 判断顺序表是否为空 */
int is_empty_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
return (L->last == -1) ;
}
/* 判断顺序表是否为满 */
int is_full_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
return (L->last == MAXSIZE-1) ;
}
/* 设置顺序表为空表 */
void set_empty_seqlist(seqlist_t *L)
{
if(is_empty_seqlist(L))
return ;
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return ;
}
for(int i = 0 ; i < L->last; i ++)
{
L->data[i] = 0 ;
}
L->last = -1 ;
}
/* 获得顺序表长度 */
int get_length_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
return L->last+1 ;
}
/* 控制台打印顺序表 */
void show_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return ;
}
for(int i = 0; i <= L->last; i ++)
printf("L->data[%d] = %d\n", i, L->data[i]) ;
}
/* 顺序表插入数据 */
int insert_seqlist(seqlist_t *L, data_t x, int pos)
{
if(is_full_seqlist(L) || (pos<0) || (pos>L->last+1))
{
puts("input argv is invalid") ;
return -1 ;
}
for(int i = L->last; i >= pos; i --)
{
L->data[i+1] = L->data[i] ;
}
L->data[pos] = x ;
L->last ++ ;
return 0 ;
}
/* 顺序表删除数据 */
int delete_seqlist(seqlist_t *L, int pos)
{
if((pos > L->last) || pos < 0)
{
puts("input argv is invalid") ;
return -1 ;
}
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
for(int i = pos ; i <= L->last; i ++)
L->data[i] = L->data[i+1] ;
L->last -- ;
return 0 ;
}
/* 根据下标改变顺序表数据 */
int change_seqlist(seqlist_t *L, data_t x, int pos)
{
if((pos > L->last) || pos < 0)
{
puts("input argv is invalid") ;
return -1 ;
}
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
L->data[pos] = x ;
return 0 ;
}
/* 查询顺序表指定数据第一次出现的位置 */
int search_seqlist(seqlist_t *L, data_t x)
{
if(is_empty_seqlist(L))
{
puts("this seqlist is empty") ;
return -1 ;
}
if(L == NULL)
{
puts("seqlist_t * L is NULL") ;
return -1 ;
}
for(int i = 0; i < L->last; i ++)
{
if(x == L->data[i])
{
return i ;
}
}
return -1 ;
}
三、测试代码
#include <stdio.h>
#include "seqlist.h"
#include <stdlib.h>
int main(int argc, char **argv)
{
int pos = -1 ;
seqlist_t *L = NULL ;
L = creat_seqlist() ;
for(int i = 0; i < 10; i ++)
insert_seqlist(L, i+1, 0) ;
printf("******************************************\n") ;
printf("creat_seqlist:\n") ;
show_seqlist(L) ;
printf("******************************************\n") ;
printf("serch_seqlist:\n") ;
pos = search_seqlist(L, 12) ;
printf("data:12 pos:%d\n", pos) ;
pos = search_seqlist(L, 4) ;
printf("data:4 pos:%d\n", pos) ;
printf("******************************************\n") ;
printf("change_seqlist:pos=8 data=10\n") ;
change_seqlist(L, 10, 8) ;
show_seqlist(L) ;
printf("******************************************\n") ;
printf("delete_seqlist:pos=6\n") ;
delete_seqlist(L, 6) ;
show_seqlist(L) ;
printf("******************************************\n") ;
printf("insert_seqlist:pos=5 data=11\n") ;
insert_seqlist(L, 11, 5) ;
show_seqlist(L) ;
printf("seqlist length is %d\n", get_length_seqlist(L)) ;
printf("******************************************\n") ;
printf("set_empty_seqlist:\n") ;
set_empty_seqlist(L) ;
show_seqlist(L) ;
printf("seqlist length is %d\n", get_length_seqlist(L)) ;
clear_seqlist(L) ;
return 0 ;
}
四、测试结果
mr-deng@ubuntu:~/user/data_structure/line/seqlist$ ./test
******************************************
creat_seqlist:
L->data[0] = 10
L->data[1] = 9
L->data[2] = 8
L->data[3] = 7
L->data[4] = 6
L->data[5] = 5
L->data[6] = 4
L->data[7] = 3
L->data[8] = 2
L->data[9] = 1
******************************************
serch_seqlist:
data:12 pos:-1
data:4 pos:6
******************************************
change_seqlist:pos=8 data=10
L->data[0] = 10
L->data[1] = 9
L->data[2] = 8
L->data[3] = 7
L->data[4] = 6
L->data[5] = 5
L->data[6] = 4
L->data[7] = 3
L->data[8] = 10
L->data[9] = 1
******************************************
delete_seqlist:pos=6
L->data[0] = 10
L->data[1] = 9
L->data[2] = 8
L->data[3] = 7
L->data[4] = 6
L->data[5] = 5
L->data[6] = 3
L->data[7] = 10
L->data[8] = 1
******************************************
insert_seqlist:pos=5 data=11
L->data[0] = 10
L->data[1] = 9
L->data[2] = 8
L->data[3] = 7
L->data[4] = 6
L->data[5] = 11
L->data[6] = 5
L->data[7] = 3
L->data[8] = 10
L->data[9] = 1
seqlist length is 10
******************************************
set_empty_seqlist:
seqlist length is 0