zoukankan      html  css  js  c++  java
  • 转c之练手篇——"链表"

    // 链表Demo.cpp : 定义控制台应用程序的入口点。
    //

    #include
    "stdafx.h"
    #include
    <malloc.h>
    #include
    <stdlib.h>


    /*
    定义结构体
    */
    struct Node
    {
    int number;//数据域
    struct Node *pNext;//指针域
    };

    //声明函数
    struct Node * NodecreatList(void);
    void PutNodeList(struct Node *);

    int main(void)
    {
    struct Node *pHead;
    pHead
    =NodecreatList();//创建头结点单表链
    PutNodeList(pHead);
    return 0;
    }

    struct Node *NodecreatList()
    {
    int len;
    int val;
    struct Node *pHead;
    pHead
    =(struct Node*)malloc(sizeof(struct Node));
    if (NULL==pHead)
    {
    printf(
    "分配失败!程序终止.....");
    exit(
    -1);
    }

    struct Node * pTail=pHead;
    pTail
    ->pNext=NULL;

    printf(
    "请输入链表长度:\n");
    scanf(
    "%d",&len);
    for (int i=0;i<len;i++)
    {
    printf(
    "请输入第%d个数:",i+1);
    scanf(
    "%d",&val);
    struct Node *pNew=(struct Node *)malloc(sizeof(struct Node));
    if (NULL==pNew)
    {
    printf(
    "分配失败!程序终止.....");
    exit(
    -1);
    }
    pNew
    ->number=val;
    pTail
    ->pNext=pNew;
    pNew
    ->pNext=NULL;
    pTail
    =pNew;
    }
    return pHead;
    }
    void PutNodeList(struct Node *pHead)
    {
    struct Node *p=pHead->pNext;
    while(p!=NULL)
    {
    printf(
    "%d\n",p->number);
    p
    =p->pNext;
    }
    printf(
    "\n");
    return;
    }
  • 相关阅读:
    Oracle 归档模式
    如果在安装32位Oracle客户端组件的情况下64位模式运行, 将出现此问题.
    ORA-00972: 标识符过长
    Oracle SQL%ROWCOUNT
    ASP.NET Core 中间件的几种实现方式
    Python 闭包
    Python 迭代器
    Python 正则表达式提高
    Python正则表达式
    Python 生成器
  • 原文地址:https://www.cnblogs.com/Mr0909/p/1932066.html
Copyright © 2011-2022 走看看