用c语言实现此函数,如下 完成代码
#include <stdio.h>
#include <stdlib.h>
#define ElemDate int
typedef struct SLIST
{
ElemDate data;
struct SLIST *next;
} SLIST_S, *LINK;
//初始化链表
int initLink(LINK *link)
{
*link = (LINK)malloc(sizeof(SLIST_S));
if (*link == NULL)
return -1;
(*link)->next = NULL;
return 0;
}
//添加元素
SLIST_S *addelem(SLIST_S *list)
{
int ch,n, i = 0;
SLIST_S *listnode = list;
printf("input number:");
scanf("%d",&n);//确定输入的元素的个数
while (i < n)
{
SLIST_S *p = (LINK)malloc(sizeof(SLIST_S));
printf("input:");
scanf("%d", &ch);
p->data = ch;
listnode->next = p;
listnode = p;
i++;
}
return list;
}
//打印元素
int show(SLIST_S *list)
{
SLIST_S *listnode = list->next;
if (listnode == NULL)
{
printf("erro\n");
return 0;
}
printf("ele:");
while (listnode)
{
printf("%2d", listnode->data);
listnode = listnode->next;
}
printf("\n");
}
//将链表A的结点给B链表,B链表通过头节点插入法从而实现A链表的结点逆序
int list_revert(SLIST_S *listA, SLIST_S *listB)
{
SLIST_S *pb = listB;
SLIST_S *pa = listA->next;
SLIST_S *q = NULL;
while (pa)
{
q = pa;
pa = pa->next;
q->next = pb->next;
pb->next = q;
}
return 0;
}
//主函数
int main(int argc, char const *argv[])
{
SLIST_S *listA, *listB;
initLink(&listA);
initLink(&listB);
addelem(listA);
show(listA);
list_revert(listA, listB);
show(listB);
return 0;
}