链表

#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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值