www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 公眾號(hào)精選 > 嵌入式云IOT技術(shù)圈
[導(dǎo)讀]????之前寫(xiě)過(guò)類似的文章: https://blog.csdn.net/morixinguan/article/details/83309576 ????關(guān)于文件操作,特別是從后往前讀取,要是像上面這篇文章一樣去操作,那效率明顯就太低了,如果一旦數(shù)據(jù)一多,很難處理。 ????于是想到了用更好的數(shù)據(jù)結(jié)構(gòu)來(lái)解決這

    之前寫(xiě)過(guò)類似的文章:

https://blog.csdn.net/morixinguan/article/details/83309576

    關(guān)于文件操作,特別是從后往前讀取,要是像上面這篇文章一樣去操作,那效率明顯就太低了,如果一旦數(shù)據(jù)一多,很難處理。

    于是想到了用更好的數(shù)據(jù)結(jié)構(gòu)來(lái)解決這個(gè)問(wèn)題,不就是想從后往前顯示嘛?那么就可以用鏈表來(lái)解決這個(gè)問(wèn)題了。

    

typedef struct links

{

int size  ; 

void *ptr ; 

struct links *next ;

struct links *pre  ;

}LINKS;

    這是鏈表的數(shù)據(jù)結(jié)構(gòu),ptr就是要存放的數(shù)據(jù),pre是前驅(qū)指針,next是后繼指針,通過(guò)這兩個(gè)指針,即可以方便實(shí)現(xiàn)鏈表的遍歷。

   下面定義要實(shí)現(xiàn)的函數(shù):

typedef  void (*print_t)(void *Data)  ; 

void print(void *Data);

//打印鏈表節(jié)點(diǎn)

void   print_links(LINKS *Header , print_t  func);

//創(chuàng)建鏈表頭

LINKS  *Create_Links_Header(int size);

//頭插

void  top_append(LINKS *Header , void *Data , int size);

//獲取文件總行數(shù)

int GetTotalLineCount(FILE *file);

    

整體實(shí)現(xiàn):

#include<stdio.h>

#include<stdlib.h>

#include <string.h>

#define  NR(x)   (sizeof(x)/sizeof(x[0]+0))

 

typedef struct links

{

int size  ; 

void *ptr ; 

struct links *next ;

struct links *pre  ;

}LINKS;

 

typedef  void (*print_t)(void *Data)  ; 

void print(void *Data);

void   print_links(LINKS *Header , print_t  func);

LINKS  *Create_Links_Header(int size);

void  top_append(LINKS *Header , void *Data , int size);

int GetTotalLineCount(FILE *file);

 

 

int main(void)

{

int line = 0 ;

int file_all_line = 0 ;

char line_buffer[50] = {0};

LINKS *Header = NULL ;

//1.初始化鏈表 

Header =  Create_Links_Header(0);

if(NULL == Header)

{

fprintf(stderr , "malloc  Header fail \n");

return -1 ; 

}

//2.插入數(shù)據(jù) 

FILE *fp = NULL ;

fp = fopen("1.csv","r");

if(NULL == fp)

{

printf("open csv file fail!\n");

return -1 ;

}

//移到文件頭 

fseek(fp,0,SEEK_SET);

//獲取文件的總行數(shù) 

file_all_line = GetTotalLineCount(fp);

for(line = 0 ; line < file_all_line ; line++)

{

if(fgets(line_buffer,50,fp))

{

printf("line_buffer:%s",line_buffer);

top_append(Header , line_buffer , 100);

}

}

print_links(Header,print);

fclose(fp);

free(Header);

return 0 ;

}

 

void print(void *Data)

{

printf("%s" ,Data);

//這里可以進(jìn)行數(shù)據(jù)處理...

//這里可以進(jìn)行數(shù)據(jù)處理...

}

//打印鏈表節(jié)點(diǎn)

void print_links(LINKS *Header , print_t func)

{

LINKS *tmp = Header->next ; 

 

while(tmp != Header)

{

func(tmp->ptr);

tmp = tmp->next ;

        free(tmp->pre);

}

}

//獲取文件的總行數(shù)

int GetTotalLineCount(FILE *file)

{   

int line_num = 0;

char strLine[50];

fseek(file,0,SEEK_SET);

while (fgets(strLine, 50, file))

line_num++;

fseek(file,0,SEEK_SET);

return line_num;

}

//創(chuàng)建表頭

LINKS  *Create_Links_Header(int size)

{

LINKS *New = NULL  ; 

New = malloc(sizeof(LINKS));

if(NULL == New)

{

fprintf(stderr , "malloc LINKS header fail \n");

return NULL ; 

}

 

New->size = size ; 

New->ptr = NULL ; 

New->next = New ;

New->pre  = New ; 

 

return New ;

}

//鏈表頭插

void  top_append(LINKS *Header , void *Data , int size)

{

LINKS *New = NULL ; 

New = malloc(sizeof(LINKS));

if(NULL == New)

{

fprintf(stderr , "malloc links fail \n");

return ;

}

New->ptr=NULL ;

New->size = size ; 

 

New->ptr = malloc(size);

if(NULL == New->ptr)

{

fprintf(stderr , "malloc links data fail \n");

return ; 

}

memcpy(New->ptr , Data , size);

New->next = Header->next ; 

New->pre  = Header ; 

New->next->pre = New ;  

New->pre->next = New ; 

}

 

運(yùn)行結(jié)果:

如下圖所示為excel文件的數(shù)據(jù):

運(yùn)行程序得到:

從這里看到,整個(gè)程序就是利用了棧的思想,先進(jìn)后出,這樣的實(shí)現(xiàn)既簡(jiǎn)單,也高效。

 

 


免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
關(guān)閉
關(guān)閉