数据结构—链表的基本操作 子函数的方式
2016-06-15 00:00
471 查看
#include<stdio.h> #include<stdlib.h> // 创建结构体链表 typedef int TELM; typedef struct link{ <pre name="code" class="plain">TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> date1; //数据域</span>struct students * next; //指针域}stu; // 添加数据函数void *add(){stu *head=NULL;stu *p=NULL;stu *temp=NULL;
TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> n; </span>printf("请输入数据,为0时退出: ");scanf("%d",&n); while(n!=0){ p=(stu *)malloc(sizeof(stu));p->date1=n;p->next=NULL;if(head==NULL){head=p;}else{ temp->next=p;} temp=p; printf("请输入数据,为0时退出: "); scanf("%d",&n); }return head;}// 修改函数void change(stu *head){stu *p;
TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> i;</span>
TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> n;</span>printf("请输入要修改的数据: ");scanf("%d",&i);//初始化指针p=head; while(p!=NULL){if(p->date1==i){printf("请输入新数据 ");scanf("%d",&n);p->date1=n;}p=p->next; }}// 查找函数void search(stu * head){
TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> i;</span>stu *p;int m=1;printf("请输入要查找的数据 ");scanf("%d",&i);p=head;while(p!=NULL){if(p->date1==i){printf("恭喜!存在你查找的数据,它是第个%d数据 \n",m);} p=p->next;m++; } }// 删除函数void del(stu * head){
TELM <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> i;</span>stu *p=NULL;printf("请写出要删除的数据 \n");scanf("%d",&i);p=head;while(1){if(p->next->next){if(p->next->date1==i){p->next=p->next->next;}else{p=p->next;}}else{if(p->next->date1==i)p->next=NULL;break;}}p=head;if(p->date1==i){head=p->next;}}// 输出函数void print(stu *head){ stu *p=head; printf("<-------目前输入的数据有如下------>\n");while(p!=NULL){ printf("%d ",p->date1);p=p->next;}}// 主函数void main(){stu *head=NULL;head=add();print(head);change(head);print(head);search(head);print(head);del(head);print(head);}
相关文章推荐
- 数据结构 —栈的创建、出、进栈
- 数据结构—逆序输出
- JavaScript数据结构排序
- 数据结构学习--快速排序算法
- 数据结构与算法之十 提高二叉搜索树的效率
- 数据结构与算法之十 提高二叉搜索树的效率
- 数据结构与算法之十 提高二叉搜索树的效率
- Redis数据结构
- Java数据结构之简单的链表的实现
- 数据结构之二叉搜索树
- HDUOJ_1754_I Hate It_线段树
- 数据结构实验之链表三:链表的逆置
- 数据结构之红黑树
- 杨辉三角队列方式
- Map的数据结构
- AC自动机的一种简单实现
- 浅谈算法和数据结构: 十一 哈希表
- 浅谈算法和数据结构: 十二 无向图相关算法基础
- 浅谈算法和数据结构: 十 平衡查找树之B树
- 浅谈算法和数据结构: 九 平衡查找树之红黑树