五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

P87-P90

2023-07-08 23:58 作者:取法于一  | 我要投稿

https://www.bilibili.com/video/BV18p4y167Md?p=87&vd_source=90f77914d35a141cd8083716ada7726e

有關(guān)llist_travel(handler,print_s)遍歷鏈表打印輸出的代碼:

llist.h
?? ?typedef void llist_op(const void *);
?? ?void llist_travel(LLIST *,llist_op *);

main.c
?? ?static void print_s(const void *record)
?? ?{
?? ??? ?const struct score_st *r = record;
?? ?????printf("%d %s %d %d\n",r->id,r->name,r->math,r->chinese);??????????????????????????????????????????????????????????????????
?? ?}

?? ?llist_travel(handler,print_s);

llist.c
?? ?void llist_travel(LLIST *ptr,llist_op *op)
?? ?{
?? ??? ?struct llist_node_st *cur;

?? ??? ?for(cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
?? ??? ???????? op(cur->data);
?? ????
?? ?}


考慮通用性,llist_travel()在遍歷鏈表之后會遇到問題,不知道*data里面的成員是什么,因?yàn)槭怯脩魟?chuàng)建的,無法提前定義。
所以需要用戶提供一個輸出每個成員的函數(shù)。將用戶提供的函數(shù)作為參數(shù),就可以解決這個問題。這就是回調(diào)函數(shù)。這就得有一個接口,告訴用戶回調(diào)函數(shù)的形式。

首先來看llist.h里的typedef void llist_op(const void *);這行代碼,這是接口函數(shù)的聲明,為什么這么寫,考慮到是打印輸出,默認(rèn)不需要返回值,所以形式是void (),那么里面的參數(shù)呢?考慮到接口要打印輸出各種類型的值,所以參數(shù)是指針,因?yàn)橹羔樋梢灾赶蚋鞣N類型的數(shù)據(jù),并且指針指向的類型是不確定的,用void。那么形式就是void?? (void *);然后取一別名,llist_op。

那么用戶就可以指定打印輸出的數(shù)據(jù)類型和成員名也就是print_s();函數(shù)里面兩行。

llist_travel();函數(shù)也可以通過回調(diào)函數(shù)打印輸出無法提前定義類型的*data。

理解了這個,后面的find,delete,fetch就容易了。下面是這幾節(jié)視頻的代碼。

llist.h
??? #ifndef LLIST_H__ ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?? ?#define LLIST_H__

?? ?#define LLIST_FORWARD?? 1
?? ?#define LLIST_BACKWARD? 2

?? ?typedef void llist_op(const void *);
?? ?typedef int llist_cmp(const void *,const void *);

?? ?struct llist_node_st
?? ?{
?? ??? ?void *data;
?? ??? ?struct llist_node_st *prev;
?? ??? ?struct llist_node_st *next;
?? ?};

?? ?typedef struct
?? ?{
?? ??? ?int size;
?? ??? ?struct llist_node_st head;
?? ?}LLIST;

?? ?LLIST *llist_create(int initsize);

?? ?int llist_insert(LLIST *,const void *data,int mode);


?? ?void *llist_find(LLIST *,const void *key,llist_cmp *);

?? ?int llist_delete(LLIST *,const void *key,llist_cmp *);

?? ?int llist_fetch(LLIST *,const void *key,llist_cmp *,void *data);

?? ??? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ?void llist_travel(LLIST *,llist_op *);

?? ?void llist_destroy(LLIST *);

?? ?#endif

llist.c

?? ?#include <stdio.h>?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ?#include <stdlib.h>
?? ?#include <string.h>

?? ?#include "llist.h"

?? ?LLIST *llist_create(int initsize)
?? ?{
?? ??? ?LLIST *new;

?? ??? ?new = malloc(sizeof(*new));
?? ??? ?if(new == NULL)
?? ??? ???????? return NULL;

?? ??? ?new->size =? initsize;
?? ??? ?new->head.data = NULL;
?? ??? ?new->head.prev = &new->head;
?? ??? ?new->head.next = &new->head;

?? ??? ?return new;
?? ?}

