201 SqList
注意:拡張子の名前に従ってファイルを新規作成します.
hを押してファイルを作成したら、後で簡単に名前を変更します.cppファイルはコンパイルが間違っています.
シーケンステーブルの実装には4つのファイルが含まれています.
c 1.hは前処理コマンドである.
c 2-1.hはSqListのデータ構造である.
bo 2-1.ppはSqListの基本的な操作関数です.
main 2-1.cpはテスト関数です.
hを押してファイルを作成したら、後で簡単に名前を変更します.cppファイルはコンパイルが間違っています.
シーケンステーブルの実装には4つのファイルが含まれています.
c 1.hは前処理コマンドである.
c 2-1.hはSqListのデータ構造である.
bo 2-1.ppはSqListの基本的な操作関数です.
main 2-1.cpはテスト関数です.
//c1.h
#include<iostream>
#include<process.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status;
//c2-1.h
#ifndef C2_1_H
#define C2_1_H
typedef int ElemType;
#define INIT_SIZE 10
#define INCREMENT 2
struct SqList
{
ElemType *elem;
int length;
int listsize;
};
void InitList(SqList &L);
int ListLength(SqList L);
Status ListEmpty(SqList L);
void ListTraverse(SqList L);
Status ListInsert(SqList &L, int index, ElemType e);
Status ListDelete(SqList &L, int index, ElemType &e);
void ClearList(SqList &L);
Status GetElem(SqList L, int index, ElemType &e);
int LocateElem(SqList L, ElemType e);
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e);
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e);
void DestroyList(SqList &L);
#endif
//bo2-1.cpp
#include "c1.h"
#include "c2-1.h"
using namespace std;
void InitList(SqList &L)
{
if (!(L.elem = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType))))
exit(OVERFLOW);
L.length = 0;
L.listsize = INIT_SIZE;
}
int ListLength(SqList L)
{
return L.length;
}
Status ListEmpty(SqList L)
{
if (L.length == 0)
return OK;
else
return ERROR;
}
void ListTraverse(SqList L)
{
for (int i = 0; i < L.length; i++)
{
if (i % 10 == 0 && i != 0)
cout << endl;
cout << L.elem[i] << " ";
}
cout << endl;
}
Status ListInsert(SqList &L, int index, ElemType e)
{
if (index >= 1 && index <= L.length + 1)
{
if (L.listsize == L.length)
{
if (!(L.elem = (ElemType *)realloc(L.elem, (L.length + INCREMENT)*sizeof(ElemType))))
exit(OVERFLOW);
L.listsize = L.length + INCREMENT;
}
//insert by index
/*
if (index == L.length + 1)
{
L.elem[index - 1] = e;
}
else
{
for (int i = L.length - 1; i >= index - 1; i--)
{
L.elem[i + 1] = L.elem[i];
}
L.elem[index - 1] = e;
}
*/
//insert by pointer
ElemType *p, *q;
p = L.elem + index - 1;
for (q = L.elem + L.length - 1; q >= p; q--)
*(q + 1) = *q;
*p = e;
L.length++;
return OK;
}
else
return ERROR;
}
Status ListDelete(SqList &L, int index, ElemType &e)
{
if (index >= 1 && index <= L.length)
{
ElemType *p, *q;
p = L.elem + index - 1;
e = *p;
q = L.elem + L.length - 1;
while (p < q)
{
*p = *(p + 1);
p++;
}
L.length--;
return OK;
}
else
return ERROR;
}
void ClearList(SqList &L)
{
L.length = 0;
}
Status GetElem(SqList L, int index, ElemType &e)
{
if (index < 1 || index > L.length)
return ERROR;
e = *(L.elem + index - 1);
return OK;
}
int LocateElem(SqList L, ElemType e)
{
if (L.length == 0)
return 0;
else
{
for (int i = 0; i < L.length; i++)
{
if (e == *(L.elem + i))
return i + 1;
}
return 0;
}
}
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
int location = LocateElem(L, cur_e);
if (location == -1 || location == 1)
return ERROR;
pre_e = *(L.elem + location - 2);
return OK;
}
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e)
{
int location = LocateElem(L, cur_e);
if (location == -1 || location == L.length)
return ERROR;
next_e = *(L.elem + location);
return OK;
}
void DestroyList(SqList &L)
{
free(L.elem);
L.elem = nullptr;
L.length = 0;
L.listsize = 0;
}
//main2-1.cpp
#include "c1.h"
#include "c2-1.h"
using namespace std;
int main()
{
SqList L;
InitList(L);
cout << "ListLength: " << ListLength(L) << endl;
cout << "ListEmpty: " << ListEmpty(L) << endl;
for (int i = 1; i <= 100; i++)
ListInsert(L, i, i);
cout << "ListLength: " << ListLength(L) << endl;
ListTraverse(L);
ElemType e;
ListDelete(L, 1, e);
cout << "ListLength: " << ListLength(L) << endl;
ListTraverse(L);
cout << "deleted element: " << e << endl;
GetElem(L, 33, e);
cout << "GetElem: " << e << endl;
cout << "LocateElem: " << LocateElem(L, 2) << endl;
PriorElem(L, 67, e);
cout << "PriorElem: " << e << endl;
NextElem(L, 67, e);
cout << "NextElem: " << e << endl;
ClearList(L);
ListTraverse(L);
cout << "ListLength: " << ListLength(L) << endl;
DestroyList(L);
cout << "ListLength: " << ListLength(L) << endl;
cout << "ListSize: " << L.listsize << endl;
cin.get();
return 0;
}