双向链表的实现

llist.h

#pragma once
#ifndef LLIST_H__
#define LLIST_H__

#define FORM 1
#define BACK 2
typedef void llist_op(void*);
typedef int llist_fd(void*, void*);

typedef struct node_st
{
	void* data;
	struct node_st* next;
	struct node_st* pre;
}node ;

typedef struct
{
	int size;
	node node;

}LLIST;



LLIST* llist_creat(int size);


int llist_insert(LLIST * head,void *data,int mode);

int llist_delete(LLIST *,void* ,llist_fd);

int llist_fetch(LLIST*, void* key, llist_fd*, void* data);

void* llist_find(LLIST *,void *key, llist_fd*);


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_creat(int size)
{
	LLIST* head;
	head = (LLIST*)malloc(sizeof(*head));
	if (head == NULL)
		return NULL;

	head->size = size;
	head->node.data = NULL;
	head->node.pre = &(head->node);
	head->node.next = &(head->node);
	return head;
}

//输入模式错误,返回-3
//创建失败,返回-1
int llist_insert(LLIST* head, void* data, int mod)
{
	node* nd;
	nd = (node*)malloc(sizeof(*nd));
	if (nd == NULL) {
		return -1;
	}

	nd->data = malloc(head->size);
	if (nd->data == NULL)
		return -2;
	memcpy(nd->data, data, head->size);
	if (mod == FORM)
	{
		nd->next = head->node.next;
		nd->pre = &(head->node);
	}
	else if (mod == BACK)
	{
		nd->next = &(head->node);
		nd->pre = head->node.pre;
	}
	else
	{
		return -3;
	}
	nd->pre->next = nd;
	nd->next->pre = nd;
	return 0;
}


int llist_delete(LLIST* head)
{

	return 0;
}

static node* find_(LLIST* head, void* key, llist_fd* fd)
{
	node* nd = head->node.next;
	while (nd != &head->node)
	{
		if (fd(nd->data, key) == 0)
		{
			break;
		}
		nd = nd->next;
	}
	return nd;
}

void* llist_find(LLIST* head,void* key, llist_fd* fd)
{
	return find_(head, key, fd)->data;
}


void llist_travel(LLIST* head, llist_op* op)
{
	node* nd;
	nd = head->node.next;
	while (nd != &(head->node))
	{
		op(nd->data);
		nd = nd->next;
	}

}


int llist_delete(LLIST* head,void* key,llist_fd* fd)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;
	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd->data);
	free(nd);
	return 0;
}

int llist_fetch(LLIST* head, void* key, llist_fd* fd, void* data)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;

	if (nd->data != NULL)
		memcpy(data, nd->data, head->size);
	
	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd->data);
	free(nd);
	return 0;

}




void llist_destroy(LLIST* head)
{
	node* p;
	p = head->node.next;

	while (p != &head->node)
	{
		p = p->next;
		free(p->pre->data);
		free(p->pre);
	}
	free(head);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"

#define NAMESIZE 8

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

}score;

static void print_s(void* data)
{
	score* sc = (score*)data;
	printf("%d %s %d %d\n", sc->id, sc->name, sc->math, sc->chinese);
}

static int find_s(void* data,void* key)
{
	score* sc = (score*)data;
	int* k = (int*)key;
	return (*k - sc->id);
}

static int find_name(void* data, void* key)
{
	score* sc = (score*)data;
	const char* name = (char*)key;
	return strcmp(sc->name, name);

}


int main()
{
	LLIST* head;
	score temp,st;
	int ret;
	head = llist_creat(sizeof(temp));
	if (head == NULL)
		exit(1);
	for (int i = 0; i < 7; i++)
	{
		temp.id = i;
		snprintf(temp.name, NAMESIZE, "std%d", i);
		temp.math = rand() % 100;
		temp.chinese = rand() % 100;
		ret = llist_insert(head, &temp, FORM);
		if (ret != 0)
			exit(1);
	}
	
	
	llist_travel(head,print_s);
	printf("----------------------------------------\n");
	//查找
	//score* scc;
	//int id = 32;
	//scc = (score*)llist_find(head, &id, find_s);
	//if (scc == NULL)
	//	printf("没有找到\n");
	//else
	//	print_s(scc);

	int id = 3;
	char name[5] = "std3";
	ret = llist_delete(head, name, find_name);
	if (ret != 0)
		printf("EROOR!\n");
	llist_travel(head,print_s);


	llist_destroy(head);

}

---------------------------------------------------------------------------------------------------------------------------------修改为边长结构体

.h

#pragma once
#ifndef LLIST_H__
#define LLIST_H__

#define FORM 1
#define BACK 2
typedef void llist_op(void*);
typedef int llist_fd(void*, void*);

