链表的结点结构
 ┌───┬───┐
 │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. 企业qq代码,工作中用到的

    <div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...

  2. php的一些特殊用法

    php ruturn的另一个用法 database.php <?php return array ( 'hostname' => 'localhost', 'database' => ...

  3. Linux中Firefox——Firebug插件安装及使用

    Firebug的安装方法即打开方式同httpfox Firebug使用指南: Firebug可以随时编辑页面:在HTML标签中,点击窗口上方的"inspect"命令,然后再选择页面 ...

  4. Gengxin讲STL系列——Set

    本系列第二篇blog 第一篇写的心潮澎湃,结果写完一看,这都是些什么玩意= =| Set的中文名称是“集合”.集合,高一数学必修一课本给出的定义已经很明确了,简单来讲就是一个不含重复元素的空间(个人定 ...

  5. (原)VS2013在Release情况下使用vector有时候会崩溃的一个可能原因

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5016352.html 参考网址: http://www.cnblogs.com/BryZ/archiv ...

  6. android ant 最简单的打包签名,混淆方法

    使用ant打包,如果脚本都是我们自己一步一步来写的话,是一个比较麻烦的东西. 关于ant,我们详细看下: ant支持 ant debug,ant release等命令,我们需要签名混淆,那么就需要an ...

  7. ETL概述

    转自:http://blog.csdn.net/leosoft/article/details/4279536 ETL,Extraction-Transformation-Loading的缩写,中文名 ...

  8. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  9. hibernate update部分更新

    hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...

  10. mysql之7xtrabackup

    目录: 1.前言 2.环境 3.开始备份 3.1.innobackupex介绍 3.2.一次完全备份 3.3.一次完全恢复 3.4.增量备份 3.5.增量备份的恢复过程 1.前言: Xtrabacku ...