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

    1. NSURL的简介 URL是对可以从互联网上得到的资源的位置和访问方法的一种简介的表示,是互联网上标准资源的地址.URL可能包含远程服务器上的资源位置,本地磁盘上的文件的路径,甚至任意一段编码的数 ...

  2. CDZSC_2015寒假新人(2)——数学 H

    H - H Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  3. (原)opencv直线拟合fitLine

    转载请注明出处 http://www.cnblogs.com/darkknightzh/p/5486234.html 参考网址: http://blog.csdn.net/thefutureisour ...

  4. mysql中查看字符集的cmd指令

    参看下面链接:http://blog.chinaunix.net/uid-20180960-id-1972668.html

  5. PHP--变量部分知识点

    PHP全局变量 PHP全局变量作用域不同与C,在函数内部不可以使用全局变量,要在函数内部使用全局变量需要,global $var或者使用超全局变量数组$GLOBALS['var']. 静态变量 PHP ...

  6. dropdownlist绑定和选中

    最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家: 现在是后台数据绑定 protected void BindCarID() { ...

  7. 我的开源框架之TAB控件

    需求 (1)支持iframe.html.json格式的tab内容远程请求 (2)支持动态添加tab (3)支持远程加载完成监听,支持tab激活事件监听 (4)支持reload tab内容[如果是远程加 ...

  8. [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out

    datafile.txt  #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...

  9. spl_autoload_register()

    5.3版本增加了命名空间prepend函数   <?php // function __autoload($class) {//     include 'classes/' . $class  ...

  10. 磁盘管理二-LVM相关内容

    1.基本概念 LVM:logical volume manager 逻辑卷管理器 LVM构成:物理卷PV,卷组VG(PE物理区域,最小存储单元),逻辑卷LV(LE逻辑区域,最小存储单元) 三者如下图所 ...