//边长结构体
typedef struct node_st
{
	struct node_st* next;
	struct node_st* pre;
	char data[0];
}node;

typedef struct
{
	int size;
	node node;

}LLIST;



LLIST* llist_creat(int size);


int llist_insert(LLIST * head,void *data,int mode);

int llist_delete(LLIST *,void* ,llist_fd);

int llist_fetch(LLIST*, void* key, llist_fd*, void* data);

void* llist_find(LLIST *,void *key, llist_fd*);


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_creat(int size)
{
	LLIST* head;
	head = (LLIST*)malloc(sizeof(*head));
	if (head == NULL)
		return NULL;

	head->size = size;
	head->node.pre = &(head->node);
	head->node.next = &(head->node);
	return head;
}


int llist_insert(LLIST* head, void* data, int mod)
{
	node* nd;
	nd = (node*)malloc(sizeof(*nd) + head->size);
	if (nd == NULL) {
		return -1;
	}

	memcpy(nd->data, data, head->size);
	if (mod == FORM)
	{
		nd->next = head->node.next;
		nd->pre = &(head->node);
	}
	else if (mod == BACK)
	{
		nd->next = &(head->node);
		nd->pre = head->node.pre;
	}
	else
	{
		return -3;
	}
	nd->pre->next = nd;
	nd->next->pre = nd;
	return 0;
}

int llist_delete(LLIST* head)
{

	return 0;
}


static node* find_(LLIST* head, void* key, llist_fd* fd)
{
	node* nd = head->node.next;
	while (nd != &head->node)
	{
		if (fd(nd->data, key) == 0)
		{
			break;
		}
		nd = nd->next;
	}
	return nd;
}

void* llist_find(LLIST* head, void* key, llist_fd* fd)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return NULL;
	return nd->data;
}


void llist_travel(LLIST* head, llist_op* op)
{
	node* nd;
	nd = head->node.next;
	while (nd != &(head->node))
	{
		op(nd->data);
		nd = nd->next;
	}

}


int llist_delete(LLIST* head, void* key, llist_fd* fd)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;
	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd);
	return 0;
}

int llist_fetch(LLIST* head, void* key, llist_fd* fd, void* data)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;

	if (nd->data != NULL)
		memcpy(data, nd->data, head->size);

	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd);
	return 0;

}




void llist_destroy(LLIST* head)
{
	node* p;
	p = head->node.next;

	while (p != &head->node)
	{
		p = p->next;
		free(p->pre);
	}
	free(head);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"

#define NAMESIZE 8

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

}score;

static void print_s(void* data)
{
	score* sc = (score*)data;
	printf("%d %s %d %d\n", sc->id, sc->name, sc->math, sc->chinese);
}

static int find_s(void* data,void* key)
{
	score* sc = (score*)data;
	int* k = (int*)key;
	return (*k - sc->id);
}

static int find_name(void* data, void* key)
{
	score* sc = (score*)data;
	const char* name = (char*)key;
	return strcmp(sc->name, name);

}


int main()
{
	LLIST* head;
	score temp,st;
	int ret;
	head = llist_creat(sizeof(temp));
	if (head == NULL)
		exit(1);
	for (int i = 0; i < 7; i++)
	{
		temp.id = i;
		snprintf(temp.name, NAMESIZE, "std%d", i);
		temp.math = rand() % 100;
		temp.chinese = rand() % 100;
		ret = llist_insert(head, &temp, FORM);
		if (ret != 0)
			exit(1);
	}
	
	
	llist_travel(head,print_s);
	printf("----------------------------------------\n");
	//查找
	//score* scc;
	//int id = 32;
	//scc = (score*)llist_find(head, &id, find_s);
	//if (scc == NULL)
	//	printf("没有找到\n");
	//else
	//	print_s(scc);

	int id = 3;
	char name[5] = "std3";
	ret = llist_delete(head, name, find_name);
	if (ret != 0)
		printf("EROOR!\n");
	llist_travel(head,print_s);


	llist_destroy(head);

}

------------------------------------------------------------------------------------------------------------------------------------------------将方法封装到结构体中

llist.h

#pragma once
#ifndef LLIST_H__
#define LLIST_H__

#define FORM 1
#define BACK 2
typedef void llist_op(void*);
typedef int llist_fd(void*, void*);

//边长结构体
typedef struct node_st
{
	struct node_st* next;
	struct node_st* pre;
	char data[0];
}node;

typedef struct llist_head
{
	int size;
	//定义了一个函数指针,返回值为int,参数类型如下,名为insert
	int (*insert)(struct llist_head*, void* , int );
	void* (*find)(struct llist_head*, void* key, llist_fd*);
	int (*delet)(struct llist_head*, void*, llist_fd);
	int (*fetch)(struct llist_head*, void* key, llist_fd*, void* data);
	void (*trave)(struct llist_head*, llist_op*);
	void (*destroy)(struct llist_head*);
	struct node_st node;
}LLIST;



