您的位置:首页 > 理论基础 > 数据结构算法

数据结构—链表的基本操作 子函数的方式

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);}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: