zoukankan      html  css  js  c++  java
  • 后插法创建带头节点的单链表

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 typedef struct{
     4     int info;
     5     struct node *next;
     6 }node;
     7 node* creat(){
     8     int n,i;
     9     node *head,*q;
    10     head=(node*)malloc(sizeof(node));
    11     printf("input creat node's num
     ");
    12     scanf("%d",&n);
    13     q=head;
    14     for(i=0;i<n;i++){
    15         node *p;
    16         p=(node*)malloc(sizeof(node));
    17         printf("input node value
    ");
    18         scanf("%d",&p->info);
    19         q->next=p;
    20         p->next=NULL;
    21         q=p;    
    22     }
    23     return head;
    24     
    25 }
    26 print(node* head)
    27 {
    28     node* q;
    29     q=head->next;
    30     while(q)
    31     {
    32         if(q->next==NULL){printf("%d",q->info);}
    33         else{printf("%d->",q->info);}
    34         q=q->next; 
    35     }
    36 }
    37 
    38 int main()
    39 {
    40     node *head;
    41     head=creat();
    42     print(head);
    43     return 0;
    44 } 

    思路:
    创建头结点head
    创建一个结构体指针q用来把整个链表穿起来;这是重点;q=head;
    创建节点p:
    q->next=p;
    p->next=NULL;
    q=p;

  • 相关阅读:
    centos安装python3
    MongoDB 索引
    dockerfile
    docker端口映射与容器互联
    操作docker容器
    docker数据管理
    ubuntu安装docker
    MVC框架
    图像标签
    HTML的标签
  • 原文地址:https://www.cnblogs.com/Thegonedays/p/9994118.html
Copyright © 2011-2022 走看看