2018年南京工业大学828数据结构第十六题
程序员文章站
2022-05-23 10:06:30
...
#include <iostream>
using namespace std;
typedef struct node
{
int data;
node *next;
} * List;
void copyList(List l1, List &l2)
{
List p = l1;
List r = l2;
while(p){
List t = (List)malloc(sizeof(node));
t->data = p->data;
t->next = nullptr;
p = p->next;
r->next = t;
r = r->next;
}
l2 = l2->next;
}
int main()
{
List l1_son = (List)malloc(sizeof(node));
l1_son->data = 3;
l1_son->next = nullptr;
List l1 = (List)malloc(sizeof(node));
l1->data = 2;
l1->next = l1_son;
List l2 = (List)malloc(sizeof(node));
copyList(l1,l2);
List p = l2;
while(p != nullptr){
cout<<p->data<<endl;
p = p->next;
}
return 0;
}