链表的结点结构
 ┌───┬───┐
 │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. 使用图片拉伸resizableImageWithCapInsets

    在仿写QQ会话的时候背景蓝色图片是拉伸而来,但是有些地方是受保护的不能拉伸 所以定义了下面的工具类中的一个方法,专门拉伸图片 UIImageResizingModeStretch:拉伸模式,通过拉伸U ...

  2. SQL重复记录查询的几种方法(转)

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people ) 2.删除表中多余的重复记录,重复记录是根据单个字段(people ...

  3. for()循环

    今天发现自己一直以来都搞错了for()循环的执行顺序.这么简单的问题一直都错了,我也是醉了. ;i>&&a[i]>a[i-];--i) { } //即 for(init_s ...

  4. MySQL 常用字段类型,介绍及其建表使用方法经验分享

    由于工作的公司没有专业的DBA又经常需要建立各种各种的表来满足自己的业务逻辑,所以经常查看MySQL 手册或者谷歌查看相关资料,所以本人就根据我的工作经验和相关资料来介绍一下MySQL各种字段类型及其 ...

  5. css制作最简单导航栏

    css制作最简单导航栏

  6. hdu 1208 Pascal's Travels

    http://acm.hdu.edu.cn/showproblem.php?pid=1208 #include <cstdio> #include <cstring> #inc ...

  7. 优盘文件系统(FOR C)

    优盘上的数据按照其不同的特点和作用大致可分为5 部分:MBR 区.DBR 区.FAT 区.FDT区和DATA 区. 主引导记录(MBR) 绝对扇区号为:MBR_LBA=0x00000000 处是主引导 ...

  8. asm_c515c.uew

    /L20"ASM for C515C" Nocase Line Comment = ; String Chars = "' DisableMLS File Extensi ...

  9. 懒猫们终究要付出代码(本领是一生的),鲸鱼们的短视(逐小利而暴死)——这么说我应该只去互联网公司:IM,云存储,邮箱(别的一概不考虑)

    摘自周鸿伟的书,好像:

  10. C# GC 垃圾回收

    一.托管 .Net所指的托管资源到底是什么意思呢?是相对于所有资源,还是只限于某一方面的资源?很多人对此不是很了解. 其实.Net所指的托管只是针对内存这一个方面,并不是对于所有的元素:因此对于Str ...