關于鏈表的一些知識
ifndef LINKLIST_H
define LINKLIST_H
typedef void Linklist;
typedef struct _tag_LinkListNode
{
struct _tag_LinkListNode *next;
}LinkListNode;
Linklist* LinkList_Create(); //鏈表的創(chuàng)建
void LinkList_Destory(Linklist *list); //銷毀鏈表
void LinkList_Clear(Linklist *list); //清空鏈表數據
int Linklist_length(Linklist *list); //獲取鏈表長度
void Linklist_Insert(Linklist *list,LinkListNode *node,int pos); //插入數據到鏈表
LinkListNode* Linklist_get(Linklist* list,int pos); //獲取鏈表某一位置數據
LinkListNode* Linklist_Delete(Linklist *list,int pos); //刪除某以數據
endif // LINKLIST_H
include "linklist.h"
include <stdlib.h>
include <stdio.h>
typedef struct _tag_LinkList
{
LinkListNode header;
int length;
}TLinkList;
Linklist* LinkList_Create() //鏈表的創(chuàng)建
{
//g分配內存
TLinkList *Tlist = (TLinkList *)malloc(sizeof(TLinkList));
if(Tlist == NULL){
return NULL;
}
Tlist->header.next = NULL;
Tlist->length = 0;
return Tlist;
}
void LinkList_Destory(Linklist *list) //銷毀鏈表
{
if(list != NULL){
free(list);
}
}
void LinkList_Clear(Linklist *list) //清空鏈表數據
{
TLinkList *tlist = (TLinkList *)list;
if(tlist ==NULL)
{
return;
}
tlist->length = 0;
tlist->header.next = NULL;
}
int Linklist_length(Linklist *list) //獲取鏈表長度
{
TLinkList *tlist = (TLinkList *)list;
if(tlist ==NULL)
{
return -1;
}
return tlist->length;
}
void Linklist_Insert(Linklist *list,LinkListNode *node,int pos) //插入數據到鏈表
{
TLinkList *tlist = (TLinkList *)list;
LinkListNode *current = NULL;
if(list == NULL || node == NULL || pos < 0){
return;
}
//要找到插入的前一個
current = &tlist->header;
for (int i = 0; (i < pos)&&(current->next!=NULL);i++){
current = current->next;
}
node->next = current->next;
current->next = node;
tlist->length++;
return;
}
LinkListNode* Linklist_get(Linklist* list,int pos) //獲取鏈表某一位置數據
{
TLinkList *tlist = (TLinkList *)list;
LinkListNode *current = NULL;
if(list == NULL || pos < 0 || pos >tlist->length){
return NULL;
}
//要找到插入的前一個
current = &tlist->header;
for (int i = 0; (i < pos)&&(current->next!=NULL);i++){
current = current->next;
}
return current->next;
}
LinkListNode* Linklist_Delete(Linklist *list,int pos) //刪除某以數據
{
TLinkList *tlist = (TLinkList *)list;
LinkListNode *current = NULL;
if(list == NULL || pos < 0 || pos >tlist->length){
return NULL;
}
//要找到插入的前一個
current = &tlist->header;
for (int i = 0; (i < pos)&&(current->next!=NULL);i++){
current = current->next;
}
LinkListNode *temp = current->next;
current->next = temp->next;
tlist->length--;
return temp;
}