我的日记——链表的创建
2015-09-12 10:44
211 查看
#include <iostream> #include <cstdio> #include <cstdlib> #include<malloc.h> #define ERROR 0 using namespace std; typedef struct student{ int date; struct student *next; }node,*Linklist; Linklist create() { Linklist p1,head,p2; p1=(Linklist)malloc(sizeof(node)); if(p1==NULL) return NULL; int n=0; cin>>p1->date; while(p1->date!=0) { n+=1; if(n==1) { head=p1; p2=p1; p2->next=NULL; } else { p2->next=p1; } p2=p1; if(n==5) p1->date=555555; p1=(Linklist)malloc(sizeof(node));//重新分配指针指向的地址和空间 cin>>p1->date; } free(p1); p2->next=NULL;//释放内存和指针 p1->next=NULL; return head; } void printfstudent(Linklist head) { int i=0; while(head->next!=NULL) { i+=1; printf("%d %d\n",i,head->date); head=head->next; } printf("%d %d\n",i+1,head->date); } int n; int main() { Linklist head; head = create(); printfstudent(head); return 0; }
相关文章推荐
- linux下卸载mysql rpm安装方式和源码安装方式的两种方法
- hdu-1013 Digital Roots
- php header()的用法总结
- socket,http,tcp区别
- hdu acm 2159 FATE
- Lynis基线检测使用解析
- 倒排序原理和实例
- C++大数据处理
- Mocking
- 人肉工程在机器学习实践中的作用
- C++基础之const系列
- HDOJ 1009
- 7.性能测试工具Locust的初级使用
- OC第四课
- ChartType属性
- iOS开发系列--Objective-C之类和对象
- html css 图片居中
- javascript闭包(Closure)初探
- 注意uiview的两个重绘函数
- UI中一系列 个人错误总结