当前位置:   article > 正文

数据结构——顺序表(C语言编写)_顺序表c语言代码

顺序表c语言代码

一、顺序表创建

输入

1 2 3 4 5 -1

 输出

1 2 3 4 5

题目分析

掌握使用  last  作为结构体的下标,不要自己出现奇怪的想法,要学会使用一些约定俗成的结构

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef int datatype;
  4. #define MAXLEN 50
  5. typedef struct
  6. {
  7. datatype data[MAXLEN];
  8. int last;
  9. }SeqList;
  10. SeqList* CreatSeqList()
  11. {
  12. SeqList *head=(SeqList *)malloc(sizeof(SeqList));
  13. head->last=-1;
  14. int x;
  15. while(scanf("%d",&x),x!=-1)
  16. {
  17. head->last++;
  18. head->data[head->last]=x;
  19. }
  20. return head;
  21. }
  22. void ShowSeqList(SeqList* L)
  23. {
  24. SeqList *p=L;
  25. for(int i=0;i<L->last;i++)
  26. printf("%d ",L->data[i]);
  27. printf("%d",L->data[L->last]);
  28. }
  29. int main()
  30. {
  31. SeqList *L;
  32. L=CreatSeqList();
  33. ShowSeqList(L);
  34. return 1;
  35. }

二、顺序表的插入删除

输入

121 aa 95 1

129 bb 90 3

111 cc 80 2

444 dd 70 2

1 1 1 -1 3

 输出

插入成功

121 aa 95

位置出错

121 aa 95

插入成功

121 aa 95

111 cc 80

插入成功

121 aa 95

444 dd 70

111 cc 80

请输入删除位序:

删除后:

121 aa 95

444 dd 70

 题目分析

注意一些边界条件,熟练掌握顺序表的插入和删除

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAXLEN 50
  5. typedef struct student //学生结构体类型
  6. {
  7. int num;
  8. char name[20];
  9. int n;
  10. }stu;
  11. typedef stu DataType;
  12. typedef struct //顺序表类型
  13. {
  14. DataType data[MAXLEN];
  15. int last;
  16. }SeqList;
  17. SeqList* InitSeqList() //创建一个空的顺序表
  18. {
  19. SeqList *L;
  20. L=(SeqList *)malloc(sizeof(SeqList));
  21. L->last=-1;
  22. return L;
  23. }
  24. int InsertSeqList(SeqList *Lq,int i,DataType x)
  25. {
  26. int k;
  27. if(i<1 || i > Lq->last+2 || Lq->last==MAXLEN-1)
  28. return 0;
  29. for(k=Lq->last;k>=i-1;k--)
  30. Lq->data[k+1]=Lq->data[k];
  31. Lq->data[i-1].n=x.n;
  32. strcpy(Lq->data[i-1].name,x.name);
  33. Lq->data[i-1].num=x.num;
  34. Lq->last++;
  35. return 1;
  36. }
  37. int DelSeqList(SeqList *Lq,int i)
  38. {
  39. int k;
  40. if(Lq->last==-1 || i<1 ||i>Lq->last+1)
  41. return 0;
  42. for(k=i;k<=Lq->last;k++)
  43. Lq->data[k-1]=Lq->data[k];
  44. Lq->last--;
  45. return 1;
  46. }
  47. void ShowSeqList(SeqList *L)
  48. {
  49. for(int i=0;i<=L->last;i++)
  50. printf("%d %s %d\n",L->data[i].n,L->data[i].name,L->data[i].num);
  51. }
  52. int main()
  53. {
  54. SeqList *Lq;
  55. DataType x;
  56. int flag; //flag用来记录操作是否成功
  57. int i;
  58. Lq=InitSeqList();
  59. i=0;//输入学生信息以及插入位置
  60. while(scanf("%d %s %d %d",&x.n, x.name, &x.num,&i),i!=-1)
  61. {
  62. if(InsertSeqList(Lq,i,x)==1)
  63. {
  64. printf("插入成功\n");
  65. ShowSeqList(Lq);
  66. }
  67. else
  68. {
  69. printf("位置出错\n");
  70. ShowSeqList(Lq);
  71. }
  72. }
  73. printf("请输入删除位序:\n");
  74. scanf("%d",&i);
  75. flag=DelSeqList(Lq,i); //删除顺序表中的某一个学生
  76. if(flag==1)
  77. {
  78. printf("删除后:\n");
  79. ShowSeqList(Lq);
  80. } //删除成功时输出顺序表,删除不成功时显示“不存在第i个元素”
  81. else
  82. printf("不存在第%d个元素\n",i);
  83. return 1;
  84. }

 三、顺序表应用——查找公共元素

输入

5 4 1 3 2 8 7 -1

2 1 4 -1

 输出

4 1 2

 题目分析

