单链表(Single Linked List)
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)的更多相关文章
- C#单链表
顺序表是用地址连续的存储单元顺序存储线性表中的各个数据元素, 逻辑上相邻的数据元素在物理位置上也相邻.因此,在顺序表中查找任何一个位置上的数据元素非常方便, 这是顺序存储的优点. 但是, 在对顺序表进 ...
- 【数据结构】单链表&&静态链表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...
- 《数据结构》2.3单链表(single linked list)
//单链表节点的定义 typedef struct node { datatype data; struct node *next; }LNode,*LinkList; //LNode是节点类型,Li ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [算法][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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
[114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
- [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 ...
随机推荐
- java调试一
Eclipse 平台的特色在于内置了 Java 调试器,该调试器提供所有标准调试功能,包括进行单步执行.设置断点和值.检查变量和值以及暂挂和恢复线程的能力.Eclipse 平台工作台(Eclipse ...
- (转) 谈C/C++指针精髓
原文:http://www.cnblogs.com/madengwei/archive/2008/02/18/1072410.html [摘要] 指针是C和C++语言编程中最重要的概念之一, ...
- (原)使用vectot的.end()报错:iterators incompatible
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5070672.html 参考网址: http://blog.csdn.net/yxnyxnyxnyxny ...
- yii2 去掉index.php的方法
1.开启apache-rewrite 在Windows下,我们一般使用的是Administrator账号,所以启用这两项非常简单: 在[Apache安装目录]/conf/httpd.conf中找到 # ...
- FTP之主动模式vs被动模式
背景说明 最近有个项目涉及到FTP的上传下载问题.在本地开发好的程序测试的时候能正常获取FTP内容,但一放到生产上却显示connection timeout,无法连接.经过一些研究,发现是防火墙造成的 ...
- [C++程序设计]引用
对一个数据可以使用“引用”(reference),这是C++对C的一个重要扩充,引用是一种新的变量类型,它的作用是为一个变量起一个别名.假如有一个变量a,想给它起一个别名b,可以这样写:int a; ...
- MAC OS中使用ll,la命令
在linux下习惯了使用ll.la等ls别名 用mac os发现没有这样的命令,很不方便. 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可:alias ll='ls ...
- query通用开源框架
Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...
- mysql不能链接远程,报(Host '***.***.***.***' is not allowed to connect to this MySQL server)
Host '***.***.***.***' is not allowed to connect to this MySQL server 其中***...是本机公网ip; 解决办法: 首先看报错窗口 ...
- Java程序员需要学习的知识点
Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你 ...