⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.25.cpp

📁 严蔚敏数据结构习题C++实现
💻 CPP
字号:
#include <iostream>
using namespace std;
#define INIT_SIZE 10
typedef int ElemType;
typedef struct
{
    ElemType* array;
    int ListSize;
    int Length;
}Sqlist;
void init_list(Sqlist& L);
bool insert_list(Sqlist& L,const ElemType item);
void display_list(const Sqlist L);
void qiu_jiao(const Sqlist A,const Sqlist B,Sqlist& C);
int main()
{
    int n;
    Sqlist A,B,C;
    init_list(A);
    init_list(B);
    init_list(C);
    for(int i=0; i<=9; i++)
    {
        cin >> n;
        insert_list(A,n);
    }
    for(int i=0; i<=9; i++)
    {
        cin >> n;
        insert_list(B,n);
    }
    qiu_jiao(A,B,C);
    display_list(C);
    system("pause");
    return 0;
}
void init_list(Sqlist& L)
{
    L.ListSize = INIT_SIZE;
    L.Length = 0;
    L.array = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
}
bool insert_list(Sqlist& L,const ElemType item)
{
     int temp = 0;
     if(L.Length == L.ListSize)
         return false;
     for(; temp<=L.Length-1; temp++)
         if(item <= L.array[temp])
             break;
     for(int i=L.Length-1; i>=temp; i--)
         L.array[i+1] = L.array[i];
     L.array[temp] = item;
     L.Length++;
     return true;
}
void display_list(const Sqlist L)
{
     if(L.Length == 0)
         ;
     for(int i=0; i<=L.Length-1; i++)
         cout << L.array[i] << ' ';
}
void qiu_jiao(const Sqlist A,const Sqlist B,Sqlist& C)
{
     for(int i=0; i<=A.Length-1;)
         for(int j=0; j<=B.Length-1;)
         {
             if(A.array[i] == B.array[j])
             {
                 insert_list(C,A.array[i]);
                 i++;
                 j++;
             }
             if(A.array[i] > B.array[j])
                 j++; 
             if(A.array[i] < B.array[j])
                 i++;
         }
}
      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -