数据结构的概念:
相互之间存在一种或多种特定关系的数据元素的集合。
数据之间关系:
逻辑关系:集合,线性(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)
不具有动态存储功能。
关于顺序表的操作
#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\nwant_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);
}
#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;
}
#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