[数据结构严蔚敏]线性表-算法2.1

/*algorithm-2.1.c*/  1 #include<stdio.h>
  2 #include<linear_list_ADT.h>
  3
  4 int main(void){
  5     List LA,LB;
  6     int LA_len,LB_len;
  7     InitList(&LA);
  8     InitList(&LB);
  9 /*********get LIST_A***********/
 10     printf("please input length of LA:");
 11     scanf("%d",&LA_len);
 12     for(int i=0;i<LA_len;i++){
 13         printf("please input No%d Element in LA:", i+1);
 14         ElemType Element;
 15         scanf("%d",&Element);
 16         ListInsert(&LA,i+1,Element);
 17     }
 18     printf("LA:");
 19     for (int i = 0; i < LA_len; i++)
 20     {
 21         ElemType Element;
 22         GetElem(&LA,i+1,&Element);
 23         printf(" %d", Element);
 24     }
 25 printf("\n");
 26 /*********get LIST_A***********/
 27 /*********get LIST_B***********/
 28     printf("please input length of LB:");
 29     scanf("%d",&LB_len);
 30     for(int i=0;i<LB_len;i++){
 31         printf("please input No%d Element in LB:", i+1);
 32         ElemType Element;
 33         scanf("%d",&Element);
 34         ListInsert(&LB,i+1,Element);
 35     }
 36     printf("LB:");
 37     for (int i = 0; i < LB_len; i++)
 38     {
 39         ElemType Element;
 40         GetElem(&LB,i+1,&Element);
 41         printf(" %d", Element);
 42     }
 43     printf("\n");
 44 /*********get LIST_B***********/
 45
 46 /*********algorithm-2.1***********/
 47     int LC_len=LA_len+LB_len,i=0,j=0,k=1;
 48     List LC;
 49     ElemType Element_A,Element_B;
 50     InitList(&LC);
 51
 52     while((i < LA_len) && (j < LB_len))
 53     {
 54         GetElem(&LA,i+1,&Element_A);
 55         GetElem(&LB,j+1,&Element_B);
 56         if(Element_A >=     Element_B){
 57             printf("%d vs %d\n", Element_A,Element_B);
 58             ListInsert(&LC,k,Element_A);
 59             k++;
 60             i++;
 61         }
 62         else{
 63             printf("%d vs %d\n", Element_A,Element_B);
 64             ListInsert(&LC,k,Element_B);
 65             k++;
 66             j++;
 67         }
 68     }
 69 printf("i=%d\n", i);
 70 printf("j=%d\n", j);
 71 printf("k=%d\n", k);
 72     if(i<=j){
 73         while(k<=LC_len){
 74             GetElem(&LA,i+1,&Element_A);
 75             ListInsert(&LC,k,Element_A);
 76             i++;
 77             k++;
 78         }
 79     }
 80     else{
 81         while(k<=LC_len){
 82             GetElem(&LB,j+1,&Element_B);
 83             ListInsert(&LC,k,Element_B);
 84             j++;
 85             k++;
 86         }
 87     }
 88
 89 /*********algorithm-2.1***********/
 90
 91 /***********result***************/
 92    printf("LC:");
 93     for (int i = 0; i < LC_len; i++)
 94     {
 95         ElemType Element_C;
 96         GetElem(&LC,i+1,&Element_C);
 97         printf(" %d", Element_C);
 98     }
 99     printf("\n");
100 /***********result***************/
101     return 0;
102 }
/*linear_list_ADT.h*/
  1 #include<malloc.h>
  2
  3 #define LIST_INIT_SIZE 100
  4 #define SUCCES 1
  5 #define FAILED 0
  6 #define ERROR -1
  7
  8 typedef int ElemType;
  9 typedef int Status;
 10
 11 typedef struct{
 12     ElemType *data;
 13     int length;}List;
 14
 15 Status InitList(List *L)
 16 {
 17   L->data = (ElemType *)(malloc(sizeof(ElemType)*LIST_INIT_SIZE));
 18   L->length = 0;
 19   return SUCCES;
 20 }
 21
 22 Status InitList_with_Len(List *L,int len)
 23 {
 24   L->data = (ElemType *)(malloc(sizeof(ElemType)*len));
 25   L->length = len;
 26   return SUCCES;
 27 }
 28
 29 Status DestroyList(List *L)
 30 {
 31   free(L->data);
 32   L->length = 0;
 33   return SUCCES;
 34 }
 35
 36 Status ClearList(List *L)
 37 {
 38
 39   L->length = 0;
 40   return SUCCES;
 41 }
 42
 43 Status ListEmpty(List L)
 44 {
 45   if(L.length==0){
 46     return SUCCES;}
 47   else{
 48     return FAILED;}
 49 }
 50
 51 int ListLength(List L)
 52 {
 53     //L中存的是线性表的地址
 54     /*
 55     int length  = 0;
 56     while()
 57     {
 58         length++;
 59     }
 60     */
 61     //return *L->length是肯定不对的。。。否则直接在调用时这样简单做就好了,干嘛还非要写一个函数呢
 62     return L.length;
 63 }
 64
 65 //返回第i个元素
 66 Status GetElem(List *L,int i,ElemType *e)//e里面存放着参数地址
 67 {
 68   if(i>=1 && i<=L->length){
 69     *e = L->data[i-1];
 70     return SUCCES;}
 71   else{
 72     return FAILED;}
 73 }
 74
 75 Status LocateElem(List *L,ElemType e,int *i)
 76 {
 77   for(int j=0;j<L->length;j++)
 78   {
 79     if(L->data[j] == e){
 80       *i=j;
 81       break;}
 82   }
 83   if(i!=0){
 84     return SUCCES;}
 85   else{
 86     return FAILED;}
 87 }
 88
 89 Status PriorElem(List *L,ElemType cur_e,ElemType *pre_e)
 90 {
 91   if(L->data[0]==cur_e){
 92       return FAILED;
 93   }
 94   else{
 95   for(int i=1;i<L->length;i++){
 96     if(L->data[i]==cur_e){
 97     *pre_e = L->data[i-1];
 98     break;
 99     }
100   }
101   return SUCCES;
102   }
103 }
104
105 Status NextElem(List *L,ElemType cur_e,ElemType *next_e)
106 {
107   if(L->data[L->length-1]==cur_e){
108     return FAILED;}
109   else{
110     for(int i=0;i<L->length-1;i++){
111       if(L->data[i]==cur_e){
112         *next_e = L->data[i+1];
113         break;
114       }
115     }
116     return SUCCES;
117   }
118 }
119
120 //第i个位置上插入新的元素
121 Status ListInsert(List *L,int i,ElemType e)
122 {
123     if(i>=1 && i<=L->length+1){
124         if(i==L->length+1){
125                 L->data[i-1]=e;
126                 ++L->length;
127         }
128         else{
129                 for(int j=i-1;j<L->length;j++)
130                 {
131                     L->data[j+1]=L->data[j];
132                 }
133                 L->data[i-1]=e;
134                 ++L->length;
135         }
136         return SUCCES;
137     }
138     else{
139         return FAILED;
140     }
141 }
142
143 //删除第i个元素
144 Status ListDelete(List *L,int i,ElemType *e)
145 {
146   if(i>=0 && i<L->length){
147     *e = L->data[i-1];
148     for(int j=i;j<L->length;j++){
149       L->data[i-1]=L->data[i];
150     }
151     free(&L->data[i]);
152     L->length--;
153     return SUCCES;
154   }
155   else{
156     return FAILED;
157   }
158 }
时间: 2024-08-05 02:36:34

