• 数据结构(C语言版)01


    1. //顺序存储
    2. int main(){
    3. int ans[5]={1,1,1,1,3};//定义并初始化
    4. printf("%d",ans[4]);
    5. return 0;
    6. }
    1. //链式存储
    2. Typdef struct Lnode{
    3. ElemType data;
    4. struct Lnode *next;
    5. }Londe,*LinKlist;
    6. Londe *L;
    7. L=(LinkList)malloc(sizeof(Lnode));
    8. A->next=B;B->next=C;

    线性表:n个想同类型的元素组成的有序集合。(个数有限,每个元素占用相同大小空间,具有逻辑上的顺序性)

    直接前驱,直接后继。 

    顺序表: 线性表的顺序表示(逻辑上相邻,物理上也相邻)

    优点:可以随机存取表中任意一个,存储密度高,每个结点只存储数据元素。

    缺点:插入和删除操作移动大量元素,数组的大小不好确认,造成很多碎片。

    1. //顺序表的定义
    2. #define Maxsize 50
    3. typedef int Elemtype;
    4. //静态分配
    5. typedef struct {
    6. Elemtype data[Maxsize];
    7. int len;
    8. }Sqlist;
    9. //动态分配
    10. #define InitSize 100;
    11. typedef struct {
    12. Elemtype *data;
    13. int capacity;
    14. int lenth;
    15. }SepList;
    16. //插入操作,插入到第i个元素前面 //L.data[i-1]=e
    17. bool insert(Sqlist&L,int i,Elemtype e){
    18. if(i>L.len+1||i<0) return false;
    19. if(L.len>Maxsize) return false;
    20. for(int j=L.len;j>i-1;j--)
    21. L.data[j]=L.data[j-1];
    22. L.data[i-1]=e;
    23. L.len++;
    24. return true;
    25. }
    26. //删除操作,删除第i个元素
    27. bool delet(Sqlist&L,int i){
    28. if(i>L.len||i<1) return false;
    29. if(L.len>Maxsize) return false;
    30. int x=L.data[i-1];
    31. for(int j=i-1;j
    32. L.data[j]=L.data[j+1];
    33. L.len--;
    34. return true;
    35. }
    36. void printSq(Sqlist L){
    37. for(int i=0;i
    38. printf("%4d",L.data[i]);
    39. printf("\n");
    40. }
    41. int main(){
    42. Sqlist L;
    43. bool ans;
    44. Elemtype del;
    45. for(int i=0;i<3;i++)
    46. L.data[i]=i;
    47. L.len=3;
    48. ans=insert(L,2,60);
    49. if(ans) printSq(L);
    50. ans=delet(L,1);
    51. if(ans) printSq(L);
    52. return 0;
    53. }

    知识点补充:

    1. C的初始动态分配语句:
    2. L.data=(ElemType*)malloc(sizeof(ElemType)*InitSize)

     链表:逻辑上相邻的两个元素在物理位置上不相邻。

    单链表:

    优点:插入删除不需要移动元素,只需要修改指针;不需要大量连续的存储空间。

    缺点:附加指针域,浪费存储空间;查找时每次都从头开始,依次查找,不能随机存取

    1. //单链表
    2. typedef struct LNode{
    3. ElemType data;
    4. struct LNode *next;
    5. }LNode,*LinkList;

    头指针:链表中第一个节点的存储位置,用来标识单链表

    头结点:在单链表第一个(有值)的节点之前附加的一个结点,为了操作上的方便。(其data一般为空)有了头结点后,对在第一个结点前插入和删除第一个结点就可以统一操作,不用频繁的重置头指针。(但头结点实际上是可有可无的)

    1. //创建新节点
    2. p=(LNode*)malloc(sizeof(LNode))
    3. p->data=x;
    4. //按序号查找结点值
    5. LNode *p=L->next;
    6. int j=1;
    7. while(p&&j
    8. p=p->next;
    9. j++;
    10. }
    11. return p;
    12. //按值查找
    13. LNode q=L->next;
    14. while(q&&q.data!=e)
    15. q=q->next;
    16. return q;

    对应练习作业:王道OJ | 课时10作业 (lgwenda.com)

    答案:核心代码不难,注意输出的格式!!!!

    1. //oj-10
    2. //初始化顺序表
    3. #include
    4. #define MaxSize 50
    5. typedef struct{
    6. int len;
    7. int data[MaxSize];
    8. }Sqlist;
    9. //插入元素
    10. bool inseret(Sqlist &L,int i,int e){
    11. if(i>=L.len||i<1) return false;
    12. if(L.len>=MaxSize) return false;
    13. for(int j=L.len;j>=i;j--)
    14. L.data[j]=L.data[j-1];
    15. L.data[i-1]=e;
    16. L.len++;
    17. return true;
    18. }
    19. //删除元素
    20. bool delet (Sqlist &L,int i){
    21. if(i>L.len||i<1) return false;
    22. int x=L.data[i];
    23. for(int j=i-1;j-1;j++){
    24. L.data[j]=L.data[j+1];
    25. }
    26. L.len--;
    27. return true;
    28. }
    29. void print(Sqlist L){
    30. for(int i=0;i
    31. printf("%3d",L.data[i]);
    32. printf("\n");
    33. //return 0;
    34. }
    35. //main()
    36. int main(){
    37. Sqlist L;
    38. L.data[0]=1;
    39. L.data[1]=2;
    40. L.data[2]=3;
    41. L.len=3;
    42. int e;scanf("%d",&e);
    43. if(inseret(L,2,e)) print(L);
    44. else printf("false\n");
    45. int i;scanf("%d",&i);
    46. if(delet(L,i)) print(L);
    47. else printf("false\n");
    48. return 0;
    49. }

  • 相关阅读:
    mybatis-plus的插件
    vue3详解
    机器学习实战(6)——决策树
    如何卸载在linux下通过rpm安装的mysql
    ubuntu20.04安装repo
    正则表达式
    [附源码]计算机毕业设计springboot右脑开发教育课程管理系统
    支付宝支付
    图卷积神经网络层的pytorch复现
    6月27日云技术研讨会 | 中央集中架构新车型功能和网络测试解决方案
  • 原文地址:https://blog.csdn.net/m0_67403773/article/details/136353112