‘’’
二次封装 借用已经实现双向链表结构来实现
队列 出对入对操作类似于 节点之前的插入和删除
更为灵活
‘’’
llist.h
#ifndef LLIST_H__
#define LLSIT_H__
#define LLIST_FORWARD 1
#definr LLIST_BACKWARD 2
typedef void llist_op(const void *);//回调函数
typedef int llist_cmp(const void *,const void *);
struct llist_node_st
{
struct llist_node_st *prev;
struct llist_node_st *next;
char data[0];
};
typedef struct
{
int size;
struct llist_node_st head;
}LLSIT;
LLIST *llist_create(int initsize);
int llist_insert(LLIST *,const void *data,int mode);
void *llist_find(LLIST *, const void *key, llist_cmp *);//数据类型不统一使用void 百搭
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"llist.h"
#include<string.h>
LLIST *llist_create(int initsize)//只包含一个头节点( 双向循环链表)
{
LLIST *new;
new = malloc(sizeof(*new));
if(new == NULL)
return NULL;
new->size= initsize;
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)+ptr->size);
if(newnode == NULL)
return -1;
//newdata->data = malloc(ptr->size);
memcpy(newnode->data,data,ptr->size);
if(mode == LLIST_FORWARD)
{
newnode->prev = &ptr->head;
newnode->next = ptr->head.next;
newnode->prev->next = newnode;//头节点的 next 指针设置为指向新节点 newnode
newnode->next->prev = newnode;//原本在头节点之后的节点的 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
{
return -3;
}
return 0;
}
static struct list_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)
{
struct llist_node_st *node;
node = find_(ptr,key,cmp);
if(node == &ptr->head))
return NULL;
return node->data;
}
int llist_delete(LLIST *ptr,const void *key,llist_cmp *cmp)
{
struct llist_node_st *node;
node = nodefind_(ptr,key,cmp);
if(node == &ptr->head)
return -1;
node->prev->next = node->next;
node->next->prev = node->prev;
free(node);
return 0;
}
int llist_fetch(LLIST *ptr,const void *key,llist_cmp *cmp,void *data)
{
struct llist_node_st *node;
node = nodefind_(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);
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)//为了封装成更通用的函数,不知道用户的结构类型,因此需要回调函数,且需要在 .h文件中使用 void 函数声明,且使用typedef重命名 看起来更好一些
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);
}
free(ptr);
}
queue.h
#ifndef QUEUE_H__
#define QUEUE_H__
typedef LLIST QUEUE;
#include"llist.h"
QUEUE *queue_cretae(int);
int queue_en(QUEUE *,const void *);
int queue_de(QUEUE *,void);
void queue_destroy(QUEUE *);
#endif
queue.c
#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
UQEUE *queue_cretae(int size)
{
return llist_create(size);
}
int queue_en(QUEUE *,const void *data)
{
llist_insert(ptr,data,LLIST_BACKWARD);
}
static int always_match(const void *p1,const void *p2)
{
return 0;
}
int queue_de(QUEUE *ptr,void *data)
{
return llist_fetch(ptr,(void *)0,always_match,data);
}
void queue_destroy(QUEUE *PTR)
{
llist_destory(ptr);
}
main.c
#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
#define NAMESIZE 32
struct score_st
{
int id;
char name[NAMESIZE];
int math;
int chinese;
};
static void print_s(void *record)
{
struct score_st *r = recored;
printf("%d %s %d %d\n",r->id,r->name,r->math,r->chinese);
}
int main()
{
int i,ret;
QUEUE * = qu;
struct score_st tmp;
qu = queue_create(sizeof(struct score_st));
if(qu ==NULL)
exit(1);
for(i =0;i<6;i++)
{
tmp.id = i;
snprintf(tmp.name,NAMESIZE,"stu%d",i);
tmp.math = rand()%100;
tmp.chinese = rand()%100;
if(queue_en(qu,&tmp)!=0)
exit(1);
}
while(1)
{
ret = queue_de(qu,&tmp);
if(ret == -1)
break;
print_s(&tmp);
}
queue_destory(qu);
exit(0);
}
Makefile
all:queue
queue:queue.o main.o
$(CC) $^ -o $@
clean:
rm queue *.o -rf