• 链表的遍历(1)


    链表的遍历
    /*------------------------------------*/
    #include
    "stdio.h"
    #include
    "stdlib.h"

    struct llist
    {
    int num;
    char name[10];
    struct llist *next;
    };
    typedef
    struct llist node;
    typedef node
    *llink;



    /*-----------------链表的创建----------------*/

    llink createllist()
    {
    llink head;
    llink ptr,ptr1;
    int i;

    /*创建第一个结点*/

    head
    =(llink)malloc(sizeof(node));
    if(!head)
    return NULL;
    printf(
    "输入六项邮寄数据:\n");
    printf(
    "请输入编号 ==> ");
    scanf(
    "%s",head->num);
    printf(
    "请输入编号(%d)的姓名 ==> ",head->num);
    scanf(
    "%s",head->name);
    head
    ->next=NULL;
    ptr
    =head;
    for( i=1; i<6; i++)
    {
    ptr1
    =(llink)malloc(sizeof(node));
    if(!ptr1)
    return NULL;
    printf(
    "请输入编号 ==>", ptr1->num);
    scanf(
    "%s",ptr1->name);
    ptr
    ->next=NULL;
    ptr
    ->next=ptr1;
    ptr
    =ptr->next;


    }
    return head;

    }

    /*--------------链表的结点遍历----------------*/

    llink findnode(llink head,
    int num)
    {
    llink ptr;
    ptr
    =head;
    while(ptr!=NULL)
    {
    if(ptr->num==num)
    return ptr;
    ptr
    =ptr->next;

    }
    return ptr;

    }

    /*----------------------输出查找邮寄姓名-------------------*/


    int main()
    {
    llink head;
    llink ptr;
    int num;


    head
    =createllist();
    if(!head)
    {
    printf(
    "内存分配失败! \n");
    exit(
    1);
    }
    while(1)
    {
    printf(
    "请输入要寻找的邮寄编号 ==> ");
    scanf(
    "%d",&num);
    if(num!=0)
    {
    ptr
    =findnode(head,num);
    if(!ptr)
    printf(
    "没有找到\n");
    else
    printf(
    " 姓名: %s\n",ptr->name);
    }
    else
    exit(
    1);
    }

    }
  • 相关阅读:
    jQuery attr 与 prop 区别最简单分析
    Js事件处理模型/周期
    canvas实现点击带水纹的按钮
    js作用域问题
    js 函数里的 this
    css3: scrollLeft,scrollWidth,clientWidth,offsetWidth 的区别
    C# 中的Async 和 Await 的用法详解
    1、Task的优势
    探秘C#中的yield关键字
    详解C#中 Thread,Task,Async/Await,IAsyncResult的那些事儿
  • 原文地址:https://www.cnblogs.com/FCWORLD/p/1881171.html
Copyright © 2020-2023  润新知