熟练运用各种函数的传递

  1. #include<stdio.h>
  2. #include"stdlib.h"
  3. #define MAXLEN 100
  4. typedef int datatype;
  5. typedef struct
  6. {
  7. datatype data [MAXLEN];
  8. int last;
  9. } SeqList;
  10. SeqList* CreatList_Seq()
  11. {
  12. SeqList *head=(SeqList*)malloc(sizeof(SeqList));
  13. head->last=-1;
  14. return head;
  15. }
  16. int InsList_Seq (SeqList *Lq, int i, datatype x)
  17. {
  18. int j;
  19. for(j=Lq->last;j>=i-1;j--)
  20. Lq->data[j+1]=Lq->data[j];
  21. Lq->data[i-1]=x;
  22. Lq->last++;
  23. return 1;
  24. }
  25. int SearchList_Seq (SeqList *Lq , datatype x )
  26. {
  27. int i=0;
  28. while(i<=Lq->last && Lq->data[i]!=x)
  29. i++;
  30. if(i<=Lq->last) return i;
  31. else return -1;
  32. }
  33. void ShowList_Seq(SeqList *Lq)
  34. {
  35. int i;
  36. for(i=0;i<=Lq->last;i++)
  37. printf("%d ",Lq->data[i]);
  38. }
  39. datatype Getdata(SeqList *h,int i)
  40. {
  41. return h->data[i];
  42. }
  43. void common(SeqList *a,SeqList *b,SeqList *c)
  44. {
  45. int i,j,pos;
  46. for(i=0;i<=a->last;i++)
  47. {
  48. pos = SearchList_Seq(b,Getdata(a,i));
  49. if(pos!=-1)
  50. InsList_Seq(c,c->last+2,Getdata(a,i));
  51. }
  52. }
  53. int main()
  54. {
  55. SeqList *a,*b,*c;
  56. datatype x;
  57. a=CreatList_Seq();
  58. b=CreatList_Seq();
  59. c=CreatList_Seq();
  60. while(scanf("%d",&x),x!=-1)
  61. a->data[++a->last] = x;
  62. while(scanf("%d",&x),x!=-1)
  63. b->data[++b->last] = x;
  64. common(a,b,c);
  65. ShowList_Seq(c);
  66. }

 四、顺序表应用——删除

 题目分析

学会顺序表删除操作

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef int datatype;
  4. #define MAXLEN 100
  5. typedef struct
  6. {
  7. datatype data[MAXLEN];
  8. int last;
  9. }SeqList;
  10. SeqList *CreatList_Seq()
  11. {
  12. SeqList *head=(SeqList *)malloc(sizeof(SeqList));
  13. head->last=-1;
  14. int x,i=0;
  15. while(scanf("%d",&x),x!=-1)
  16. {
  17. head->last++;
  18. head->data[head->last]=x;
  19. }
  20. return head;
  21. }
  22. int DelList(SeqList *L,int pos,int len)
  23. {
  24. int k,i;
  25. if (len<0 || L->last==-1 || pos < 1 || pos > L->last+1|| len+pos-1 > L->last+1) //下标越界
  26. return 0;
  27. for(k=pos;k<=L->last;k++)
  28. L->data[k-1]=L->data[k+len-1];
  29. L->last=L->last-len;
  30. return 1;
  31. }
  32. void ShowList_Seq(SeqList *L)
  33. {
  34. for(int i=0;i<L->last;i++)
  35. printf("%d ",L->data[i]);
  36. if(L->last!=-1)
  37. printf("%d",L->data[L->last]);
  38. }
  39. int main()
  40. {
  41. SeqList *L;
  42. int pos,len;
  43. L=CreatList_Seq();
  44. scanf("%d %d",&pos,&len);
  45. if(DelList(L,pos,len)==0) printf("删除失败\n");
  46. ShowList_Seq(L);
  47. return 0;
  48. }

五、顺序表应用——合并顺序表

输入

1 3 5 7 -1

2 4 6 8 -1

 输出

1 2 3 4 5 6 7 8

 题目分析

