嵌入式系统学习Day19(数据结构)

数据结构的概念:

相互之间存在一种或多种特定关系的数据元素的集合。

数据之间关系:

逻辑关系:集合,线性(1对1,中间位置的值有且仅有一个前驱,一个后继),树(1对多),图(多对多)

物理关系:

顺序结构(类似于数组):存储空间连续,有序

链表:存储空间不连续

算法:

对特定问题求解步骤的描述。(类似函数)

算法的特性:输入输出特性(输入可省,输出必须有,数值的改变就可以称为输出),可读性(便于阅读), 可行性(可以用代码执行出来),有穷性(是有限的),确定性(同一个输入会是同一个输出)

衡量算法好坏的方法。

时间复杂度,时间的度量(事前分析法) ,大O 记法。

O(1)<O(lgn)<O(N)<O(nLgN)<O(N^2)<O(N^3)

顺序表

存储空间是连续

特征: 支持随机访问 head+5 head[0] O(1)

插入,删除, 整体移动。 O(N)

不具有动态存储功能。

关于顺序表的操作

cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
#include <string.h>
int main(int argc, char** argv)
{
  FILE* fp = fopen("/home/linux/dict.txt","r");
  if(NULL == fp)
  {
    perror("fopen");
    return 1;
  }

  SeqList* sl = CreateSeqList(20000);
  if(NULL == sl)
  {
    fprintf(stderr,"CreateSeqList error\n");
    return 1;
  }

  while(1)
  {
    DATATYPE data;
   
    char * word=NULL;
    char* mean=NULL;
    char linebuf[1024]={0};
    if(NULL==fgets(linebuf,sizeof(linebuf),fp))
    {
      break;
    }

    word = strtok(linebuf," ");
    mean = strtok(NULL,"\r");
    strcpy(data.word,word);
    strcpy(data.mean,mean);
    // strcpy(data->word,word);
    // strcpy(data->mean,mean);
    InsertTailSeqList(sl, &data);

  }
  
  while(1)
  {
    char want_word[50]={0};
    printf("input word:");
    fgets(want_word,sizeof(want_word),stdin);// book\n
    want_word[strlen(want_word)-1]='\0';
    if(0==strcmp(want_word,"#quit"))
    {
      break;
    }
    int ret = FindSeqList(sl, want_word);
    if(-1 == ret)
    {
      printf("cant find %s\n",want_word);
    }
    else  
    {
      ShowSeqListOne(sl, ret);
    }

  }
  return 0;
  fclose(fp);
  DestroySeqList(sl);
}
cs 复制代码
#include "hwdict.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
SeqList *CreateSeqList(int len)
{
  SeqList *sl = malloc(sizeof(SeqList));
  if (NULL == sl)
  {
    perror("CreateSeqList malloc error\n");
    return NULL;
  }
  sl->head = malloc(sizeof(DATAWORD) * len);
  if (NULL == sl->head)
  {
    perror("CreateSeqList malloc2 error\n");
    return NULL;
  }
  sl->tlen = len;
  sl->clen = 0;
  return sl;
}

void ReadInfo(SeqList *list, char *argv)
{
  int fd = open(argv, O_RDONLY);
  if(-1 ==fd)
  {
    perror("open");
    exit(1);
  }
  char buf[50];
  char info_buf[500];
  int i;
  int flag =1;
  int ret =0;
  //while (list->clen < list->tlen)
  //每一行读取
  while(flag)
  {
    i = 0;
    while (1)
    {
      ret = read(fd, &buf[i], 1);
      if(ret<=0)
      {
        flag =0;
        break;
      }
      if (buf[i] == ' ')
      {
        buf[i] = '\0';
        strcpy(list->head[list->clen].name, buf);
        break;
      }
      i++;
    }
    i = 0;
    while (1)
    {
      
      ret= read(fd, &info_buf[i], 1);
      if(ret<=0)
      {
        flag =0;
        break;
      }
      if (info_buf[i] == '\n')
      {
        info_buf[i] = '\0';
        strcpy(list->head[list->clen].info, info_buf);
        break;
      }
      i++;
    }
    list->clen++;
    if(list->clen == list->tlen)
    {
        flag=0;
        break;
    }
  }
  close(fd);
}

int GetSizeSeqList(SeqList *list)
{
  return list->clen;
}

int FindSeqList(SeqList *list, char *name)
{
  int len = GetSizeSeqList(list);
  for (int i = 0; i < len; i++)
  {
    if (0 == strcmp(list->head[i].name, name))
    {
      return i;
    }
  }
  return -1;
}
cs 复制代码
#ifndef _SEQLIST_H_
#define _SEQLIST_H_

typedef struct word
{
  char name[50];
  char info[500];
} DATAWORD;

typedef struct list
{
  DATAWORD *head;
  int tlen;
  int clen;
} SeqList;

//创建一个顺序表
SeqList *CreateSeqList(int len);

void ReadInfo(SeqList *list, char *argv);

int GetSizeSeqList(SeqList *list);

int FindSeqList(SeqList *list, char *name);
#endif
相关推荐
在路上`1 小时前
前端学习之后端小白java的一些理论知识(框架)
java·学习
练习时长两年半的Java练习生(升级中)1 小时前
从0开始学习Java+AI知识点总结-18.web基础知识(Java操作数据库)
java·学习·web
DdduZe2 小时前
8.19作业
数据结构·算法
PyHaVolask2 小时前
链表基本运算详解:查找、插入、删除及特殊链表
数据结构·算法·链表
xy_recording2 小时前
Day08 Go语言学习
开发语言·学习·golang
黑客影儿3 小时前
黑客哲学之学习笔记系列(三)
笔记·学习·程序人生·安全·职场和发展·网络攻击模型·学习方法
1白天的黑夜14 小时前
链表-2.两数相加-力扣(LeetCode)
数据结构·leetcode·链表
花开富贵ii4 小时前
代码随想录算法训练营四十六天|图论part04
java·数据结构·算法·图论
熬了夜的程序员4 小时前
【LeetCode】16. 最接近的三数之和
数据结构·算法·leetcode·职场和发展·深度优先