zoukankan      html  css  js  c++  java
  • 创建单链表

     1 // practice32.cpp : Defines the entry point for the console application.
     2 //
     3 #include "StdAfx.h"
     4 #include<malloc.h> // malloc()等
     5 #include<stdlib.h> // atoi()
     6 #include <stdio.h>
     7 
     8 typedef int ElemType;
     9 
    10 // 线性表的单链表存储结构
    11 struct LNode
    12 {
    13  ElemType data;
    14  LNode *next;
    15 };
    16 typedef LNode *LinkList; // 另一种定义LinkList的方法
    17 
    18 // 操作结果:构造一个空的线性表L
    19 int  InitList(LinkList &L)
    20 {
    21  L=(LinkList)malloc(sizeof(LNode)); // 产生头结点,并使L指向此头结点
    22  if(!L) // 存储分配失败
    23   return 0;
    24  L->next=NULL; // 指针域为空
    25  return 1;
    26 }
    27 // 正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表
    28 void CreateList2(LinkList &L,int n)
    29 {
    30  int i;
    31  LinkList p,q;
    32  L=(LinkList)malloc(sizeof(LNode)); // 生成头结点
    33  L->next=NULL;
    34  q=L;
    35  printf("请输入%d个数据
    ",n);
    36  for(i=1;i<=n;i++)
    37  {
    38   p=(LinkList)malloc(sizeof(LNode));
    39   scanf("%d",&p->data);
    40   q->next=p;
    41   q=q->next;
    42  }
    43  p->next=NULL;
    44 }
    45 
    46 int ListTraverse(LinkList L)
    47 {
    48  LinkList p;
    49  p = L->next;
    50  while(p)
    51  {
    52   printf("%d ",p->data);
    53   p = p->next;
    54  }
    55  printf("
    ");
    56  return 1;
    57 }
    58 
    59 int main(int argc, char* argv[])
    60 {
    61    int n=5;
    62    LinkList La;
    63 
    64    CreateList2(La,n); // 正位序输入n个元素的值
    65    printf("La="); // 输出链表La的内容
    66    ListTraverse(La);
    67 
    68    return 0;
    69 }
  • 相关阅读:
    jmeter参数化文件路径问题
    kafka在linux下安装
    性能测试案例:Oracle重复的SQL
    Elastic:菜鸟上手指南
    python推导式特殊用法
    python动态参数
    python 循环控制
    python 代码执行顺序
    Python eval() 函数
    if __name__ == '__main__': 详解
  • 原文地址:https://www.cnblogs.com/Zblogs/p/3360511.html
Copyright © 2011-2022 走看看