zoukankan      html  css  js  c++  java
  • 数据结构之链表

    一、 单链表的创建、插入、遍历

      1 //
      2 //  main.c
      3 //  1.1-链表创建
      4 //
      5 //  Created by LinKun on 2016/10/14.
      6 //  Copyright © 2016年 LinKun. All rights reserved.
      7 //
      8 
      9 #include <stdio.h>
     10 #include <stddef.h>
     11 #include <stdlib.h>
     12 #include <stdbool.h>
     13 
     14 #define ElemType char
     15 
     16 #pragma mark - 链表结点结构
     17 typedef struct Node {
     18     ElemType data;
     19    struct Node *next;
     20 }Node;
     21 
     22 #pragma mark - 1. 创建单链表
     23 Node *CreateList(Node *head) {
     24     // 如果头结点为空,则创建头结点
     25     if (head == NULL) {
     26         head = (Node *)malloc(sizeof(Node));
     27         head->next = NULL;
     28     }
     29     
     30     // 指向当前结点
     31     Node *prev = head, *temp;
     32     ElemType ch;
     33     
     34     while (true) {
     35         printf("input node: ");
     36         
     37         scanf("%c", &ch);
     38         
     39         if (ch == '#')
     40             break;
     41         
     42         // 创建新链表结点
     43         temp = (Node *)malloc(sizeof(Node));
     44         temp->data = ch;
     45         temp->next = NULL;
     46         
     47         // 在表尾结点后面插入新结点
     48         prev->next = temp;
     49         prev = temp;
     50         
     51         // 过滤回车键
     52         getchar();
     53     }
     54     
     55     return head;
     56 }
     57 
     58 #pragma mark - 2. 遍历单链表
     59 void TranversalList(Node *head) {
     60     // 没有头结点则直接返回
     61     if (head == NULL) {
     62         printf("遍历失败,链表表头为空!");
     63         return;
     64     }
     65     
     66     // 指向当前结点的下一个结点
     67     Node *curr = head->next;
     68     
     69     // 当前结点有下一结点时,输出下一结点值
     70     while (curr) {
     71             printf("结点%c->", curr->data);
     72             curr = curr->next;
     73 
     74     }
     75     printf("NULL
    ");
     76 }
     77 
     78 #pragma mark 3. 插入结点
     79 Node *InsertNode(Node *head, ElemType element) {
     80     if (head == NULL) {
     81         printf("插入失败,链表表头为空!");
     82         return head;
     83     }
     84     
     85     // 指向当前结点
     86     Node *prev = head;
     87     // 指向当前结点的下一个结点
     88     Node *curr = head->next;
     89     
     90     // 递推到当前结点是链表尾结点
     91     while (curr) {
     92         prev = curr;
     93         curr = curr->next;
     94     }
     95     
     96     // 创建新结点
     97     Node *temp = (Node *)malloc(sizeof(Node));
     98     temp->data = element;
     99     temp->next = NULL;
    100     
    101     // 插入到链表尾结点之后
    102     prev->next = temp;
    103     
    104     return head;
    105 }
    106 
    107 
    108 
    109 #pragma mark - 测试
    110 int main(int argc, const char * argv[]) {
    111     
    112     Node *head;
    113     
    114     // 创建链表并遍历
    115     head = CreateList(head);
    116     printf("遍历结果:
    ");
    117     TranversalList(head);
    118     
    119     // 插入链表元素并遍历
    120     head = InsertNode(head, 'a');
    121     printf("遍历结果:
    ");
    122     TranversalList(head);
    123     
    124     return 0;
    125 }
    单链表
  • 相关阅读:
    如何给LG gram写一个Linux下的驱动?
    题解 CF492C Vanya and Exams
    题解 CF546B Soldier and Badges
    题解 P1283 【平板涂色】
    题解 P1717 【钓鱼】
    题解 UVA663 Sorting Slides(烦人的幻灯片)
    题解 P2835 【刻录光盘】
    题解 P5367 【【模板】康托展开】
    题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
    题解 P2272 【[ZJOI2007]最大半连通子图】
  • 原文地址:https://www.cnblogs.com/gzhu-lkun/p/6012996.html
Copyright © 2011-2022 走看看