[数据结构严蔚敏]线性表-算法2.1的相关文章

基于c语言数据结构+严蔚敏——线性表章节源码,利用Codeblocks编译通过

白天没屌事,那我们就来玩玩线性表的实现吧,快要失业了,没饭吃了咋整哦 题目描述假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B.这就要求对线性表做如下操作:扩大线性表LA,将存在于线性表LB中而不存在于线性表LA中的数据元素插入到线性表LA中去.只要从线性表LB中依次取得每个元素,并依值在线性表LA中进行查访,若不存在,则插入之. #ifndef FUNC_H_INCLUDED #define FUNC_H_INCLUDE

数据结构学习笔记——线性表的应用

数据结构学习笔记——线性表的应用 线性表的应用 线性表的自然连接 计算任意两个表的简单自然连接过程讨论线性表的应用.假设有两个表A和B,分别是m1行.n1列和m2行.n2列,它们简单自然连接结果C=A*B(i==j),其中i表示表A中列号,j表示表B中的列号,C为A和B的笛卡儿积中满足指定连接条件的所有记录组,该连接条件为表A的第i列与表B的第j列相等. 如:         1 2 3                3 5 A  =  2 3 3         B =  1 6       

数据结构学习笔记——线性表

第2章  线性表 2.1  线性表的类型定义  线性结构的特点是:在数据元素的非空有限集中,(1)存在唯一的一个被称做“第一个”的数据元素:(2)存在唯一的一个被称做“最后一个”的数据元素:(3)除第一个之外,集合中的每个数据元素只有一个前驱:(4)除最后一个外,集合中每个数据元素均只有一个后继. 线性表的类型定义 线性表(linear_list)是最常用的且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成.在这种情况下

线性表算法-插入

线性表算法-插入

线性表算法-删除

线性表算法-删除

线性表算法-合并

线性表算法-合并

python数据结构之一:线性表

线性表:零个或多个数据元素的有限序列. 咋一看这句话,我也不明白是什么意思,哈哈,举例说明一下吧.去电影院买票时,人们的排队就是一个线性表,有固定的最前一个,固定的最后一个. 张三是其中一个,他的前一个人,和后一个人是确定的单一的. 再如,一个公司里的一个部里有一个领导,多名员工,他们的关系就不是线性表了,有一对多的关系. 那么在python里如何创建线性表呢?如下: L1=["a","b","c","d","e&q

【算法与数据结构实战】线性表操作-实现A并B,结果放入A中

//数据结构与算法基础题1:线性表操作,实现A并B,结果放入A中 #include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <algorithm> #pragma warning(disable:4996) using namespace std; int main() { vector<int> set_A, se

数据结构和算法 (二)数据结构基础、线性表、栈和队列、数组和字符串

Java面试宝典之数据结构基础 —— 线性表篇 一.数据结构概念 用我的理解,数据结构包含数据和结构,通俗一点就是将数据按照一定的结构组合起来,不同的组合方式会有不同的效率,使用不同的场景,如此而已.比 如我们最常用的数组,就是一种数据结构,有独特的承载数据的方式,按顺序排列,其特点就是你可以根据下标快速查找元素,但是因为在数组中插入和删除元素会 有其它元素较大幅度的便宜,所以会带来较多的消耗,所以因为这种特点,使得数组适合:查询比较频繁,增.删比较少的情况,这就是数据结构的概念.数据结构 包括