初学链表之链表的创建与遍历

  链表是一个最简单的数据结构,说什么也要掌握,以前看到链表中用到大量的指针就头疼了,但学起来,发现不是那么复杂,只要知道了那个指针是干什么用的,怎么用,就变得不是那么难了...这里不多说,说多了不会写链表等于没说,看代码的注释...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct xsxx
{
    char name[20];
    int age;
    char num[20];
    char clas[20];
    struct xsxx *next;                                  //创建存储下一个链表指针数据的指针
};                                                      //创建结构体

struct xsxx *head = NULL;                               //创建头指针
struct xsxx *ls = NULL;                                 //创建临时指针,用来开辟新链表
struct xsxx *now = NULL;                                //记录当前访问或修改的节点的位置

int input(void)                                         //创建家里链表的函数
{
    int a[20];
    scanf("%s",a);
    if((ls=(struct xsxx *)malloc(sizeof(struct xsxx)*1))==NULL)
    {
        printf("system error\n");
        return 0;
    }                                                    //若系统不给此链节分配内存,就跳出
    else
    {
        if(head==NULL)                                  //检查是否创建了头节点
        {
            head = ls;
            strcpy(ls->name,a);
            scanf("%d%s%s",&ls->age,ls->num,ls->clas);
            now = head;
            now->next = NULL;
        }
        else
        {
            now->next = ls;                            //开辟新的链表
            now = now->next;                           //now记录当前访问的位置
            now->next = NULL;                          /*本链结中会记录下一个链结的地址,但下一个链接未创建,所以
                                                       赋值为空指针,这也是遍历链表时结束的标志*/
            strcpy(ls->name,a);
            scanf("%d%s%s",&ls->age,ls->num,ls->clas);
        }
    }
    return 0;
}

int print(void)                                         //定义打印链表函数
{
    if(head == NULL)                                   //如果头指针为空,打印没东西
    {
        printf("nothing\n");
        return 0;
    }
    else
    {
        ls = head;
        while(ls != NULL)                                //用while循环遍历整个链表,上面提到的结束条件也在这里体现
        {
            printf("%s\n%d\n%s\n%s\n",ls->name,ls->age,ls->num,ls->clas);
            ls = ls->next;                               //ls指针每次读取正在访问的链节中的下一个链节的地址
        }
    }
    return 0;
}

int main()                                              //主函数,不用过多解释
{
    int n = 0;
    scanf("%d",&n);
    while(n--)
    {
        input();
    }
    print();
    return 0;
}

这是我感觉我的链表学的不错的时候写的:

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;//数据域
    struct node *next;//指针域
};//链表的节点

int main()
{
    struct node *head = (struct node *)malloc(sizeof(struct node));//给头节点开内存
    head->next = NULL;//头结点的下一个节点目前不存在,设为空
    int n;//输入数的个数
    struct node *p = head;//用游动指针指向头结点
    scanf("%d",&n);
    for(int i = 0;i < n;i++)//循环输入n个数
    {
        struct node *q = (struct node *)malloc(sizeof(struct node));//结点开内存
        scanf("%d",&q->num);
        p->next = q;
        q->next = NULL;
        p = q;//游动指针移动
    }
    p = head->next;
    while(p != NULL)//遍历
    {
        printf("%d",p->num);
        if(p->next != NULL)
            printf(" ");
            p = p->next;
    }
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值