链表的结点结构
 ┌───┬───┐
 │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. CSU 1808 地铁

    题意: ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟 ...

  2. 在本地调试微信项目(C#)

    之前一人负责微信的项目,那时2014年LZ还没毕业..啥都不懂,为此特别感谢@SZW,没有你的框架,我可能都无从下手 当时做项目最麻烦的就是调试,因为很多页面都要使用 网页授权获取用户信息 在电脑上打 ...

  3. PHP学习笔记三十【final】

    <?php //final不能去修饰属性(变量) //如果希望类不希望被继承就可以使用final关键字 final class Person() { public function sayHi( ...

  4. iOS——protoco和delegate (事件代理)

    一:被代理人personOne personOne.h #import <Foundation/Foundation.h> @protocol SomeThing<NSObject& ...

  5. 从C# String类理解Unicode(UTF8/UTF16)

    上一篇博客:从字节理解Unicode(UTF8/UTF16).这次我将从C# code 中再一次阐述上篇博客的内容. C# 代码看UTF8 代码如下: string test = "UTF- ...

  6. HBase配置&启动脚本分析

    本文档基于hbase-0.96.1.1-cdh5.0.2,对HBase配置&启动脚本进行分析 date:2016/8/4 author:wangxl HBase配置&启动脚本分析 剔除 ...

  7. [hdu5113]Black And White2014北京赛区现场赛B题(搜索加剪枝)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Black And White Time Limit: 2000/2000 MS ...

  8. self和this的不同

    在Java和C++中,this总是指的是当前实例地址,而在静态方法也就是类方法中,是不可以使用this的.在Objectvie-C中,self是既可以出现在实例方法中,也可以出现在类方法中,并且在不同 ...

  9. js验证身份证格式

    (function(){ Validate={ data:{ // 加权因子 Wi : [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ...

  10. python-整理--pip whl命令

    如果要在windows系统上安装新的包,可以下载*.exe安装文件,双击下一步...,如果找不到exe的话. 在CMD中执行 pip install 安装包文件.whl 就可以安装了 pip这个命令本 ...