#include
#include
struct node
{
int value;
struct node *next,*prev;
};
static int init_value[5]={1,2,3,4,5};
struct node* createDoublyLinked(int *init_value,int postion,int length)
{
if(length == 0) return NULL;
struct node *init_node = malloc(sizeof(struct node));
init_node->next = NULL;
init_node->prev = NULL;
if(postion == length -1)
{
init_node->value = init_value[postion];
return init_node;
}
struct node* final_node= createDoublyLinked(init_value,postion+1,length);
init_node->next = final_node;
init_node->value = init_value[postion];
final_node->prev = init_node;
return init_node;
}
struct node *createLinkedList(int *init_value,int postion,int length)
{
if(length == 0) return NULL;
struct node *init_node = malloc(sizeof(struct node));
init_node->next = NULL;
if(postion == length-1)
{
init_node->value=init_value[postion];
return init_node;
}
init_node->next = createLinkedList(init_value,postion+1,length);
init_node->value = init_value[postion];
return init_node;
}
int main(void)
{
struct node* result = createLinkedList(init_value,0,sizeof(init_value)/sizeof(init_value[0]));
while(result != NULL)
{
printf("%d\t",result->value);
result = result->next;
}
struct node* final = createDoublyLinked(init_value,0,sizeof(init_value)/sizeof(init_value[0]));
struct node* last_node = NULL;
while(final != NULL)
{
last_node = final;
printf("%d ",final->value);
final = final->next;
}
while(last_node != NULL)
{
printf("%d ",last_node->value);
last_node = last_node->prev;
}
return 0;
}
阅读(981) | 评论(0) | 转发(0) |