zoukankan      html  css  js  c++  java
  • linux c语言链表的简单应用之创建链表

    /* ************************************************************************
    * Filename: link.c
    * Description:
    * Version: 1.0
    * Created: 2011骞?4鏈?9鏃?17鏃?2鍒?3绉?
    * Revision: none
    * Compiler: gcc
    * Author: wen hao (WH), hnrain1004@gmail.com
    * Company: sunplusapp
    * ***********************************************************************
    */


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

    #define LEN sizeof(struct stu)
    //声明结构体
    typedef struct stu
    {
    int num;
    char name[10];
    struct stu *next;
    }TYPE;

    //链表创建函数,返回类型为结构体指针类型
    TYPE * create(int n)
    {
    TYPE
    *head,*prev,*curre;
    int i;

    for(i = 0; i < n; i++)
    {
    curre
    = (TYPE *)malloc(LEN);//申请空间
    printf("input number and name:\n");
    scanf(
    "%d %s",&curre->num,curre->name);//等待用户输入数据

    if(i == 0)
    prev
    =head=curre;
    else
    prev
    ->next=curre;
    prev
    =curre;
    }
    curre
    ->next =NULL;
    return head;
    }

    //打印输出函数,形参为链表头指针
    void print(TYPE *head)
    {
    printf(
    "\nthe link message is :\n");
    printf(
    "number \t\tname \n");
    while(head!=NULL)//如果没有指向链表尾就一直打印
    {
    printf(
    "%d\t\t%s\n",head->num,head->name);
    head
    =head->next;
    }
    printf(
    "\n");
    }

    int main(void)
    {
    TYPE
    *head;//定义结构体变量
    head = create(3);//创建链表
    print(head);//打印链表

    return 0;
    }
  • 相关阅读:
    oracle函数大全(转载)
    oracle的基本数据类型(转载)
    oracle 数据类型详解---日期型(转载)
    队列的链式存储结构
    在Lua里写unity游戏笔记
    NGUI-快捷键
    NGUI在线电子书---NGUI for Unity
    NGUI学习笔记-UISprite
    unity延时方法Invoke和InvokeRepeating
    NGUI-制作位图字体以及图文混排
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2032708.html
Copyright © 2011-2022 走看看