单向链表的建立、打印、 删除
2015-09-22 15:55
363 查看
#include <stdlib.h> /*含ma l l o c ( ) 的头文件*/ #include <stdio.h> typedef struct Student { int data; struct Student *next; }node; node* del(node *head, int num)//删除链表中的一个数(最早出现) { node *point,*temp; point=head; while((num!=point->data)&&(point->next!=NULL)) { temp=point; printf("want to find %d\n",point->data); point=point->next; } if(num==point->data) { <span style="white-space:pre"> </span>printf("have found %d\n",point->data); if (point==head) { point =head->next; free(point); } else { temp->next=point->next; } } else { printf("\n%d 找不到\n",num); } return(head); }
node* creat(){ node *head , *temp , *s; int x,cycle=1; head =(node*)malloc(sizeof(node));//申请空间 head存储在 temp=head; printf(" temp=%d\n temp->next=%d\n head=%d\n head->next=%d\n",temp,temp->next,head,head->next ); while(cycle) { printf("请输入数据:\n"); scanf("%d",&x); if (x!=0) { s=(node*)malloc(sizeof(node)); temp->next=s; temp=s; //建立链接,注意顺序 s->data=x; printf("%d is saved in %d \n",s->data,s); printf(" temp=%d\n temp->next=%d\n head=%d\n head->next=%d\n",temp,temp->next,head,head->next ); } else { cycle=0; } } head=head->next; //赋值头链接地址 temp->next=NULL; printf("head is in %d\n",head ); return(head);}void print(node *head ){ node *p; p=head; if (head!=NULL) { while(p!=NULL) { printf("uuu %d\n",p->data); p=p->next; } } }main( ) { struct node *head; head=NULL; //②建一个空表 print(creat(head));/*打印单链表*/ }
相关文章推荐
- Spring中的jdbcTemplate.queryForObject()使用
- Android应用开发EditText文本内容变化监听方法
- MySql篇
- java基于AbstractQueuedSynchronizer实现资源共享锁,限制并发线程数目
- 应用程序启动初始化失败 0xc0150002,请点击确定关闭应用程序
- PIC16F887 RB0外部中断(2)
- Android 获取屏幕尺寸
- Bochs调试手册(英)
- [MySQL分享]MySQL启动以及my.cnf参数文件结构小结
- 继承Thread父类和实现Runnable接口的区别
- 服务器基础问题
- SecureCRT配置
- js数组算法总结
- svg中的path指令分析
- UIView动画
- jquery Select2 学习笔记之中文提示
- H2.64中I帧和IDR帧的区别
- C语言第一节-进制,运算符,输入输出
- 关于WPF转换.net平台的工作
- BitmapFactory.Options避免 内存溢出 OutOfMemoryError的优化方法