掌握顺序表合并

  1. #include<stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. #define MAXLEN 100
  5. typedef int datatype;
  6. typedef struct
  7. {
  8. datatype data [MAXLEN];
  9. int last;
  10. } SeqList;
  11. SeqList* CreatList_Seq()
  12. {
  13. SeqList *head=new SeqList;
  14. head->last=-1;
  15. return head;
  16. }
  17. int InsList_Seq (SeqList *Lq, int i, datatype x)
  18. {
  19. int j;
  20. if(Lq->last == MAXLEN-1||i<1||i>Lq->last+2)
  21. return 0;
  22. for(j=Lq->last;j>=i-1;j--)
  23. Lq->data[j+1]=Lq->data[j];
  24. Lq->data[i-1]=x;
  25. Lq->last++;
  26. return 1;
  27. }
  28. int SearchList_Seq (SeqList *Lq , datatype x )
  29. {
  30. int i=0;
  31. while(i<=Lq->last && Lq->data[i]!=x)
  32. i++;
  33. if(i<=Lq->last) return i;
  34. else return -1;
  35. }
  36. void ShowList_Seq(SeqList *Lq)
  37. {
  38. int i;
  39. for(i=0;i<=Lq->last;i++)
  40. cout<<Lq->data[i]<<' ';
  41. }
  42. datatype Getdata(SeqList *h,int i)
  43. {
  44. return h->data[i];
  45. }
  46. void Union(SeqList *a,SeqList *b,SeqList *c)
  47. {
  48. int i = 0, j = 0, k = 0;
  49. while( i <= a->last && j <= b->last)
  50. {
  51. if (a->data[i] <= b->data[j])
  52. c->data[k++] = a->data[i++];
  53. else
  54. c->data[k++] = b->data[j++];
  55. }
  56. while (i <= a->last) c->data[k++] = a->data[i++];
  57. while (j <= b->last) c->data[k++] = b->data[j++];
  58. c->last = k-1;
  59. }
  60. int main()
  61. {
  62. SeqList *a,*b,*c;
  63. datatype x;
  64. a=CreatList_Seq();
  65. b=CreatList_Seq();
  66. c=CreatList_Seq();
  67. while(scanf("%d",&x),x!=-1)
  68. a->data[++a->last] = x;
  69. while(scanf("%d",&x),x!=-1)
  70. b->data[++b->last] = x;
  71. Union(a,b,c);
  72. ShowList_Seq(c);
  73. }

五、多项式求和——顺序表实现

输入

2 2

1 1 

0 0

5 1

1 0

0 0

 输出

2 2 

6 1

1 0

 题目分析

掌握多项式求和

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define MAXLEN 100
  4. typedef struct xznode
  5. {
  6. int m; // 系数
  7. int n; // 指数
  8. }datatype;
  9. typedef struct
  10. {
  11. datatype data[MAXLEN];
  12. int last;
  13. } SeqList;
  14. SeqList* InitSeqList()
  15. {
  16. SeqList *L;
  17. L=new SeqList;
  18. L->last=-1;
  19. return L;
  20. }
  21. int InsSeqList(SeqList *L, int i, datatype x)
  22. {
  23. int j;
  24. for(j=L->last;j>=i;j--)
  25. {
  26. L->data[j+1]=L->data[j];
  27. }
  28. L->data[i]=x;
  29. L->last++;
  30. }
  31. int CreatSeqList(SeqList *L)
  32. {
  33. int n,m,i=0;
  34. while(scanf("%d %d",&m,&n),m!=0||n!=0)
  35. {
  36. L->data[i].n=n;
  37. L->data[i].m=m;
  38. i++;
  39. }
  40. L->last=i-1;
  41. }
  42. int LenSeqList(SeqList *L)
  43. {
  44. return L->last;
  45. }
  46. int Add_Seq(SeqList *A,SeqList *B,SeqList *C)
  47. {
  48. int i=0,j=0,k=0;
  49. while(1)
  50. {
  51. if(i>LenSeqList(A)&&j>LenSeqList(B)) break;
  52. if(i>LenSeqList(A)&&j<=LenSeqList(B))
  53. {
  54. InsSeqList(C,k,B->data[j]);
  55. k++;j++;
  56. }
  57. if(i<=LenSeqList(A)&&j>LenSeqList(B))
  58. {
  59. InsSeqList(C,k,A->data[i]);
  60. k++;i++;
  61. }
  62. if(i<=LenSeqList(A)&&j<=LenSeqList(B))
  63. {
  64. if(A->data[i].n > B->data[j].n)
  65. {
  66. InsSeqList(C,k,A->data[i]);
  67. k++;i++;
  68. }
  69. else if(A->data[i].n < B->data[j].n)
  70. {
  71. InsSeqList(C,k,B->data[j]);
  72. k++;j++;
  73. }
  74. else if(A->data[i].n == B->data[j].n)
  75. {
  76. InsSeqList(C,k,B->data[j]);
  77. C->data[k].m=A->data[i].m+B->data[j].m;
  78. k++;j++;i++;
  79. }
  80. }
  81. }
  82. }
  83. int ShowSeqList(SeqList *L)
  84. {
  85. int i;
  86. for(i=0;i<=LenSeqList(L);i++)
  87. {
  88. if(L->data[i].m!=0)
  89. printf("%d %d\n",L->data[i].m,L->data[i].n);
  90. }
  91. }
  92. int main()
  93. {
  94. SeqList *L1,*L2,*L3;
  95. L1=InitSeqList(); //初始化三个空表
  96. L2=InitSeqList();
  97. L3=InitSeqList();
  98. CreatSeqList(L1); //创建两个顺序表
  99. CreatSeqList(L2);
  100. Add_Seq(L1,L2,L3); //L1 L2相加之后结果放入L3
  101. ShowSeqList(L3);
  102. return 1;
  103. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/381591
推荐阅读
相关标签
  

闽ICP备14008679号