单向链表 例子
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *p_next;
};
struct link
{
struct node head;
struct node tail;
struct link *p_link;
};
void link_init (struct link *link_p)
{
link_p->head.p_next=&link_p->tail;
link_p->tail.p_next=NULL;
link_p->head.data=0;
link_p->tail.data=0;
}
void link_dell(struct link *link_p)
{
while (link_p->head.p_next!=&link_p->tail)
{
struct node * first=&link_p->head;
struct node * mid=first->p_next;
struct node * last=mid->p_next;
first->p_next=last;
free(mid);
mid=NULL;
}
}
void printf_link(struct link *link_p)
{
struct node *index;
struct node * first ,*mid ,*last ;
for (index = &link_p->head; index!=&link_p->tail; index=index->p_next)
{
first=index;
mid=first->p_next;
last=mid->p_next;
if(mid!=&link_p->tail)
printf("data %d \n",mid->data);
}
}
int link_add_head(struct link *link_p,int data)
{
struct node * first ,*mid ,*last ;
struct node * new = calloc(1,sizeof(struct node));
new->data=data;
first=&link_p->head;
mid=first->p_next;
last=mid->p_next;
first->p_next=new;
new->p_next=mid;
return 0;
}
int main(int argc,char ** argv)
{
struct link link_node;
link_init(&link_node);
link_add_head(&link_node,10);
link_add_head(&link_node,11);
link_add_head(&link_node,12);
printf_link(&link_node);
link_dell(&link_node);
return 0;
}
双向链表例子
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * p_next;
struct node * p_preo;
};
typedef struct link
{
struct node head;
struct node tail;
struct link * p_link;
}link;
void link_init(link * link_p)
{
link_p->head.p_next=&link_p->tail;
link_p->head.p_preo=NULL;
link_p->tail.p_preo=&link_p->head;
link_p->tail.p_next=NULL;
}
void link_clean(link * link_p)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
nonius=&link_p->head;
int count =0;
while(nonius->p_next!=NULL)
{
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(middle!=&link_p->tail)
{
free(middle);
middle=NULL;
front->p_next=last;
last->p_preo=front;
count++;
nonius=nonius->p_next;
if(last->p_next!=NULL)
{
nonius=&link_p->head;
}
}
}
}
int link_line_add(link * link_p ,int number)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
struct node *new = (struct node *)malloc (sizeof(struct node));
if(new==NULL)
{
printf("malloc data failed ! \n");
return -1;
}
new->p_next=NULL;
new->p_preo=NULL;
new->data=number;
nonius=&link_p->head;
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(front->p_next==&link_p->tail)
{
front->p_next=new;
new->p_preo=front;
new->p_next=middle;
middle->p_preo=new;
}else
{
middle->p_next=new;
new->p_preo=middle;
new->p_next=last;
last->p_preo=new;
}
return 0;
}
int link_add_max(link * link_p ,int number)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
struct node *new = (struct node *)malloc (sizeof(struct node));
if(new==NULL)
{
printf("malloc data failed ! \n");
return -1;
}
new->p_next=NULL;
new->p_preo=NULL;
new->data=number;
nonius=&link_p->head;
while(nonius->p_next!=&link_p->tail)
{
if(nonius->p_next ==NULL)
{
printf("link data error \n");
return -1;
}
nonius=nonius->p_next;
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(middle->data<number)
{
middle->p_next=new;
new->p_preo=middle;
new->p_next=last;
last->p_preo=new;
break;
}
}
return 0;
}
int link_add_min(link * link_p ,int number)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
struct node *new = (struct node *)malloc (sizeof(struct node));
if(new==NULL)
{
printf("malloc data failed ! \n");
return -1;
}
new->p_next=NULL;
new->p_preo=NULL;
new->data=number;
nonius=&link_p->head;
while(nonius->p_next!=&link_p->tail)
{
if(nonius->p_next ==NULL)
{
printf("link data error \n");
return -1;
}
nonius=nonius->p_next;
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(middle->data>number)
{
front->p_next=new;
new->p_next=middle;
new->p_preo=front;
middle->p_preo=new;
break;
}
}
}
int link_del_num(link* link_p,int number)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ;
struct node * nonius =&link_p->head;
while(nonius->p_next!=&link_p->tail)
{
nonius=nonius->p_next;
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(middle->data==number)
{
free(middle);
middle=NULL;
front->p_next=last;
last->p_preo=front;
printf("del %d \n",number);
break;
}
}
return 0;
}
struct node * get_number(link * link_p,int number)
{
struct node * front=NULL,*middle=NULL ,*last=NULL ,*ret=NULL ;
struct node * nonius =&link_p->head;
while(nonius->p_next!=&link_p->tail)
{
nonius=nonius->p_next;
front=nonius;
middle=front->p_next;
last=middle->p_next;
if(middle->data==number)
{
ret=middle;
break;
}
}
return ret;
}
int link_print(link * link_p)
{
const struct node * front=NULL,*middle=NULL ,*last=NULL ;
struct node * nonius =&link_p->head;
int tatol=0;
printf("\n ");
while(nonius->p_next!=&link_p->tail)
{
tatol++;
nonius=nonius->p_next;
printf(" %d ",nonius->data);
}
printf(" \n link tatol %d \n ",tatol);
return 0;
}
int main(int argc,char ** argv)
{
link link_m;
link_init(&link_m);
link_line_add(&link_m,10);
link_line_add(&link_m,11);
link_line_add(&link_m,12);
link_line_add(&link_m,13);
link_line_add(&link_m,14);
link_line_add(&link_m,15);
link_print(&link_m);
link_clean(&link_m);
printf("{{{{{{{\n");
link_print(&link_m);
printf("}}}}}}}}\n");
link_line_add(&link_m,10);
link_line_add(&link_m,11);
link_line_add(&link_m,8);
link_line_add(&link_m,13);
link_line_add(&link_m,14);
link_add_max(&link_m,9);
link_add_min(&link_m,2);
link_print(&link_m);
link_del_num(&link_m,2);
link_print(&link_m);
printf("get number = %d \n",(int)get_number(&link_m,9)->data);
return 0;
}