zoukankan      html  css  js  c++  java
  • 建立链表(尾接法)

    //对指针还是很不熟悉 得强化
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct Node
    {
        int data;
        struct Node *next;
    }Node;
    Node* CreateLinkList(int q)
    {
        int flag=0,i,d,jishu=0;
        Node *root,*e,*tree;
        for(i=1;i<=q;i++)
        {
            scanf("%d",&d);
            if(!flag)
            {
                root=(Node*)malloc(sizeof(Node));
                root->data=d;
                root->next=NULL;
                tree=root;     //建立了一个头节点
                flag=1;
            }
            else
            {
                e=(Node*)malloc(sizeof(Node));
                e->data=d;
                e->next=NULL;
                tree->next=e;   //把e节点连接到tree后面
                tree=e;  //数的最后面为新加的节点
            }
        }
        return root;
    }
    void PrintLinkList(Node *head)
    {
        int flag = 0;
        Node *p = head, *q;
        while(p)
        {
            if(flag)
                printf(" ");
            flag = 1;
            printf("%d", p->data);
            q = p;
            p = p->next;
            free(q);
        }
    }
    int main()
    {
        int n;
        scanf("%d", &n);
        Node *h = CreateLinkList(n);
        PrintLinkList(h);
        return 0;
    }
  • 相关阅读:
    jieba库与词云的使用——以孙子兵法为例
    Python 计算π及进度条显示
    风车
    UDP协议编程
    matplotlib的基本使用、绘制雷达图及绘制数学函数
    numpy库的学习笔记及自定义手绘风
    PIL库的学习总结及生成GIF
    预测球队比赛结果及利用pyinstaller打包文件
    汉诺塔的实现
    有进度条的圆周率计算
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/10473201.html
Copyright © 2011-2022 走看看