欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2018年南京工业大学828数据结构第十六题

程序员文章站 2022-05-23 10:06:30
...

2018年南京工业大学828数据结构第十六题

#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;
}
相关标签: 南工大