📄 link.c
字号:
#include "link.h"
#include <malloc.h>
#include <stdio.h>
/* 从链表头结点插入 */
void link_insert_front(struct LINKLIST* THIS,void *data,int size)
{
NODE *newNode=(NODE *)malloc(sizeof(NODE));
//void* tdata =(void *)malloc(size);
newNode->data=(void *)malloc(size);
memset(newNode->data,0,size);
//memset(tdata, 0 ,size);
memcpy(newNode->data,data,size);
//memcpy(tdata, data ,size);
// newNode->data =tdata;
newNode->prior=THIS->head;
newNode->next=THIS->head->next;
THIS->head->next->prior=newNode;
THIS->head->next=newNode;
THIS->count++;
}
/* 从链表尾结点插入*/
void link_insert_rear(struct LINKLIST* THIS,void *data,int size)
{
NODE *newNode=(NODE *)malloc(sizeof(NODE));
newNode->data=(void *)malloc(size);
memcpy(newNode->data,data,size);
newNode->next=THIS->rearptr;
newNode->prior=THIS->rearptr->prior;
THIS->rearptr->prior->next=newNode;
THIS->rearptr->prior=newNode;
THIS->count++;
}
/* 从链表某个结点插入*/
void link_insert_at(struct LINKLIST* THIS,int index,void *data,int size)
{
/* int i=0;
NODE *temp=THIS->head->next;
while(temp)
{
i++;
if(i==index)
break;
temp=temp->next;
}
if(!temp) return;
*/
NODE *p=NULL;
NODE *temp=THIS->get_node_by_index(THIS,index);
if(!temp) return;
p=(NODE *)malloc(sizeof(NODE));
if(!p) return;
p->data=malloc(size);
if(!p->data)
{
free(p);
return;
}
memcpy(p->data,data,size);
p->prior=temp;
p->next=temp->next;
p->next->prior=p;
temp->next=p;
THIS->count++;
}
/*通过结点编号获得结点*/
static NODE* link_get_node_by_index(struct LINKLIST* THIS,int index)
{
int i=0;
NODE *temp=THIS->rearptr;
if (index<=0||index>THIS->count)
{
return NULL;
}
else
{
for (i=0;i<index;i++)
{
temp=temp->prior;
}
return temp;
}
}
/* 根据key值,并通过一定的比较算法cmp获取链表中的结点 */
NODE* link_get_node_by_key(struct LINKLIST* THIS,char* key,int (*condition)(void *p1,void *p2))
{
NODE *temp=THIS->rearptr->prior;
if (THIS->count==0)
{
return NULL;
}
else
{
while (temp->prior!=NULL)
{
if (condition(temp,key))
{
return temp;
}
else
{
temp=temp->prior;
}
}
return NULL;
}
}
/* 获取结点在链表中的位置 */
int link_get_index_by_key(struct LINKLIST* THIS,char* key,int (*condition)(void *p1,void *p2))
{
NODE *temp=THIS->rearptr->prior;
int i=1;
if (THIS->count==0)
{
return 0;
}
else
{
while (temp->prior!=NULL)
{
if (condition(temp,key))
{
return i;
}
else
{
temp=temp->prior;
i++;
}
}
return 0;
}
}
/*通过关键字删除结点*/
static int link_del_node_by_key(struct LINKLIST* THIS,char* key,int (*condition)(void *p1,void *p2))
{
NODE *p,*temp=THIS->head->next;
while(temp)
{
if(condition(temp,key))
{
p=temp;
temp->prior->next=temp->next;
temp->next->prior=temp->prior;
THIS->count--;
free(p->data);
free(p);
return 1;
}else
{
temp=temp->next;
}
}
return 0;
}
/*删除第几个结点*/
int link_del_node_by_index(struct LINKLIST* THIS,int index)
{
NODE *temp=link_get_node_by_index(THIS,index);
if (temp==NULL)
{
return 0;
}
else
{
temp->next->prior=temp->prior;
temp->prior->next=temp->next;
THIS->count--;
free(temp->data);
free(temp);
temp = NULL;
return 1;
}
}
/* 删除链表结点,但是不删除头尾结点 */
void link_del_all_data(struct LINKLIST* THIS)
{
NODE *temp=THIS->head->next;
while (temp->next!=NULL)
{
THIS->head->next=temp->next;
temp->next->prior=THIS->head;
free(temp->data);
free(temp);
THIS->count--;
temp=THIS->head->next;
}
}
/* 删除链表,包括链表头表尾结点,本身结构体 */
void link_del_all(struct LINKLIST** THIS)
{
NODE *temp=(*THIS)->head->next;
while (temp->next!=NULL)
{
(*THIS)->head->next=temp->next;
temp->next->prior=(*THIS)->head;
free(temp->data);
free(temp);
(*THIS)->count--;
temp=(*THIS)->head->next;
}
free((*THIS));
}
int link_len(struct LINKLIST* THIS)
{
NODE *temp=THIS->head->next;
int count=0;
while (temp->next!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
/*创建一个链表*/
LINK * new_link()
{
LINK * link=NULL;
NODE *node=NULL;
NODE *node1=NULL;
link=(LINK *)malloc(sizeof(LINK));
if(link==NULL) return link;
node = (NODE*)malloc(sizeof(NODE));
node1= (NODE*)malloc(sizeof(NODE));
if(node==NULL)
{
if (node1==NULL)
{
free(link);
return NULL;
}
else
{
free(link);
free(node1);
return NULL;
}
}else
{
if (node1==NULL)
{
free(link);
free(node);
return NULL;
}
}
link->head = node;
link->rearptr = node1;
node->next=node1;
node->prior=NULL;
node1->next=NULL;
node1->prior=node;
link->count = 0;
link->insert_front=link_insert_front;
link->insert_rear=link_insert_rear;
link->insert_at=link_insert_at;
link->get_node_by_index=link_get_node_by_index;
link->get_node_by_key=link_get_node_by_key;
link->get_index_by_key=link_get_index_by_key;
link->del_node_by_key=link_del_node_by_key;
link->del_node_by_index=link_del_node_by_index;
link->del_all_data=link_del_all_data;
link->del_all=link_del_all;
link->len=link_len;
return link;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -