当前位置: 代码迷 >> 综合 >> 网络银行——day02
  详细解决方案

网络银行——day02

热度:33   发布时间:2023-11-24 09:39:43.0

冲冲冲!!!!


1.slinklist

slinklist.h

#ifndef _SLINKLIST_H__
#define _SLINKLIST_H__#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>typedef struct Slink{
    	void *elem;struct Slink *next;
}*Slink;Slink slink_list_create();
int slink_list_insert(Slink list,void *elem,size_t elemsize);
Slink slink_list_find(Slink list,void *elem,int (*cmp)(const void *,const void *));
int slink_list_delete(Slink list,void *elem,int (*cmp)(const void *,const void *));
void slink_list_destroy(Slink list);#endif //_SLINKLIST_H__

slinklist.c

#include "slinklist.h"Slink slink_list_create(){
    Slink head = malloc(sizeof(struct Slink));assert(head != NULL);head->elem = NULL;head->next = NULL;return head;
}int slink_list_insert(Slink list,void *elem,size_t elemsize){
    assert(list != NULL);Slink node = malloc(sizeof(struct Slink));assert(node != NULL);node -> elem = malloc(elemsize);if(node->elem != NULL){
    free(node);return -1;}node->next = list->next;list->next = node;memcpy(node->elem,elem,elemsize);return 1;
}Slink slink_list_find(Slink list,void *elem,int (*cmp)(const void *,const void *)){
    assert(list != NULL);Slink next = list ->next;while(next != NULL){
    if(cmp(next->elem,elem) == 0){
    return next;}next = next->next;}return NULL;
}int slink_list_delete(Slink list,void *elem,int (*cmp)(const void *,const void *)){
    assert(list != NULL);Slink next = list->next;Slink node = list;while(next != NULL){
    if(cmp(next->next->elem,elem) == 0){
    node = next->next;free(next->elem);free(next);return 1;}next = next->next;node = node->next;}return 0;
}void slink_list_delete(Slink list){
    assert(list != NULL);Slink next = list->next;Slink node = list;while(next != NULL){
    node = next -> next;free(next ->elem);free(next);next = node;}list->next = NULL;free(list);
}