-
什么是线性结构?
结构中的数据元素之间存在一个对一个的关系. -
线性结构中都包含什么内容?
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结
构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物
理上存储时,通常以数组和链式结构的形式存储。 -
什么是顺序表?顺序表的分类?
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组
上完成数据的增删查改.
顺序表增删查改
#pragma once
typedef int DateType;
typedef struct SeqList {
DateType* _array;
int _capacity;
int _size;
}SeqList, *PSeq;
void SeqListInit(PSeq ps, int capacity);
void SeqListPushBack(PSeq ps, DateType date);
void SeqListPopBack(PSeq ps);
void SeqListPushFront(PSeq ps, DateType date);
void SeqListPopFront(PSeq ps);
void SeqListInsert(PSeq ps, int pos, DateType date);
void SeqListErese(PSeq ps, int pos);
int SeqListFind(PSeq ps, DateType date);
int SeqListEmpty(PSeq ps);
int SeqListSize(PSeq ps);
int SeqListCapacity(PSeq ps);
void SeqlistClear(PSeq ps);
void SeqlistRemove(PSeq ps, DateType date);
void SeqListDestroy(PSeq ps);
void CheckCapacity(PSeq ps);
void TestSeqlist();
#include<stdio.h>
#include<stdlib.h>
#include "Seqlist.h"
#include<malloc.h>
#include<assert.h>
void SeqListInit(PSeq ps, int capacity) {
ps->_array = (DateType*)malloc(sizeof(DateType)*capacity);
if (NULL == ps->_array) {
assert(0);
return;
}
ps->_capacity = capacity;
ps->_size = 0;
}
void SeqListDestroy(PSeq ps) {
if (ps->_array) {
free(ps->_array);
ps->_array = NULL;
ps->_capacity = 0;
ps->_size = 0;
}
}
void SeqListPushBack(PSeq ps, DateType date) {
//assert(ps);
顺序表满
//CheckCapacity(ps);
//ps->_array[ps->_size] = date;
//ps->_size++;
SeqListInsert(ps, ps->_size, date);
}
void SeqListPopBack(PSeq ps) {
/*assert(ps);
if (SeqListEmpty(ps)) {
return;
}
ps->_size--;*/
SeqListErese(ps, ps->_size - 1);
}
void SeqListPushFront(PSeq ps, DateType date) {
//assert(ps);
CheckCapacity(ps);
//for (int i = ps->_size-1;i >= 0;i--) {
// ps->_array[i + 1] = ps->_array[i];
//}
//ps->_array[0] = date;
//ps->_size++;
SeqListInsert(ps, 0, date);
}
void SeqListPopFront(PSeq ps) {
/*if (SeqListEmpty(ps)) {
return;
}
for (int i = 1;i < ps->_size;i++) {
ps->_array[i - 1] = ps->_array[i];
}
ps->_size--;*/
SeqListErese(ps, 0);
}
void SeqListInsert(PSeq ps, int pos, DateType date) {
assert(ps);
if (pos<0 || pos>ps->_size) {
return;
}
CheckCapacity(ps);
for (int i = ps->_size - 1;i >= pos;i--) {
ps->_array[i + 1] = ps->_array[i];
}
ps->_array[pos] = date;
ps->_size++;
}
void SeqListErese(PSeq ps, int pos) {
assert(ps);
if (pos < 0 || pos >= ps->_size) {
return;
}
for (int i = pos + 1;i < ps->_size;++i) {
ps->_array[i - 1] = ps->_array[i];
}
ps->_size--;
}
int SeqListFind(PSeq ps, DateType date) {
assert(ps);
for (int i = 0;i < ps->_size;i++) {
if (ps->_array[i] == date) {
return i;
}
}
return -1;
}
int SeqListSize(PSeq ps) {
assert(ps);
return ps->_size;
}
int SeqListCapacity(PSeq ps) {
assert(ps);
return ps->_capacity;
}
int SeqListEmpty(PSeq ps) {
assert(ps);
return 0 == ps->_size;
}
void SeqlistClear(PSeq ps) {
assert(ps);
ps->_size = 0;
}
void SeqlistRemove(PSeq ps, DateType date) {
SeqListErese(ps, SeqListFind(ps, date));
}
void CheckCapacity(PSeq ps) {
assert(ps);
if (ps->_size == ps->_capacity) {
//顺序表中已经没有空间
//申请新空间
int newCapacity = ps->_capacity * 2;
int*pTemp = (DateType*)malloc(newCapacity * sizeof(DateType));
if (NULL == pTemp) {
assert(0);
return;
}
//拷贝元素
for (int i = 0;i < ps->_size;i++) {
pTemp[i] = ps->_array[i];
}
//释放旧空间
free(ps->_array);
//更新
ps->_array = pTemp;
ps->_capacity = newCapacity;
}
}
void SeqListPrint(PSeq ps) {
for (int i = 0;i < ps->_size;i++) {
printf("%d", ps->_array[i]);
}
printf("\n");
}
void TestSeqlist() {
SeqList S;
int pos = -1;
SeqListInit(&S, 10);
SeqListPushBack(&S, 1);
SeqListPushBack(&S, 2);
SeqListPushBack(&S, 3);
SeqListPushBack(&S, 4);
SeqListPushBack(&S, 5);
SeqListPrint(&S);
SeqListPopBack(&S);
SeqListPrint(&S);
SeqListPushFront(&S, 0);
SeqListPrint(&S);
SeqListPopFront(&S);
SeqListPrint(&S);
SeqListInsert(&S, 1, 5);
SeqListPrint(&S);
pos = SeqListFind(&S, 5);
if (pos != -1) {
printf("5 si in %d\n", pos);
}
else {
printf("5 is not in %d\n",pos);
}
SeqListPrint(&S);
SeqListErese(&S, 1);
pos = SeqListFind(&S, 5);
if (pos != -1) {
printf("5 si in %d\n", pos);
}
else {
printf("5 is not in %d\n", pos);
}
SeqListPrint(&S);
printf("size=%d\n", SeqListSize(&S));
printf("capacity=%d\n", SeqListCapacity(&S));
SeqListDestroy(&S);
}
int main() {
TestSeqlist();
system("pause");
return 0;
}