通过子函数调用创建链表的三种方式

// 通过子函数调用创建链表的三种方式

#include "stdafx.h"
#include <iostream>
using namespace std;

struct node{
    int value;
    node* next;
};

node* createList();
void createList1(node* head);
void createList2(node*& head);
void printList(node* head);
int _tmain(int argc, _TCHAR* argv[])
{
    // 创建链表方式1,在子函数中创建链表,返回头指针
    //node* head = createList();
    //printList(head);

    // 创建链表方式2,在主函数中创建头结点
    // 不需要返回值,不需要引用传递
    //node* head1 = new node();
    //head1->value = -1;
    //head1->next = NULL;

    //createList1(head1);
    //printList(head1);

    // 创建链表方式3,在主函数中定义头结点,仅仅是定义
    // 不需要返回值,但要使用引用传递
    node* head2;
    createList2(head2);
    printList(head2);

    system("pause");
    return 0;
}

// 方式1:在子函数中创建链表,返回头指针
node* createList(){
    node* head = new node();
    head->value = -1;
    head->next = NULL;

    for(int i=0;i<100;i++){
        node* newnode = new node();
        newnode->value = i;

        // 头插
        newnode->next = head->next;
        head->next = newnode;
    }
    return head;
}

// 方式2:在主函数中创建头结点,头指针指向已经明确,不需要子函数再去修改头指针的指向,所以不需要head的引用传递
void createList1(node* head){

    for(int i=0;i<100;i++){
        node* newnode = new node();
        newnode->value = i;

        // 头插
        newnode->next = head->next;
        head->next = newnode;
    }
}

// 方式3::仅仅在主函数中定义了头指针,但并未开辟内存,需要子函数改掉头指针的指向,所以需要head的引用传递,但不需要返回值
void createList2(node*& head){
    head = new node();
    for(int i=0;i<100;i++){
        node* newnode = new node();
        newnode->value = i;

        // 头插
        newnode->next = head->next;
        head->next = newnode;
    }
}

// 打印链表
void printList(node* head){
    node* ptr = head->next;
    while(ptr!=NULL){
        cout<<ptr->value<<" ";
        ptr = ptr->next;
    }
}

创建链表的三种方式:

1.在子函数中定义头结点并为头结点开辟空间,将新的数据结点链接在头结点之后,最后返回头结点指针。

2.在主函数中定义头结点并为头结点开辟空间,此时头结点已经创建好,只需要将头结点指针传给子函数即可,子函数调用结束后长长的链表自然就被创建好了。

3.只在主函数中定义头结点,但并不开辟空间,此时头结点指针还没有被赋予初值,将头指针的的引用传给子函数,子函数为头结点开辟空间之后,自然就把主函数中定义的头指针给改了,让头指针指向子函数开辟的空间。

以上三种方法我认为第一种比较好,将整个创建的链表的过程封装在子函数中,最后只返回一个创建好的链表的头指针。在创建二叉树,图的例子中跟这个是完全相仿的。

比较容易让人迷惑的是第三种方式,主函数定义头指针后并未开辟空间,此时head的指向还不明确,但子函数为head开辟过空间之后head的指向就被确定下来,所以需要在子函数中改掉头指针的指向,指针本质就是一个表示内存地址的长整数,因为要在子函数中改掉这个长整数,这就需要用到引用传递。

可以将保存链表到文件的操作封装成一个子函数,以便在程序中重复利用。下面是一个示例代码,演示如何利用子函数链表保存到文件中: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { int id; char name[20]; struct Node* next; } Node; // 将链表保存到文件中 int saveListToFile(Node* head, const char* fileName) { // 打开文件 FILE* fp; fp = fopen(fileName, "w"); if (fp == NULL) { printf("文件打开失败!\n"); return 0; } // 遍历链表,将每个节点的数据写入文件 Node* p = head; while (p != NULL) { fprintf(fp, "%d %s\n", p->id, p->name); p = p->next; } // 关闭文件 fclose(fp); return 1; } int main() { // 创建链表 Node* head = NULL; Node* tail = NULL; for (int i = 1; i <= 5; i++) { Node* node = (Node*)malloc(sizeof(Node)); node->id = i; sprintf(node->name, "name%d", i); node->next = NULL; if (head == NULL) { head = node; } else { tail->next = node; } tail = node; } // 保存链表到文件中 int result = saveListToFile(head, "data.txt"); if (result) { printf("链表保存成功!\n"); } else { printf("链表保存失败!\n"); } // 释放链表占用的内存 Node* p = head; while (p != NULL) { Node* next = p->next; free(p); p = next; } return 0; } ``` 在上面的示例代码中,我们将保存链表到文件的操作封装成了一个名为saveListToFile的子函数。该函数接受两个参数,分别为链表指针和文件名。在函数内部,我们使用fopen函数打开文件,然后遍历链表,将每个节点的数据输出到文件中。最后,我们使用fclose函数关闭文件,并返回1表示操作成功。在主函数中,我们先创建了一个包含5个节点的链表,然后调用saveListToFile函数链表保存到名为"data.txt"的文件中。最后,我们释放链表所占用的内存。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值