#include "stdio.h"
#include "stdlib.h"
struct __chain
{
struct __chain* next_ptr;
struct __chain* pro_ptr;
int chain_number;
struct
{
unsigned char data_a;
unsigned char data_b;
};
};
typedef struct __chain _chain;
_chain head={0,0,0,0,0};//chain_head;
_chain *chain_head=&head;
void chain_init(_chain* chain)
{
chain->next_ptr=0;
chain->data_a=0;
chain->data_b=0;
}
int chain_add(_chain *head)
{
_chain *ch_ptr=head;
while(ch_ptr->next_ptr!=0)
{
ch_ptr=ch_ptr->next_ptr;
}
ch_ptr->next_ptr=malloc(sizeof (_chain));
if(ch_ptr->next_ptr==NULL)return 1;
ch_ptr->next_ptr->pro_ptr=ch_ptr;
ch_ptr->next_ptr->chain_number=ch_ptr->chain_number+1;
ch_ptr=ch_ptr->next_ptr;
chain_init(ch_ptr);
printf("add chain finished !!\n");
return 0;
}
_chain* chain_seek(_chain* head,int num)
{
_chain *ch_ptr=head;
if(num<0)return NULL;
if(ch_ptr->chain_number<=num)
while(ch_ptr->chain_number!=num)
{
ch_ptr=ch_ptr->next_ptr;
}
else
while(ch_ptr->chain_number!=num)
{
ch_ptr=ch_ptr->pro_ptr;
}
return ch_ptr;
}
void chain_show(_chain *ch)
{
printf("chain %d , address %d , data a and b is %d and %d\n",ch->chain_number,ch,ch->data_a,ch->data_b);
}
void chain_total(void)
{
unsigned int total=1;
_chain * ptr=chain_head;
while(ptr->next_ptr!=0)
{
ptr=ptr->next_ptr;
total++;
}
printf("chain total is %d\n",total);
}
void main()
{
_chain *ch;
chain_add(&head);
chain_add(&head);
chain_add(&head);
chain_add(&head);
chain_add(&head);
chain_add(&head);
ch=chain_seek(&head,0);
ch->data_a=3;
chain_show(ch);
ch=chain_seek(&head,6);
chain_show(ch);
// ch=chain_seek((_chain *)ch->next_ptr,1);
ch->data_a=2;
chain_total();
}
链表
最新推荐文章于 2024-06-14 09:15:00 发布