?? ?int llist_insert(LLIST *ptr,const void *data,int mode)
?? ?{
?? ??? ?struct llist_node_st *newnode;
?? ??? ?
?? ??? ?newnode = malloc(sizeof(*newnode));
?? ??? ?if(newnode == NULL)
?? ??? ???????? return -1;

?? ??? ?newnode->data = malloc(ptr->size);
?? ??? ?
?? ??? ?//if(newnode->data == NULL);一個分號找半個小時。
?? ??? ?if(newnode->data == NULL)
?? ??? ???????? return -2;
?? ??? ?memcpy(newnode->data,data,ptr->size);
?? ??? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ??? ?if(mode == LLIST_FORWARD)
?? ??? ?{
?? ??? ???????? newnode->prev = &ptr->head;
?? ??? ???????? newnode->next = ptr->head.next;
?? ?//????????????? newnode->prev->next = newnode;
?? ?//????????????? newnode->next->prev = newnode;
?? ??? ?}
?? ??? ?else??? if(mode == LLIST_BACKWARD)
?? ??? ???????? {
?? ??? ???????????????? newnode->prev = ptr->head.prev;
?? ??? ???????????????? newnode->next = &ptr->head;
?? ?//????????????????????? newnode->prev->next = newnode;
?? ?//????????????????????? newnode->next->prev = newnode;
?? ??? ???????? }
?? ??? ???????? else??? //error
?? ??? ???????? {
?? ??? ???????????????? return -3;
?? ??? ???????? }
?? ??? ?newnode->prev->next = newnode;
?? ??? ?newnode->next->prev = newnode;
?? ??? ?
?? ??? ?return 0;
?? ?}
?? ??? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ?static struct llist_node_st * find_(LLIST *ptr,const void *key,llist_cmp *cmp)
?? ?{
?? ??? ?struct llist_node_st *cur;
?? ??? ?for(cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
?? ??? ?{
?? ??? ???????? if(cmp(key,cur->data) == 0)
?? ??? ???????????????? break;
?? ??? ?}

?? ??? ?return cur;
?? ?}

?? ?void *llist_find(LLIST *ptr,const void *key,llist_cmp *cmp)
?? ?{
?? ??? ?return find_(ptr,key,cmp)->data;
?? ?}

?? ?int llist_delete(LLIST *ptr,const void *key,llist_cmp *cmp)
?? ?{
?? ??? ?struct llist_node_st *node;
?? ??? ?node = find_(ptr,key,cmp);

?? ??? ?if(node == &ptr->head)
?? ??? ???????? return -1;?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?

?? ??? ?node->prev->next = node->next;
?? ??? ?node->next->prev = node->prev;
?? ??? ?free(node->data);
?? ??? ?free(node);

?? ??? ?return 0;
?? ?}


?? ?int llist_fetch(LLIST *ptr,const void *key,llist_cmp *cmp,void *data)
?? ?{
?? ??? ?struct llist_node_st *node;
?? ??? ?node = find_(ptr,key,cmp);

?? ??? ?if(node == &ptr->head)
?? ??? ???????? return -1;
?? ??? ?
?? ??? ?node->prev->next = node->next;
?? ??? ?node->next->prev = node->prev;

?? ??? ?if(data != NULL)
?? ??? ???????? memcpy(data,node->data,ptr->size);

?? ??? ?free(node->data);??????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ??? ?free(node);

?? ??? ?return 0;
?? ?}

?? ?void llist_travel(LLIST *ptr,llist_op *op)
?? ?{
?? ??? ?struct llist_node_st *cur;

?? ??? ?for(cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
?? ??? ???????? op(cur->data);
?? ??? ?
?? ?}

?? ?void llist_destroy(LLIST *ptr)
?? ?{
?? ??? ?struct llist_node_st *cur,*next;

?? ??? ?for(cur = ptr->head.next; cur != &ptr->head; cur = next)
?? ??? ?{
?? ??? ???????? next = cur->next;
?? ??? ???????? free(cur->data);
?? ??? ???????? free(cur);
?? ??? ?}??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ??? ?free(ptr);
?? ?}

main.c

?? ?#include <stdio.h>?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ?#include <stdlib.h>
?? ?#include <string.h>

?? ?#include "llist.h"

?? ?#define NAMESIZE??????? 32

?? ?struct score_st
?? ?{
?? ??? ?int id;
?? ??? ?char name[NAMESIZE];
?? ??? ?int math;
?? ??? ?int chinese;
?? ?};???????????? ?

?? ?static void print_s(const void *record)
?? ?{
?? ??? ?const struct score_st *r = record;

?? ??? ?printf("%d %s %d %d\n",r->id,r->name,r->math,r->chinese);
?? ?}

?? ?static int id_cmp(const void *key,const void *record)
?? ?{
?? ??? ?const int *k = key;
?? ??? ?const struct score_st *r = record;
?? ??? ?
?? ??? ?return (*k - r->id);
?? ?}

?? ?static int name_cmp(const void *key,const void *record)
?? ?{
?? ??? ?const char *k = key;
?? ??? ?const struct score_st *r = record;
?? ??? ?return strcmp(k,r->name);
?? ?}

?? ?int main()
?? ?{??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?

?? ??? ?LLIST *handler;
?? ??? ?struct score_st tmp;
?? ??? ?int ret,i;
?? ??? ?int id = 3;
?? ??? ?char *del_name = "std6";
?? ??? ?struct score_st *data;

?? ??? ?handler = llist_create(sizeof(struct score_st));
?? ??? ?if(handler == NULL)
?? ??? ???????? exit(1);

?? ??? ?for(i = 0; i < 7; i++)
?? ??? ?{
?? ??? ???????? tmp.id = i;
?? ??? ???????? snprintf(tmp.name,NAMESIZE,"std%d",i);
?? ??? ???????? tmp.math = rand()%100;
?? ??? ???????? tmp.chinese = rand()%100;

?? ??? ???????? ret = llist_insert(handler,&tmp,LLIST_FORWARD);
?? ??? ???????? if(ret)????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ??? ???????????????? exit(1);

?? ??? ?}

?? ??? ?llist_travel(handler,print_s);

?? ??? ?printf("\n\n");

?? ??? ?ret = llist_delete(handler,&id,id_cmp);
?? ??? ?if(ret)
?? ??? ???????? printf("llist_delete failed!\n");
?? ??? ?llist_travel(handler,print_s);

?? ??? ?printf("\n\n");

?? ??? ?ret = llist_delete(handler,del_name,name_cmp);//compile
?? ??? ?if(ret)
?? ??? ???????? printf("llist_delete failed!\n");
?? ??? ?llist_travel(handler,print_s);

?? ?#if 0
?? ??? ?data = llist_find(handler,&id,id_cmp);
?? ??? ?if(data == NULL)
?? ??? ???????? printf("Can not find!\n");?????????????????????????????????????????????????????????????????????????????????????????????????? ?
?? ??? ?else
?? ??? ???????? print_s(data);
?? ?#endif
?? ??? ?llist_destroy(handler);

?? ??? ?exit(0);
?? ?}

P87-P90的評論 (共 條)

分享到微博請遵守國家法律
博罗县| 当雄县| 仙桃市| 淮滨县| 无锡市| 宝山区| 连云港市| 十堰市| 铜梁县| 扎鲁特旗| 阜阳市| 新巴尔虎右旗| 尼勒克县| 兰西县| 两当县| 搜索| 阿克陶县| 陆川县| 霞浦县| 扶沟县| 随州市| 梁平县| 成都市| 连平县| 紫金县| 东兰县| 古丈县| 河西区| 崇左市| 巧家县| 晋州市| 扎囊县| 信阳市| 嘉祥县| 沧源| 恩施市| 乐平市| 竹溪县| 荃湾区| 菏泽市| 库伦旗|