- //顺序存储
- int main(){
- int ans[5]={1,1,1,1,3};//定义并初始化
- printf("%d",ans[4]);
- return 0;
- }
- //链式存储
- Typdef struct Lnode{
- ElemType data;
- struct Lnode *next;
- }Londe,*LinKlist;
-
- Londe *L;
- L=(LinkList)malloc(sizeof(Lnode));
- A->next=B;B->next=C;
线性表:n个想同类型的元素组成的有序集合。(个数有限,每个元素占用相同大小空间,具有逻辑上的顺序性)
直接前驱,直接后继。
顺序表: 线性表的顺序表示(逻辑上相邻,物理上也相邻)
优点:可以随机存取表中任意一个,存储密度高,每个结点只存储数据元素。
缺点:插入和删除操作移动大量元素,数组的大小不好确认,造成很多碎片。
- //顺序表的定义
- #define Maxsize 50
- typedef int Elemtype;
- //静态分配
- typedef struct {
- Elemtype data[Maxsize];
- int len;
- }Sqlist;
- //动态分配
- #define InitSize 100;
- typedef struct {
- Elemtype *data;
- int capacity;
- int lenth;
- }SepList;
-
- //插入操作,插入到第i个元素前面 //L.data[i-1]=e
- bool insert(Sqlist&L,int i,Elemtype e){
- if(i>L.len+1||i<0) return false;
- if(L.len>Maxsize) return false;
- for(int j=L.len;j>i-1;j--)
- L.data[j]=L.data[j-1];
- L.data[i-1]=e;
- L.len++;
- return true;
- }
- //删除操作,删除第i个元素
- bool delet(Sqlist&L,int i){
- if(i>L.len||i<1) return false;
- if(L.len>Maxsize) return false;
- int x=L.data[i-1];
- for(int j=i-1;j
- L.data[j]=L.data[j+1];
- L.len--;
- return true;
- }
-
- void printSq(Sqlist L){
- for(int i=0;i
- printf("%4d",L.data[i]);
- printf("\n");
- }
-
- int main(){
- Sqlist L;
- bool ans;
- Elemtype del;
- for(int i=0;i<3;i++)
- L.data[i]=i;
- L.len=3;
- ans=insert(L,2,60);
- if(ans) printSq(L);
- ans=delet(L,1);
- if(ans) printSq(L);
- return 0;
- }
知识点补充:
- C的初始动态分配语句:
- L.data=(ElemType*)malloc(sizeof(ElemType)*InitSize)
链表:逻辑上相邻的两个元素在物理位置上不相邻。
单链表:
优点:插入删除不需要移动元素,只需要修改指针;不需要大量连续的存储空间。
缺点:附加指针域,浪费存储空间;查找时每次都从头开始,依次查找,不能随机存取
- //单链表
- typedef struct LNode{
- ElemType data;
- struct LNode *next;
- }LNode,*LinkList;
头指针:链表中第一个节点的存储位置,用来标识单链表
头结点:在单链表第一个(有值)的节点之前附加的一个结点,为了操作上的方便。(其data一般为空)有了头结点后,对在第一个结点前插入和删除第一个结点就可以统一操作,不用频繁的重置头指针。(但头结点实际上是可有可无的)
- //创建新节点
- p=(LNode*)malloc(sizeof(LNode))
- p->data=x;
-
- //按序号查找结点值
- LNode *p=L->next;
- int j=1;
- while(p&&j
- p=p->next;
- j++;
- }
- return p;
- //按值查找
- LNode q=L->next;
- while(q&&q.data!=e)
- q=q->next;
- return q;
对应练习作业:王道OJ | 课时10作业 (lgwenda.com)
答案:核心代码不难,注意输出的格式!!!!
- //oj-10
- //初始化顺序表
- #include
- #define MaxSize 50
- typedef struct{
- int len;
- int data[MaxSize];
- }Sqlist;
- //插入元素
- bool inseret(Sqlist &L,int i,int e){
- if(i>=L.len||i<1) return false;
- if(L.len>=MaxSize) return false;
- for(int j=L.len;j>=i;j--)
- L.data[j]=L.data[j-1];
- L.data[i-1]=e;
- L.len++;
- return true;
- }
- //删除元素
- bool delet (Sqlist &L,int i){
- if(i>L.len||i<1) return false;
- int x=L.data[i];
- for(int j=i-1;j
-1;j++){ - L.data[j]=L.data[j+1];
- }
- L.len--;
- return true;
- }
- void print(Sqlist L){
- for(int i=0;i
- printf("%3d",L.data[i]);
- printf("\n");
- //return 0;
- }
- //main()
- int main(){
- Sqlist L;
- L.data[0]=1;
- L.data[1]=2;
- L.data[2]=3;
- L.len=3;
- int e;scanf("%d",&e);
- if(inseret(L,2,e)) print(L);
- else printf("false\n");
- int i;scanf("%d",&i);
- if(delet(L,i)) print(L);
- else printf("false\n");
- return 0;
- }
-
相关阅读:
mybatis-plus的插件
vue3详解
机器学习实战(6)——决策树
如何卸载在linux下通过rpm安装的mysql
ubuntu20.04安装repo
正则表达式
[附源码]计算机毕业设计springboot右脑开发教育课程管理系统
支付宝支付
图卷积神经网络层的pytorch复现
6月27日云技术研讨会 | 中央集中架构新车型功能和网络测试解决方案
-
原文地址:https://blog.csdn.net/m0_67403773/article/details/136353112