//Take the lead in the bidirectional circular linked list (variable-length structure)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#defineNAMESIZE 24
typedef struct stuinfo{
int id;
char name[NAMESIZE];
int math;
}DATA;
static void print_s(const void *data) // data 接收地址是p->data
{
const DATA *stup = data; // 把接受到的数据放置到结构体中。
printf("%d %s %d\n",
stup->id,
stup->name,
stup->math);
}
static int IdCmp(const void *key, const void *data)
{
const int *id = key;
const DATA *stup = data;
return (*id - stup->id);
}
static int NameCmp(const void *key, const void *data)
{
const char *name = key;
const DATA *stup = data;
return strcmp(name, stup->name);
}
int main()
{
int i;
int id = 5;
char name[NAMESIZE] = "stu3";
DATA stu, *stup;
LIST handle = NULL;
handle = ListCreate(sizeof(DATA)); //handle为头节点的指针,头节点数据域大小为DATA的类型大小。
if(NULL == handle)
return -1;
for(i = 0; i < 6; i++)
{
stu.id = i + 1;
snprintf(stu.name,NAMESIZE,
"stu%d", i + 1); // int snprintf(char *str, size_t size, const char *format, ...);
stu.math = 100 - i;
//ListInsert(handle, &stu, HEADINSERT);
ListInsert(handle, &stu, TAILINSERT);
}
/*find*/
//stup = ListFind(handle, &id, IdCmp);
stup = ListFind(handle, name, NameCmp);
if(stup != NULL)
print_s(stup);
else
printf("not find\n");
ListDelete(handle, name, NameCmp);
ListDelete(handle, &id, IdCmp);
ListDisplay(handle, print_s);
ListDispose(handle);
return 0;
}
----------------------------------------------#ifndef _LIST_H__
#define _LIST_H__
#define HEADINSERT1
#define TAILINSERT 2
struct listnode;
struct headnode;
typedef struct headnode *LIST;
typedef struct listnode *PtrNode;
typedef void print(const void *);
typedef int cmp(const void *, const void *);
LIST ListCreate(int);
int ListInsert(LIST, const void *, int);
void *ListFind(LIST, const void *, cmp *);
int ListDelete(LIST, const void *, cmp *);
int ListFetch(LIST, const void *, cmp *, void *);
void ListDisplay(LIST, print *);
void ListDispose(LIST);
struct listnode{
struct listnode *prev;
struct listnode *next;
char data[0];
};
struct headnode{
int size;
struct listnode head;
};
#endif
----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
LIST ListCreate(int size) //LIST 说明返回值handle是struct node * 类型的
{
LIST handle = malloc(sizeof(*handle));
if(NULL == handle)
return NULL;
handle->size = size;
handle->head.next = handle->head.prev = &handle->head;
return handle;
}
int ListInsert(LIST l, const void *data, int mode)//l 为指向头节点的指针handle, data类型为const void *类型,要接受DATA类型变量stu的地址。
{
PtrNode cur = malloc(sizeof(*cur) + l->size); //cur为struct listnode *类型,分配指针域和数据域空间。
if(NULL == cur)
return -1;
memcpy(cur->data, data, l->size); //
if(mode == HEADINSERT) //先连后断
{
cur->prev = &l->head;
cur->next = l->head.next;
}
else if(mode == TAILINSERT)
{
cur->prev = l->head.prev;
cur->next = &l->head;
}
else
{
free(cur);
return -2;
}
cur->next->prev = cur;
cur->prev->next = cur;
return 0;
}
//查找数据,id或name,通过调用main中的IdCmp或NameCmp函数(函数名为函数的地址,所以加*/ cmp * funp) 而cmp表示函数的类型为参数为(const void * ,const void*)返回值为int。
static PtrNode find(LIST l, const void *key, cmp *funp)
{
PtrNode p = l->head.next;
for(; p != &l->head && funp(key,p->data); p = p->next);
return p; //查找返回值为PtrNode 类型
}
void *ListFind(LIST l, const void *key, cmp *funp) //查找数据
{
PtrNode p = find(l, key, funp);
if(p == &l->head)
return NULL;
return p->data; // 返回值为void *类型 //data本身是listnode节点的数据域data[0]的地址,p->data表示地址。
}
int ListDelete(LIST l, const void *key, cmp *funp)
{
PtrNode p = find(l, key, funp);
if(p == &l->head)
return -1;
p->next->prev = p->prev;
p->prev->next = p->next;
free(p);
return 0;
}
int ListFetch(LIST l, const void *key, cmp *funp, void *data)
{
PtrNode p = find(l, key, funp);
if(p == &l->head)
return -1;
memcpy(data, p->data, l->size);
p->next->prev = p->prev;
p->prev->next = p->next;
free(p);
return 0;
}
void ListDisplay(LIST l, print * funp)// print为返回值为void,参数为const void * 类型,函数调传地址,
{
PtrNode p = l->head.next;// p指向第一个节点
while(p != &l->head)
{
funp(p->data);
p = p->next;
}
}
void ListDispose(LIST l)
{
PtrNode p = l->head.next;
PtrNode q;
while(p != &l->head)
{
q = p;
p = p->next;
free(q);
}
free(l);
}
分享到:
相关推荐
Optimal-design-methodology-of-bidirectional-LLC-resonant-DC-DC-converter-for-solid-state-transformer.pdf 双向LLC设计参考资料,个人感觉讲的是比较详细的资料
Recurrent neural networks, particularly the long short-term memory networks, are extremely appealing for sequence-tosequence learning tasks. Despite their great success, they typically suffer from a ...
11双向优化,通过起点与终点双点发展扩展树,优化处理速度。
Utilizing the Bidirectional Effect of Evolutive Trust-Rating for Recommendation in E-commerce
基于注意力的双向长期短期记忆网络用于关系分类使用卷积递归神经网络的关系提取挑战的深度学习方法( )。实验结果楷模测试数据准确性F1分数BiLSTMAtt-softmax(dim:1000) 71.58% 76.43 BiLSTMAtt排名(dim:1000...
We have projected and verified a bidirectional intra-/inter-radio-access-technology carrier-aggregation method for a next-generation heterogeneous mobile network supported by filter bank multicarrier ...
具有LSTM GRU的双向堆叠RNN 在这里,我为Twitter情感分析数据集使用带有LSTM / GRU单元的双向堆叠RNN开发了情感分类器,。 为了开发模型,我尝试了堆叠RNN的数量,隐藏层的数量,像元类型,跳过连接,梯度修剪和丢失...
In addition the book presents the state-of-the-art in computational photonics techniques, covering methods such as full-vectorial finite-element beam propagation, bidirectional beam propagation, ...
-l, --len #[KM] length of buffer to read or write (default 8 KB) -m, --print_mss print TCP maximum segment size (MTU - TCP/IP header) -o, --output output the report or error message to this ...
In this Letter, we report on a novel architecture for a self-starting mode-locked figure-eight erbium-doped fiber laser using a loss-imbalanced nonlinear optical loop mirror (NOLM) with a ...
In the design of TPMS, the character of Atmel low frequency communication ATA5283, ATA5275 are introduced. The corresponding communication system for low frequency Wake-up are described in detail. ...
使用Seq2Seq双向LSTM单词嵌入的语言翻译模型 从英语翻译成法语 楷模 在本节中,使用了4种不同的模型。 模型1是一个简单的LSTM 模型2是具有嵌入功能的LSTM 模型3是具有嵌入功能的双向LSTM 模型4是具有双向LSTM和...
利用内在注意实现句子比较模型 参考 资料参考 1.内心关注句子的比较。 在本文中,SNLI数据用于查找两个句子之间的关系(对比度,蕴含度,中性), 在这段代码中,我基于一个模型确定了模型,该模型确定Quora中的两...
双向DC-DC设计优化,建模控制,Bidirectional dc-dc converter, high power density, complementary gating control operation, averaged model, general-purposed power stage modeling, modeling and control, ...
In other words, an EV with bidirectional charging won't just turn the alternating current (AC) from your wall plug to direct current (DC) to charge it; it can also turn the stored energy in its ...
科学可视化 医学图像可视化 医学图像可视化的应用 医学图像可视化分类
this code predicts the satellite signal from 0.25 to...spectral bands of satellite for spectral conditions) or define your own conditions(in the example,you have to define the assumed spectral response).
Linked data structure 3 Succinct data structure 6 Implicit data structure 8 Compressed data structure 9 Search data structure 10 Persistent data structure 11 Concurrent data structure 18 Abstract data...
We report a flexible all-optical gain controlled bidirectional optical amplifier. The device achieves constant gain and low noise figure over a large input power range. Moreover, the device removes ...
Bidirectional Recurrent Convolutional Networks for Multi-Frame Super-Resolution