思路:
1.先存入数据到链表,然后将最后的元素插到链表头,然后删除链表尾元素,再依次将链表尾元素顺序插到链表头
2.新建两个链表,.先存入数据到链表1,再将,链表1的数据从表头拷贝到链表2即可
注意:两种方法均采用头插法
#include<stdio.h>
#include<stdlib.h>
typedef struct _linkode_{
int data;
struct _linkode_ *next;
}Linknode;
Linknode *create_linknode()
{
Linknode *node = NULL;
node = (Linknode *)malloc(sizeof(Linknode));
node -> next = NULL;
return node;
}
Linknode *init_linknode()
{
return create_linknode();
}
int insert_linknode(Linknode *head,int data)
{
Linknode *new = NULL;
new = create_linknode();
new->data = data;
new->next = head ->next;
head->next = new;
return 0;
}
int display_linknode(Linknode *head)
{
int count = 0;
while(head->next != NULL)
{
printf("%d\t",head->next->data);
count ++ ;
head = head -> next;
}
putchar('\n');
return count;
}
int delete_linknode(Linknode *head)
{
Linknode *tmp = head;
while(tmp -> next-> next != NULL)
{
tmp = tmp->next;
}
tmp->next = NULL;
return 0;
}
int reverse_insert(Linknode *head)
{
int i,count = 0;
Linknode *tmp = NULL;
tmp = head;
while(tmp->next != NULL)
{
count ++ ;
tmp = tmp -> next;
}
for(i = 0;i < count ; i++)
{
tmp = head;
while(tmp->next->next != NULL)
{
tmp = tmp -> next;
}
insert_linknode(head,tmp->next->data);
delete_linknode(head);
head = head->next;
}
return 0;
}
int copy_insert(Linknode *head,Linknode *copy ,int count)
{
int i;
i = count;
for(i = 0;i < count ; i++ )
{
insert_linknode(copy,head->next->data);
head = head ->next;
}
return 0;
}
int main(int argc, const char *argv[])
{
Linknode *node,*node_copy;
int i =0;
node = init_linknode();
node_copy = init_linknode();
for(i = 0;i< 10; i++)
insert_linknode(node,i);
display_linknode(node);
copy_insert(node,node_copy,10); //采用方法2
display_linknode(node_copy);
reverse_insert(node); //采用方法一,比较麻烦
display_linknode(node);
return 0;
}