1,可扩容的顺序表
.h文件
#pragma once
#define SIZE 10//初始大小
#define MULTIPLE 2//扩容的倍数
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype* data;
Elemtype capacity;
Elemtype cursize;
}Seqlist;
void Init(Seqlist* plist);
int Getsize(Seqlist* plist);
void Printf(Seqlist* plist);
bool Isfull(Seqlist* plist);
bool Inc_capacity(Seqlist* plist);//扩容函数
bool Insert_pos(Seqlist* plist, Elemtype val, int pos);
bool Insert_front(Seqlist* plist, int val);
bool Insert_back(Seqlist* plist, int val);
bool Del_pos(Seqlist* plist, int pos);
bool Del_front(Seqlist* plist);
bool Del_back(Seqlist* plist);
int Find_pos(Seqlist* plist, int val);
bool Del_val(Seqlist* plist, int val);
bool Del_allsame(Seqlist* plist, int val);//删除相同值;
void Clear(Seqlist* plist);
void Destroy(Seqlist* plist);
.cpp文件
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Seqlist.h"
void Init(Seqlist* plist)
{
plist->capacity = SIZE;
plist->data = (Elemtype*)malloc(sizeof(Elemtype) * plist->capacity);
plist->cursize = 0;
printf("初始化成功\n");
}
int Getsize(Seqlist* plist)
{
assert(plist != nullptr);
return plist->cursize;
}
void Printf(Seqlist* plist)
{
assert(plist != nullptr);
printf("capacity:%d\ncursize:%d\n", plist->capacity, plist->cursize);
for (int i = 0; i < Getsize(plist); i++)
{
printf("%d ", plist->data[i]);
}
printf("\n");
}
bool Isfull(Seqlist* plist)
{
assert(plist != nullptr);
return plist->capacity == plist->cursize;
}
bool Inc_capacity(Seqlist* plist)//扩容函数
{
assert(plist != nullptr);
int total = plist->capacity * MULTIPLE;
/*Elemtype* newdata = (Elemtype*)malloc(sizeof(Elemtype) * total);
if (newdata == nullptr)
{
return false;
}
memmove(newdata, plist->data, sizeof(Seqlist) * total);
free(plist->data);
plist->data = nullptr;
plist->data = newdata;
plist->capacity = total;
return true;*/
Elemtype* newdata = (Elemtype*)realloc(plist->data, sizeof(Elemtype) * total);//用newdata而不用data是因为直接用data如果扩容失败data里的内容可能会丢失;
if (nullptr == newdata)
{
return false;
}
plist->data = newdata;
plist->capacity = total;
return true;
}
bool Insert_pos(Seqlist* plist, Elemtype val, int pos)
{
assert(plist != nullptr);
if (pos<0 || pos>plist->cursize) return false;
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
for (int i = plist->cursize; i > pos; --i)
{
plist->data[i] = plist->data[i - 1];
}
plist->data[pos] = val;
plist->cursize += 1;
return true;
}
bool Insert_front(Seqlist* plist, int val)
{
assert(plist != nullptr);
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
Insert_pos(plist, val, 0);
}
bool Insert_back(Seqlist* plist, int val)
{
assert(plist != nullptr);
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
plist->data[plist->cursize] = val;
plist->cursize++;
return true;
}
bool Del_pos(Seqlist* plist, int pos)
{
assert(plist != nullptr);
if (pos<0 || pos>plist->cursize - 1)
{
return false;
}
for (int i = pos; i < plist->cursize - 1; i++)
{
plist->data[i] = plist->data[i + 1];
}
plist->cursize -= 1;
return true;
}
bool Del_front(Seqlist* plist)
{
assert(plist != nullptr);
return Del_pos(plist, 0);
}
bool Del_back(Seqlist* plist)
{
assert(plist != nullptr);
return Del_pos(plist, plist->cursize - 1);
}
int Find_pos(Seqlist* plist, int val)
{
assert(plist != nullptr);
int pos = -1;
for (int i = 0; i < plist->cursize - 1; ++i)
{
if (plist->data[i] == val)
{
pos = i;
break;
}
}
return pos;
}
bool Del_val(Seqlist* plist, int val)
{
assert(plist != nullptr);
return Del_pos(plist, Find_pos(plist, val));
}
bool Del_allsame(Seqlist* plist, int val)//删除相同值;
{
assert(plist != nullptr);
/*int j = -1;
for (int i = 0; i < plist->cursize; i++)
{
if (plist->data[i] != val)
{
j += 1;
plist->data[j] = plist->data[i];
}
}
plist->cursize=j+1;*/
int j = 0;
for (int i = 0; i < plist->cursize; i++)
{
if (plist->data[i] != val)
{
plist->data[j] = plist->data[i];
j += 1;
}
}
plist->cursize = j;
return true;
}
void Clear(Seqlist* plist)
{
assert(plist != nullptr);
plist->cursize = 0;
}
void Destroy(Seqlist* plist)
{
assert(plist != nullptr);
plist->cursize = 0;
plist->capacity = 0;
free(plist->data);
}
int main()
{
Seqlist mylist;
Init(&mylist);
int ar[] = { 12,23,34,12,33,26,12 };
int n = sizeof(ar) / sizeof(ar[0]);
for (int i = 0; i < n; i++)
{
Insert_back(&mylist, ar[i]);
}
Printf(&mylist);
Del_allsame(&mylist, 12);
Printf(&mylist);
Destroy(&mylist);
return 0;
2,将两个顺序表合并
a,b两个顺序表都递增将其合并到c中也递增;
a,b两个顺序表都递增将其合并到a中也递增
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Seqlist.h"
bool Merge1(Seqlist* pa, Seqlist* pb,Seqlist* pc)
{
assert(pa != nullptr && pb != nullptr && pc != nullptr);
int capacity=Getsize(pa)+Getsize(pb);
pc->data = (Elemtype*)malloc(sizeof(Elemtype)*capacity);
if (pc->data == nullptr) return false;
pc->capacity = capacity;
pc->cursize =0;
Elemtype* a = pa->data;
Elemtype* b = pb->data;
Elemtype* c = pc->data;
Elemtype* pa_last = pa->data + pa->cursize - 1;
Elemtype* pb_last = pb->data + pb->cursize - 1;
while (a <= pa_last && b <= pb_last)
{
*c++ = *a < *b ? *a++ : *b++;
pc->cursize++;
}
while (a<=pa_last)
{
*c++ = *a++;
pc->cursize++;
}
while (b <= pb_last)
{
*c++ = *b++;
pc->cursize++;
}
return true;
}
bool Merge2(Seqlist* pa, Seqlist* pb)
{
assert(pa != nullptr && pb != nullptr);
int a = pa->cursize - 1;
int b = pb->cursize - 1;
if ((a + b > pa->cursize) && !Inc_capacity(pa))
{
return false;
}
int k = a+1 + b+1 - 1;
while (a >= 0 && b>= 0)
{
pa->data[k--] = pa->data[a] >= pb->data[b] ? pa->data[a--] : pb->data[b--];
}
while (b>=0)
{
pa->data[k--] = pb->data[b--];
}
pa->cursize = pa->cursize + pb->cursize;
return true;
}
int main()
{
int ar[] = {1,3,5,7,9,11,20};
int an = sizeof(ar) / sizeof(ar[0]);
int br[] = {2,4,8,10,11,15,19,55,66};
int bn = sizeof(br) / sizeof(br[0]);
Seqlist La, Lb, Lc;
Init(&La);
Init(&Lb);
for (int i = 0; i < an;i++)
{
Insert_back(&La,ar[i]);
}
for (int i = 0; i < bn; i++)
{
Insert_back(&Lb, br[i]);
}
Merge1(&La,&Lb,&Lc);
Printf(&La);
Printf(&Lb);
Printf(&Lc);
Destroy(&Lc);
Merge2(&La,&Lb);
Printf(&La);
return 0;
}