链表的结点结构
 ┌───┬───┐
 │data|next│
 └───┴───┘
data域--存放结点值的数据域
next域--存放结点的直接后继的地址(位置)的指针域(链域)

实例:从终端输入5个姓名,单链表输出
typedef struct node{

char * name;

struct node *next;

}Node;  // 定义一个结构体

void myfree(Node * pHead){   //从头指针开始释放

while (pHead != NULL) {

Node *ptemp = pHead->next;

free(pHead->name);

free(pHead);

pHead = ptemp;

}

} //释放申请过的内存

int main(int argc, const char * argv[]) {

Node *phead = NULL;   //定义头指针

Node *ptail = NULL;   //定义尾指针

for (int i = 0; i<5; i++) {

Node *ptemp = (Node*)malloc(1*sizeof(Node));   //申请一个结构体大小的空间

if (ptemp == NULL) {

myfree(phead);

exit(EXIT_FAILURE);

}

printf("请输入姓名:");

char * name=NULL;    //临时存放name

char c;

int total =0;  // 用来计数 当前存到第几个

while (1) {

c=getchar();

if (c=='\n') {

break;

}

if (name == NULL) {   // 如果第一次存放

name = (char*)malloc(1*sizeof(char));

if(name==NULL){

free(ptemp);

myfree(phead);

exit(EXIT_FAILURE);

}

}else{

name = (char*)realloc(name,(total+1)*sizeof(char) );

if (name == NULL) {

free(ptemp);

myfree(phead);

exit(EXIT_FAILURE);

}

}

*(name+total)=c;   // 依次存放

total++;

}

ptemp->name = name;   //指向临时的name

ptemp->next = NULL;

if (phead==NULL) {  //如果头指针是空的 头指针尾指针指向第一个

phead = ptemp;

ptail = ptemp;

}else{

ptail->next = ptemp;  // 衔接之后 尾指针移动

ptail = ptemp;

}

}

Node * ptemp = phead;  //防止头指针跑,找不到后面的,所以定义临时的

while (ptemp!=NULL) {

printf("%s  ",ptemp->name);

ptemp=ptemp->next;

}

myfree(phead);

return 0;

}

单链表(Single Linked List)的更多相关文章

  1. C#单链表

    顺序表是用地址连续的存储单元顺序存储线性表中的各个数据元素, 逻辑上相邻的数据元素在物理位置上也相邻.因此,在顺序表中查找任何一个位置上的数据元素非常方便, 这是顺序存储的优点. 但是, 在对顺序表进 ...

  2. 【数据结构】单链表&&静态链表详解和代码实例

    喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...

  3. 《数据结构》2.3单链表(single linked list)

    //单链表节点的定义 typedef struct node { datatype data; struct node *next; }LNode,*LinkList; //LNode是节点类型,Li ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  7. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  8. 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】

    [114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  9. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

随机推荐

  1. jquery文本框验证字符长度和只能输入数字

    <input type="text" class="chujia" onkeyup="this.value=this.value.replace ...

  2. SQL Server无法打开用户默认数据库,登录失败,用户‘sa’登录失败,错误:4064的解决方法

    用windows验证方式进入Management Studio, 安全性 > 用户名 > 右击sa > 属性 > 把默认数据库选“master”

  3. 数组,集合 转成DataTable 方法

    public static DataTable ToDataTable(IList p_List) { DataTable _DataTable = new DataTable(); if (p_Li ...

  4. 解决ScrollView嵌套ListView和GridView冲突的方法

    本文摘抄自:http://blog.csdn.net/yuhailong626/article/details/20639217 原文地址:http://blog.csdn.net/yuhailong ...

  5. C# 创建execl文件 并且填充数据

    第一步:引用文件 using NPOI.HSSF.UserModel;using System.Data;using CTUClassLibrary;using System.IO;using NPO ...

  6. 【solr基础教程之二】索引

    一.向Solr提交索引的方式 1.使用post.jar进行索引 (1)创建文档xml文件 <add> <doc> <field name="id"&g ...

  7. python运维开发(二十三)---tornado框架

    内容目录: 路由系统 模板引擎 cookie 加密cookie 自定义api 自定义session 自定义form表单验证 异步非阻塞 web聊天室实例 路由系统 路由系统其实就是 url 和 类 的 ...

  8. python selenium初入

    ubuntu python3.4 1.安装selenium, pip 安装 pip install selenium 2.selenium版本2.53.x 试过从官网下载的selenium3,但是fi ...

  9. C语言运算符的注意问题

    //对于自增和自减运算符的运算规律值得研究,小心被坑.1 #include<stdio.h> int main(void){ ,j=,p,q; p=(i++)+(i++)+(i++); q ...

  10. debian gnome 3插件

    1.gnome 配置-安装插件 http://maxubuntu.blogspot.com/2012/09/debian-gnome3.html hunagqf|hunaqf2|hunaqf3 2.快 ...