单向链表
单向链表
- 单向链表存储空间是不连续的,它有数据和指向下一个节点的首地址组成
代码示例
list.h
#include <stdio.h>
#include <stdlib.h>
//节点结构体
typedef struct listnode
{
void* data;
struct listnode* next;
}ListNode;
//链表信息结构体
typedef struct listinfo
{
ListNode* head;
int size;
}ListInfo;
typedef void(*PRINTLISTNODE)(void*);//定义函数指针,给Print函数用,参数void*,返回值void
typedef int (*JUDGEEQUAL)(void*,void*);//Find_list的形参,判断相等函数指针,用户提供实现,传入
ListInfo* Init_List();
void Insert_list(ListInfo* list,int pos,void* data);
void RemoveByPos_list(ListInfo* list,int pos);
int Size_list(ListInfo* list);
int Find_list(ListInfo* list,void* data,JUDGEEQUAL judge_equal);
void* Front_list(ListInfo* list);
void Free_list(ListInfo* list);
void Print(ListInfo* list,PRINTLISTNODE print);
list.c
#include "../include/list.h"
//初始化链表
ListInfo* Init_List()
{
ListInfo* list = (ListInfo*)malloc(sizeof(ListInfo));
list->size=0;
list->head=(ListNode*)malloc(sizeof(ListNode));
list->head->data=NULL;
list->head->next=NULL;
return list;
}
//插入节点
void Insert_list(ListInfo* list,int pos,void* data)
{
if(list == NULL) return;
if(pos<0 || pos>list->size)return;
ListNode* NewNode=(ListNode*) malloc(sizeof(ListNode));
NewNode->data=data;
NewNode->next=NULL;
ListNode* pCurrent = list->head;
for(int i=0; i<pos; i++)
{
pCurrent = pCurrent->next;
}
NewNode->next = pCurrent->next;
pCurrent->next = NewNode;
list->size++;
}
//删除节点
void RemoveByPos_list(ListInfo* list,int pos)
{
if(pos<0 || pos>list->size)
return ;
ListNode* pCurrent = list->head;
for(int i=0; i<pos; i++)
{
pCurrent=pCurrent->next;
}
ListNode* tmp=pCurrent->next;
pCurrent->next=pCurrent->next->next;
free(tmp);
list->size--;
}
//返回链表长度
int Size_list(ListInfo* list)
{
return list->size;
}
//查找节点,找到返回位置
int Find_list(ListInfo* list,void* data)
{
if(list==NULL)return -1;
ListNode* pCurrent=list->head;
for(int i=0; i<list->size; i++)
{
pCurrent=pCurrent->next;
if(judge_equal(pCurrent->data,data1))
{
return i;
}
}
return -1;
}
//返回第一个节点
void* Front_list(ListInfo* list)
{
return list->head->next->data;
}
//释放链表
void Free_list(ListInfo* list)
{
if(list == NULL)exit(1);
ListNode* pCurrent = list->head;
while(pCurrent != NULL)
{
ListNode* tmp =pCurrent->next;
free(pCurrent);
pCurrent = tmp;
}
free(list);
}
//打印链表
void Print(ListInfo* list,PRINTLISTNODE print)
{
if(list == NULL)return;
ListNode* pCurrent = list->head->next;
while(pCurrent != NULL)
{
print(pCurrent->data);
pCurrent = pCurrent->next;
}
}
#include <stdio.h>
#include <stdlib.h>
#include "../include/list.h"
typedef struct PERSON
{
char name[64];
int age;
int score;
}Person;
void MyPrint(void* data)
{
Person* p =(Person*)data;
printf("name: %s,age: %d,score: %d\n",p->name,p->age,p->score);
}
int Judge_Equal(void* data1,void* data2)
{
Person* p1 =(Person*)data1;
Person* p2 =(Person*)data2;
if(p1->age==p2->age && p1->name==p2->name && p1->score == p2->score)
{
return 1;
}
else
{
return 0;
}
}
int main(void)
{
ListInfo* list = Init_List();
Person p1 ={ "julian",18,90};
Person p6 ={ "kerr",12,60};
Person p5 ={ "mike",13,30};
Person p4 ={ "john",13,93};
Person p3 ={ "lucy",17,90};
Person p2 ={ "candy",17,80};
Insert_list(list,0,&p1);
Insert_list(list,1,&p6);
Insert_list(list,2,&p2);
Insert_list(list,3,&p3);
Insert_list(list,4,&p4);
Insert_list(list,5,&p5);
Print(list,MyPrint);
printf("----------删除第2个元素---------\n");
RemoveByPos_list(list,2);
Print(list,MyPrint);
printf("--------返回第一个元素-----------\n");
Person* tmp= (Person*)Front_list(list);
printf("name: %s,age: %d,score: %d\n",tmp->name,tmp->age,tmp->score);
printf("---------查找p3,lucy------------------");
int position = Find_list(list,&p3,Judge_Equal);
printf("pos: %d\n",position);
Free_list(list);
return 0;
}
结果
name: julian,age: 18,score: 90
name: kerr,age: 12,score: 60
name: candy,age: 17,score: 80
name: lucy,age: 17,score: 90
name: john,age: 13,score: 93
name: mike,age: 13,score: 30
--------删除第二个元素-----------
name: julian,age: 18,score: 90
name: kerr,age: 12,score: 60
name: lucy,age: 17,score: 90
name: john,age: 13,score: 93
name: mike,age: 13,score: 30
---------返回第一个元素---------
name: julian,age: 18,score: 90
---------查找p3,lucy------------------
pos: 2
还没有评论,来说两句吧...