单链表(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 ...
随机推荐
- 【JavaScript DOM 编程艺术】 笔记
第一章:JavaScript 简史 1.1 javascript的起源 JavaScript是一种脚本语言,通常只能通过web浏览器去完成一些操作而不能像普通意义上的程序那样独立运行,需要由Web浏览 ...
- 关于Struts2的类型转换详解
详细出处参考:http://www.jb51.net/article/35465.htm 一.类型转换的意义 对于一个智能的MVC框架而言,不可避免的需要实现类型转换.因为B/S(浏览器/服务器)结构 ...
- (转) Name visibility
Scopes Named entities, such as variables, functions, and compound types need to be declared before b ...
- hdu 1042 N!(高精度乘法)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in ...
- CSS3动画之旋转魔方盒
步骤: 1.大盒子里盛放六个子盒子代表立方体的6个面: 2.子盒子开启3d效果 transform-style:preserve-3d; 3.上下面沿X轴旋转90度,一个上移盒子一半高,一个下移盒子 ...
- Android 周报
1. https://androidsweets.ongoodbits.com/ 2. https://www.androiddevdigest.com/ 3.http://us12.campaign ...
- DataSnap
一. DataSnap REST - http://docwiki.embarcadero.com/RADStudio/Berlin/en/DataSnap_REST 1. URI Mapping: ...
- 最短路(Dijkstra模板题)
就不写题目链接了 Sample Input 5 5 点个数a,边个数b 1 2 20 点,点,权值 2 3 30 3 4 20 4 5 20 1 5 100 求出1到a的最短距离 Sample Out ...
- [TYVJ] P1006 ISBN
ISBN 背景 Background NOIP2008年普及组第一题 描述 Description 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位 ...
- 【其他】IT公司的企业文化与竞争力
一直觉得三流企业靠成本竞争,二流企业靠体制竞争,一流企业靠文化竞争. 企业在竞争时候,总会提到一个词:核心竞争力.对于IT企业来说,核心竞争是什么?无论是技术也好,销售也罢,归根到底还是人才的竞争,优 ...