全家桶,齐全"/>
链表全家桶,齐全
目录
链表的概念:
1.创建结点:
2.链表的创建(输入):
3.链表的输出:
4.链表的求最大值(max):
5.链表的插入:
6.链表的删除:
7.主函数代码:
链表的概念:
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
链表的优缺点:
优点:在插入和删除操作时,只需要修改被删节点上一节点的链接地址,不需要移动元素,从而改进了在顺序存储结构中的插入和删除操作需要移动大量元素的缺点。
缺点:
1、没有解决连续存储分配带来的表长难以确定的问题。
2、失去了顺序存储结构随机存取的特性。
1.创建结点:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//创建结点结构体
struct node{int data; //数据域 struct node *next; //指针域
};
2.链表的创建(输入):
int create(struct node *head){struct node *p=head; //2、定义头指针指向头结点 //3、循环创建5个结点 struct node *q; //新结点 int i;for(i=0;i<5;i++){q=(struct node *)malloc(sizeof(struct node)); printf("请输入数据:");scanf("%d",&q->data); //数据域赋值// 头插法q->next=head->next; head->next=q;}
}
create 的意思就是创建,
struct node *p=head; //2、定义头指针指向头结点 地址指向第一个结点
3.链表的输出:
int shuchu(struct node *head){struct node *p=head;p=p->next; //跳过头结点,指向第一个有数据的结点 //结果---逆序输出
// struct node *p=head->next; while(p!=NULL){printf("%d ",p->data); //数据当前结点的数据 p=p->next; //跳到下一个节点上 }
}
28行:代码p=p->next 指向第一个有数据的结点,存放数据的位置
NULL 表示 空值 意思也很明确就是什么数据也没有的意思
当p所指向的空间是有数据的就输出数据,
4.链表的求最大值(max):
int max(struct node *head){struct node *p=head->next;int max=p->data; //把该结点的数当成最大值while(p!=NULL){if(max<p->data){max=p->data;} p=p->next; //指针后移 //先比较,再指针后移 (不然存在p变成NULL,还取数据的情况) } printf("最大值为:%d\n",max);
}
int max 定义一个 整型变量,把p所指向的数据域的存放第一个数据的值给max ,把第一个值当成最大值再与其余数据进行比较,如果比我们假设的最大值还大,那么就是更新 max 的值
91行代码: p=p->next; //指针后移 指针后移 说白就是往后移动一位,87行代码是循环,88行代码就是判断max的值是否比其他数据大,大的话就互换位置,更新最大值,
最后再输出其max的值,就是我们所有数据的最大的那个数。
5.链表的插入:
int charu(struct node *head){int n,i; struct node *p=head;printf("\n请输入你要插入数据的位置:");scanf("%d",&n); for(i=1;i<n;i++){ // 循环n-1 p=p->next; //跳到下一个结点 if(p->next==NULL){ //当前结点指针域为NULL,没法断开 printf("位置不合适!");return 0; } }//创建要插入的节点struct node *a; //a 是要插入的结点 a=(struct node *)malloc(sizeof(struct node)); printf("请输入数据:");scanf("%d",&a->data); //插入到对应位置a->next=p->next; p->next=a;
}
这就是插入数据的完整代码,n 就是我们要插入的数据的位置,i 是我们数据的下标
下标从1开始,当<n时,我们就找到了我们想插入的位置,if 判断 p->next==NULL 表示
当前结点指针域为 NULL (为空),没有办法断开,也就表示没有办法插入我们想
插入的数据,
6.链表的删除:
int shanchu(struct node *head){struct node *p=head; //指向头结点int n;printf("\n请输入你要删除的位置(第几个数):");scanf("%d",&n); //跳到删除的前一位 int i;for(i=1;i<n;i++){ //循环n-1次p=p->next; //合理判断一下 if(p->next==NULL){printf("删除不合适!");return 0; } }
// p->next=p->next->next; //删除结点struct node *del=p->next; //del 保存你要删除的结点p->next=del->next; free(del); //释放删除的结点
}
链表的插入和链表的删除是有着相似之处的,也可以套用插入的模块儿的,先输入要删除的位置(第几个数),通过下标找到的,然后在循环里判断一下,我们删除的位置,是否在我们的范围之内,不在就显示不合适,在的话,就继续往下执行。
7.主函数代码:
int main(){//创建链表// 1、创建头结点,指针域为NULLstruct node *head=(struct node *)malloc(sizeof(struct node)); head->next=NULL;create(head); //创建函数 shuchu(head); //输出函数 charu(head); //插入函数 shuchu(head); //输出函数 shanchu(head); //删除函数 shuchu(head); //输出函数 max(head); //最大函数
}
下面是完整的代码,可复制:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//创建结点结构体
struct node{int data; //数据域 struct node *next; //指针域
}; int create(struct node *head){struct node *p=head; //2、定义头指针指向头结点 //3、循环创建5个结点 struct node *q; //新结点 int i;for(i=0;i<5;i++){q=(struct node *)malloc(sizeof(struct node)); printf("请输入数据:");scanf("%d",&q->data); //数据域赋值// 头插法q->next=head->next; head->next=q;}
} int shuchu(struct node *head){struct node *p=head;p=p->next; //跳过头结点,指向第一个有数据的结点 //结果---逆序输出
// struct node *p=head->next; while(p!=NULL){printf("%d ",p->data); //数据当前结点的数据 p=p->next; //跳到下一个节点上 }
} int charu(struct node *head){int n,i; struct node *p=head;printf("\n请输入你要插入数据的位置:");scanf("%d",&n); for(i=1;i<n;i++){ // 循环n-1 p=p->next; //跳到下一个结点 if(p->next==NULL){ //当前结点指针域为NULL,没法断开 printf("位置不合适!");return 0; } }//创建要插入的节点struct node *a; //a 是要插入的结点 a=(struct node *)malloc(sizeof(struct node)); printf("请输入数据:");scanf("%d",&a->data); //插入到对应位置a->next=p->next; p->next=a;
} int shanchu(struct node *head){struct node *p=head; //指向头结点int n;printf("\n请输入你要删除的位置(第几个数):");scanf("%d",&n); //跳到删除的前一位 int i;for(i=1;i<n;i++){ //循环n-1次p=p->next; //合理判断一下 if(p->next==NULL){printf("删除不合适!");return 0; } }
// p->next=p->next->next; //删除结点struct node *del=p->next; //del 保存你要删除的结点p->next=del->next; free(del); //释放删除的结点
}int max(struct node *head){struct node *p=head->next;int max=p->data; //把该结点的数当成最大值while(p!=NULL){if(max<p->data){max=p->data;} p=p->next; //指针后移 //先比较,再指针后移 (不然存在p变成NULL,还取数据的情况) } printf("最大值为:%d\n",max);
} int main(){//创建链表// 1、创建头结点,指针域为NULLstruct node *head=(struct node *)malloc(sizeof(struct node)); head->next=NULL;create(head); //创建函数 shuchu(head); //输出函数 charu(head); //插入函数 shuchu(head); //输出函数 shanchu(head); //删除函数 shuchu(head); //输出函数 max(head); //最大函数
}
为了方便我就用拼音代替了,本人表示英语这一块是有点缺陷的,哈哈哈😄,不要介意,目前还在学习的阶段,如果有什么不对的,或者有什么缺陷的,还请各位大神多多指点指点,嘿嘿(●ˇ∀ˇ●)
更多推荐
链表全家桶,齐全
发布评论