save.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct linkNode{
struct linkNode *next;
};
struct pointer{
struct linkNode pHeader;
int length;
};
typedef void *link;
// 初始化链表
link init_link();
// 添加链表
void insert_value(link *list,void *value);
// 遍历链表
void link_foreach(link *list,void(*print)(void *));
// 头插
void link_unshift(link list,void *value);
// 尾插
void link_push(link list,void *value);
// 头删
void* link_shift(link list);
// 尾删
void* link_pop(link list);
// 指定位置删
void *link_splice(link list,int pos);
// 获取链表长度
int link_length(link list);
// 翻转链表
void link_reverse(link list);
// 销毁链表
void link_destory(link list);
save.c
#include "save.h"
link init_link(){
struct pointer *pNode=(struct pointer *)malloc(sizeof(struct pointer));
if(!pNode)
return NULL;
pNode->pHeader.next=NULL;
pNode->length=0;
return pNode;
}
void insert_value(link list,int pos,void *value){
if(!list||!value)
return ;
struct pointer *pNode=(struct pointer *)list;
if(pos<0||pos>pNode->length){
pos=pNode->length;
}
struct linkNode *pCur=&pNode->pHeader;
for(int i=0;i<pos;++i){
pCur=pCur->next;
}
struct linkNode * newNode = (struct linkNode *)value;
newNode->next=pCur->next;
pCur->next=newNode;
pNode->length++;
}
void link_foreach(link list,void(*print)(void *)){
if(!list||!print)
return ;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
pCur=pCur->next;
for(int i=0;i<pNode->length;++i){
print(pCur);
pCur=pCur->next;
}
}
void link_unshift(link list,void *value){
if(!list||!value)
return ;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
struct linkNode *newNode=(struct linkNode *)value;
newNode->next=pCur->next;
pCur->next=newNode;
pNode->length++;
}
void link_push(link list,void *value){
if(!list||!value)
return ;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
struct linkNode *newNode=(struct linkNode *)value;
for(int i=0;i<pNode->length;++i){
pCur=pCur->next;
}
newNode->next=pCur->next;
pCur->next=newNode;
pNode->length++;
}
void* link_shift(link list){
if(!list)
return NULL;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
void *data=pCur->next;
pCur->next=pCur->next->next;
pNode->length--;
return data;
}
void* link_pop(link list){
if(!list)
return NULL;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
for(int i=0;i<pNode->length-1;++i){
pCur=pCur->next;
}
void *data=pCur->next;
pCur->next=pCur->next->next;
pNode->length--;
return data;
}
void *link_splice(link list,int pos){
if(!list)
return NULL;
struct pointer *pNode=(struct pointer *)list;
if(pos<0||pos>pNode->length)
return NULL;
struct linkNode *pCur=&pNode->pHeader;
for(int i=0;i<pos;++i){
pCur=pCur->next;
}
void *data=pCur->next;
pCur->next=pCur->next->next;
pNode->length--;
return data;
}
int link_length(link list){
if(!list)
return -1;
struct pointer *pNode=(struct pointer *)list;
return pNode->length;
}
void link_reverse(link list){
if(!list)
return ;
struct pointer *pNode=(struct pointer *)list;
struct linkNode *pCur=&pNode->pHeader;
struct linkNode *pre=NULL;
struct linkNode *pNext=NULL;
while(pCur->next){
pre=pCur->next;
pCur->next=pre->next;
pre->next=pNext;
pNext=pre;
}
pCur->next=pre;
}
void link_destory(link list){
if(!list)
return;
free(list);
list=NULL;
}
struct person{
void *node;
int id;
char name[30];
int age;
};
void myPrint(void *value){
struct person *p=(struct person *)value;
printf("id:%d name:%s age:%d \n",p->id,p->name,p->age);
}
void test(){
struct person p1={NULL,1,"Andy",21};
struct person p2={NULL,2,"Tom",21};
struct person p3={NULL,3,"Sam",21};
struct person p4={NULL,4,"Tim",21};
struct person p5={NULL,5,"John",21};
link plink=init_link();
insert_value(plink,0,&p1);
link_push(plink,&p2);
link_push(plink,&p3);
link_push(plink,&p4);
link_push(plink,&p5);
struct person *p=(struct person *)link_shift(plink);
printf("shift value|| id:%d name:%s age:%d \n",p->id,p->name,p->age);
p=(struct person *)link_pop(plink);
printf("shift value|| id:%d name:%s age:%d \n",p->id,p->name,p->age);
p=(struct person *)link_splice(plink,2);
printf("shift value|| id:%d name:%s age:%d \n",p->id,p->name,p->age);
link_foreach(plink,myPrint);
printf("length: %d\n",link_length(plink));
insert_value(plink,0,&p1);
insert_value(plink,3,&p4);
insert_value(plink,4,&p5);
link_reverse(plink);
link_foreach(plink,myPrint);
printf("length: %d\n",link_length(plink));
link_destory(plink);
}
int main(){
test();
system("pause");
return EXIT_SUCCESS;
}
执行结果
shift value|| id:1 name:Andy age:21
shift value|| id:5 name:John age:21
shift value|| id:4 name:Tim age:21
id:2 name:Tom age:21
id:3 name:Sam age:21
length: 2
id:5 name:John age:21
id:4 name:Tim age:21
id:3 name:Sam age:21
id:2 name:Tom age:21
id:1 name:Andy age:21
length: 5
请按任意键继续. . .