数据结构之链表

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

  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\n");
 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("遍历结果:\n");
117     TranversalList(head);
118     
119     // 插入链表元素并遍历
120     head = InsertNode(head, 'a');
121     printf("遍历结果:\n");
122     TranversalList(head);
123     
124     return 0;
125 }
单链表

 

转载于:https://www.cnblogs.com/gzhu-lkun/p/6012996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值