zoukankan      html  css  js  c++  java
  • 一个简单的动态链表。

    一个简单的动态链表。

     1 #include <stdio.h>
     2 #include <malloc.h>                                        /*包含动态内存分配函数的头文件*/
     3 #define N 10                                               /*N为人数*/ 
     4                         
     5 typedef struct node    {
     6     char name[20];
     7     struct node *link;
     8 }stud;
     9 
    10 stud * creat(n){                                           /*建立单链表的函数,形参n为人数*/
    11     stud *p,*h,*s;                                           /* *h保存表头结点的指针,*p指向当前结点的前一个结点,*s指向当前结点*/
    12     int i;                                                   /*计数器*/
    13     if((h =(stud *)malloc(sizeof(stud))) == NULL){           /*分配空间并检测*/
    14         printf("不能分配内存空间");
    15         return NULL;
    16     }
    17     h->name[0]='\0';                                       /*把表头结点的数据域置空*/
    18     h->link=NULL;                                           /*把表头结点的链域置空*/
    19     p=h;                                                   /*p指向表头结点*/
    20     for(i=0;i<n;i++){
    21         if((s =(stud *)malloc(sizeof(stud))) == NULL){     
    22             printf("不能分配内存空间!");
    23             return NULL;
    24         }
    25         p->link=s;                                            /*把s的地址赋给p所指向的结点的链域,这样就把p和s所指向的结点连接起来了*/
    26         printf("请输入第%d个人的姓名",i+1);
    27         scanf("%s",s->name);                                /*在f当前结点s的数据域中存储姓名*/
    28         s->link=NULL;
    29         p=s;
    30     }
    31     return h;
    32 }
    33 void main(){
    34     int number;
    35     stud *head;                                                /*head是保存单链表的表头结点地址的指针*/
    36     number =N;                                                /*把所新建的单链表表头地址赋给head*/
    37     head =creat(number);
    38     do
    39     {
    40         head = head->link;
    41         printf("名字是:%s \n",head->name);
    42     }
    43     while(head->link !=NULL);
    44 }
  • 相关阅读:
    HDFS命令操作和高可用
    nginx-nginx和反向代理概念
    day01 Hadoop 简单介绍及架构设计
    常用正则表达式
    【JavaWeb笔记】第二章 JDBC
    【JavaSE笔记】第二章 进制数据类型和运算符
    LeetCode-94 Binary Tree Inorder Traversal Solution (with Java)
    LeetCode-1019 Next Greater Node In Linked List Solution (with Java)
    LeetCode-946 Validate Stack Sequences Solution (with Java)
    LeetCode-739 Daily Temperatures Solution (with Java)
  • 原文地址:https://www.cnblogs.com/kakaliush/p/1697094.html
Copyright © 2011-2022 走看看