zoukankan      html  css  js  c++  java
  • 建立链表并逆序打印该链表

    下面程序有几个地方注意一下,见注释.

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 /*以后写结构体,都使用该格式.即typedef标注.上下ListNode要相同.*/
     5 typedef struct ListNode
     6 {
     7     int key;
     8     struct ListNode *next;
     9 } ListNode;
    10 
    11 static ListNode *create_list();
    12 static void print_list(ListNode *);
    13 static void reverse_print(ListNode *);
    14 
    15 int main(int argc, char *argv[])
    16 {
    17     ListNode *head = NULL;
        /*不能直接使用create_list(head),想想为什么?
        **函数调用时,参数的传递相当于赋值.不能将空指针赋值给其他指针.*/
    18 head = create_list(); 19 print_list(head); 20 21 reverse_print(head); 22 printf(" "); 23 return 0; 24 } 25 26 static ListNode *create_list() 27 { 28 int c; 29 ListNode *p; 30 31 head = (ListNode *)malloc(sizeof(ListNode)); 32 head->next = NULL; 33 p = head; 34 35 scanf("%d", &c); 36 while (c != -1) 37 { 38 p->next = (ListNode *)malloc(sizeof(ListNode)); 39 p->next->key = c; 40 p->next->next = NULL; 41 p = p->next; 42 scanf("%d", &c); 43 } 44 return head; 45 } 46 47 48 static void print_list(ListNode *head) 49 { 50 ListNode *p; 51 p = head->next; 52 53 while (p != NULL) 54 { 55 printf("%d ", p->key); 56 p = p->next; 57 } 58 printf(" "); 59 } 60 /*该递归调用好好品品.*/ 61 static void reverse_print(ListNode *head) 62 { 63 ListNode *p; 64 65 p = head->next; 66 if (p == NULL) 67 { 68 return; 69 } 70 else 71 { 72 reverse_print(p); 73 } 74 75 printf("%d ", p->key); 76 }
  • 相关阅读:
    Java-----Excel转HTML
    UI_拖动View
    查找——图文翔解Treap(树堆)
    android 三级菜单 BaseExpandableListAdapter
    New Relic——手机应用app开发达人的福利立即就到啦!
    安卓 下载多线程带进度条
    大数据
    Android学习路线(六)为Android应用加入ActionBar
    JavaScript日期对象使用总结
    经典二叉树
  • 原文地址:https://www.cnblogs.com/yyxayz/p/4027007.html
Copyright © 2011-2022 走看看