C 摆脱FIFO结构的中间单元
我使用FIFO结构来获得时间线的简化版本。问题是,当一条消息被喜欢时,它必须再次进入结构的顶部 为此,我想把它从堆里拿出来,再堆在上面,更新喜欢的数量 遗憾的是,我想不出一个简单的方法来让FIFO结构中间的一个单元离开它。 在timeline.h文件中:C 摆脱FIFO结构的中间单元,c,timeline,fifo,C,Timeline,Fifo,我使用FIFO结构来获得时间线的简化版本。问题是,当一条消息被喜欢时,它必须再次进入结构的顶部 为此,我想把它从堆里拿出来,再堆在上面,更新喜欢的数量 遗憾的是,我想不出一个简单的方法来让FIFO结构中间的一个单元离开它。 在timeline.h文件中: typedef struct tipoConteudoMsg { int idUsuario; int idMsg; char msg[280]; int tempo; int curtidas; } t
typedef struct tipoConteudoMsg {
int idUsuario;
int idMsg;
char msg[280];
int tempo;
int curtidas;
} tipoConteudoMsg;
typedef struct tipoMsg *apontadorTimeline;
typedef struct tipoMsg {
tipoConteudoMsg cont; // content of the message;
apontadorTimeline prox;
} tipoMsg; // celula
typedef struct tipoTimeline {
apontadorTimeline fundo, topo;
int tamanho;
} tipoTimeline; // pilha
我可以获取指向具有我需要的消息ID的单元格的指针,也可以获取指向堆上它正上方的单元格的指针
在timeline.c文件中,以防人们需要了解我是如何实现FIFO结构的:
// fazTimelineVazia means "create empty timeline (FIFO structure)
void fazTimelineVazia (tipoTimeline *timeline) {
timeline->topo = (apontadorTimeline) malloc(sizeof(tipoMsg));
timeline->fundo = timeline->topo;
timeline->topo->prox = NULL;
timeline->tamanho = 0;
}
// empilhaTimeline means "pile message on top of the FIFO structure"
void empilhaTimeline (tipoTimeline *timeline, tipoConteudoMsg msg) {
apontadorTimeline aux;
aux = (apontadorTimeline) malloc(sizeof(tipoMsg));
timeline->topo->cont = msg;
aux->prox = timeline->topo;
timeline->topo = aux;
timeline->tamanho++;
}
如果你需要重新排序,你首先不应该使用FIFO。如果你需要重新排序,你首先不应该使用FIFO。