1.順序結構
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType *elem;//存儲空間的基地址
int length;//多項式中當前項的個數
}SqList;
//初始化
void InitList(SqList &L){
L.elem = new ElemType[MAXSIZE];
L.length = 0;
return;
}
//取值
void GetElem(SqList L, int i, ElemType &e){
e = L.elem[i - 1];//elem[1-1]單元存儲第i個數據元素
return;
}
//查找
bool LocateElem(SqList L, ElemType e){
for (int i = 0; i < L.length; i++)
if (e == L.elem[i])return true;
return false;
}
//插入
void InsertList(SqList &L, int i, ElemType e){
//超出表+1長(插入完表長會+1),存儲空間滿,曾出現過,都直接返回
if (i<1 || i>(L.length + 1) || (L.length == MAXSIZE)||LocateElem(L,e))return;
for (int j = L.length - 1; j >= i - 1; j--)//從最後一項開始往後挪一位,直到第i個元素(第i-1項),給插入到第i個元素(第i-1項)騰位置
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;//插入
++L.length;//表長+1
return;
}
//輸出順序表
void WatchList(SqList L){
if (L.length == 0){
printf("表空\n");
return;
}
printf("順序表:");
for (int i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
return;
}
int main(){
SqList A, B;
InitList(A); InitList(B);
int n, m, e;
cout << "A:";
cin >> n;
for (int i = 1; i <= n; i++){
cin >> e;
InsertList(A, i, e);
}
cout << "B:";
cin >> m;
for (int i = 1; i <= m; i++){
cin >> e;
InsertList(B, i, e);
}
for (int i = A.length + 1, j = 1; i <= A.length + B.length&&j <= B.length; j++){
GetElem(B, j, e);
if (LocateElem(A, e))
continue;
InsertList(A, i, e);
i++;
}
WatchList(A);
system("PAUSE");
return 0;
}
1.0結構體
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType *elem;//存儲空間的基地址
int length;//多項式中當前項的個數
}SqList;
1.1初始化
void InitList(SqList &L){
L.elem = new ElemType[MAXSIZE];
L.length = 0;
return;
}
1.2取值
void GetElem(SqList L, int i, ElemType &e){
e = L.elem[i - 1];//elem[1-1]單元存儲第i個數據元素
return;
}
1.3查找
bool LocateElem(SqList L, ElemType e){
for (int i = 0; i < L.length; i++)
if (e == L.elem[i])return true;
return false;
}
1.4插入
void InsertList(SqList &L, int i, ElemType e){
//超出表+1長(插入完表長會+1),存儲空間滿,曾出現過,都直接返回
if (i<1 || i>(L.length + 1) || (L.length == MAXSIZE)||LocateElem(L,e))return;
for (int j = L.length - 1; j >= i - 1; j--)//從最後一項開始往後挪一位,直到第i個元素(第i-1項),給插入到第i個元素(第i-1項)騰位置
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;//插入
++L.length;//表長+1
return;
}
1.5輸出
void WatchList(SqList L){
if (L.length == 0){
printf("表空\n");
return;
}
printf("順序表:");
for (int i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
return;
}
2.鏈式結構(無頭結點的單鏈表)
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 100
#define ERROR 9999999
typedef int ElemType;//數據域類型(一個類型或一個結構體)
typedef struct LNode* LinkList;
typedef struct LNode{
ElemType data;//數據域
LinkList next;//指針域
}LNode;
//初始化
void InitList(LinkList &L){
L = new LNode;
L->next = NULL;
return;
}
//遞歸插入
void InsertList(LinkList &L, ElemType e){
if (!L){
InitList(L);
L->data = e;
}
else if (L->data!=e)
InsertList(L->next, e);
return;
}
//查找
int LocateList(LinkList L, int i,int length){
if (i < 1 || i>length)return ERROR;
else
while (--i){
L = L->next;
}
return L->data;
}
int main(){
LinkList A=NULL, B=NULL;
int n,m;
ElemType e;
cout << "A:";
cin >> n;
while (n--){
cin >> e;
InsertList(A, e);
}
cout << "B:";
cin >> n;
m = n;
while (n--){
cin >> e;
InsertList(B, e);
}
for (int i = 1; i <= m; i++){
e = LocateList(B, i,m);
if (e!=ERROR)
InsertList(A, e);
}
printf("合併:");
while (A!=NULL){
printf("%d ", A->data);
A = A->next;
}
system("PAUSE");
return 0;
}
2.0結構體
#define MAXSIZE 100
#define ERROR 9999999
typedef int ElemType;//數據域類型(一個類型或一個結構體)
typedef struct LNode* LinkList;
typedef struct LNode{
ElemType data;//數據域
LinkList next;//指針域
}LNode;
2.1初始化
//初始化
void InitList(LinkList &L){
L = new LNode;
L->next = NULL;
return;
}
2.2插入(遞歸)
void InsertList(LinkList &L, ElemType e){
if (!L){
InitList(L);
L->data = e;
}
else if (L->data!=e)
InsertList(L->next, e);
return;
}
2.3查找
int LocateList(LinkList L, int i,int length){
if (i < 1 || i>length)return ERROR;
else
while (--i){
L = L->next;
}
return L->data;
}
3.有序表
1.可有相同元素,所以去掉插入和合並時查找相同元素若有則跳過的條件
2.有序
2.1順序表加入快排
例如
bool comp(ElemType a,ElemType b){
return a < b;
}
sort(A.elem, A.elem+ A.length, comp);
2.2鏈表
在插入的時候有序插入,因爲他不是順序的結構,可以在插入的時候就比較大小,若不符合排序順序則插入元素和當前元素的值(不是地址)互換再插入