LLIST* llist_creat(int size);
void llist_destroy(LLIST*);


#endif

llist.c

#include <stdio.h>
#include <stdlib.h>
#include <String.h>
#include "llist.h"

int llist_insert(LLIST *, void* , int );
void* llist_find(LLIST*, void* key, llist_fd*);
int llist_delete(LLIST*, void*, llist_fd);
int llist_fetch(LLIST*, void* key, llist_fd*, void* data);
void llist_travel(LLIST*, llist_op*);
void llist_destroy(LLIST*);

LLIST* llist_creat(int size)
{
	LLIST* head;
	head = (LLIST*)malloc(sizeof(*head));
	if (head == NULL)
		return NULL;

	head->size = size;
	head->node.pre = &(head->node);
	head->node.next = &(head->node);
	//赋值,让结构体里的函数指针指向函数,注意函数没有'()'
	head->insert = llist_insert;
	head->delet = llist_delete;
	head->find = llist_find;
	head->fetch = llist_fetch;
	head->trave = llist_travel;


	return head;
}


int llist_insert(LLIST* head, void* data, int mod)
{
	node* nd;
	nd = (node*)malloc(sizeof(*nd) + head->size);
	if (nd == NULL) {
		return -1;
	}

	memcpy(nd->data, data, head->size);
	if (mod == FORM)
	{
		nd->next = head->node.next;
		nd->pre = &(head->node);
	}
	else if (mod == BACK)
	{
		nd->next = &(head->node);
		nd->pre = head->node.pre;
	}
	else
	{
		return -3;
	}
	nd->pre->next = nd;
	nd->next->pre = nd;
	return 0;
}

int llist_delete(LLIST* head)
{

	return 0;
}


static node* find_(LLIST* head, void* key, llist_fd* fd)
{
	node* nd = head->node.next;
	while (nd != &head->node)
	{
		if (fd(nd->data, key) == 0)
		{
			break;
		}
		nd = nd->next;
	}
	return nd;
}

void* llist_find(LLIST* head, void* key, llist_fd* fd)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return NULL;
	return nd->data;
}


void llist_travel(LLIST* head, llist_op* op)
{
	node* nd;
	nd = head->node.next;
	while (nd != &(head->node))
	{
		op(nd->data);
		nd = nd->next;
	}

}


int llist_delete(LLIST* head, void* key, llist_fd* fd)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;
	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd);
	return 0;
}

int llist_fetch(LLIST* head, void* key, llist_fd* fd, void* data)
{
	node* nd;
	nd = find_(head, key, fd);
	if (nd == &head->node)
		return -1;

	if (nd->data != NULL)
		memcpy(data, nd->data, head->size);

	nd->pre->next = nd->next;
	nd->next->pre = nd->pre;
	free(nd);
	return 0;

}




void llist_destroy(LLIST* head)
{
	node* p;
	p = head->node.next;

	while (p != &head->node)
	{
		p = p->next;
		free(p->pre);
	}
	free(head);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"

#define NAMESIZE 8

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

}score;

static void print_s(void* data)
{
	score* sc = (score*)data;
	printf("%d %s %d %d\n", sc->id, sc->name, sc->math, sc->chinese);
}

static int find_s(void* data,void* key)
{
	score* sc = (score*)data;
	int* k = (int*)key;
	return (*k - sc->id);
}

static int find_name(void* data, void* key)
{
	score* sc = (score*)data;
	const char* name = (char*)key;
	return strcmp(sc->name, name);

}


int main()
{
	LLIST* head;
	score temp,st;
	int ret;
	head = llist_creat(sizeof(temp));
	if (head == NULL)
		exit(1);
	for (int i = 0; i < 7; i++)
	{
		temp.id = i;
		snprintf(temp.name, NAMESIZE, "std%d", i);
		temp.math = rand() % 100;
		temp.chinese = rand() % 100;
		//定义的头结点名为head,head里封装了方法
		ret = head->insert(head, &temp, FORM);
		if (ret != 0)
			exit(1);
	}
	
	
	head->trave(head,print_s);

/*

	printf("----------------------------------------\n");
	//查找
	//score* scc;
	//int id = 32;
	//scc = (score*)llist_find(head, &id, find_s);
	//if (scc == NULL)
	//	printf("没有找到\n");
	//else
	//	print_s(scc);

	int id = 3;
	char name[5] = "std3";
	ret = llist_delete(head, name, find_name);
	if (ret != 0)
		printf("EROOR!\n");
	llist_travel(head,print_s);


	llist_destroy(head);
	*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值