シーヶンシャルラインテーブルソース
3110 ワード
シーケンス線形テーブルソース(C++)
//
//Description:
//
#include <iostream>
#include <malloc.h>
using namespace std;
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 10
typedef struct{
int * elem;
int length;
int listsize;
}SqList;
//--------------------- --------------------------
void input(SqList &L);
void output(SqList &L);
int InitList_Sq(SqList &L);
int ListInsert_Sq(SqList &L, int i, int e);
int ListDelete_Sq(SqList &L, int i, int &e);
//---------------------- -------------------------
int *p=NULL, *q=NULL;
//------------------------- -----------------------------
void main()
{
cout << "======================== =================" << endl;
cout << "************** ******************" << endl;
cout << "
~~~~~~~ ~~~~~~~~
";
SqList L;
InitList_Sq(L);
input(L);
output(L);
cout << "
~~~~~~~ ~~~~~~~~
";
cout << " " << endl;
int i, e;
cin >> i;
cout << " " << endl;
cin >> e;
ListInsert_Sq(L, i, e);
output(L);
cout << "
~~~~~~~ ~~~~~~~~
";
cout << " " << endl;
cin >> i;
int d;//d
ListDelete_Sq(L, i, d);
output(L);
cout << " \t" << i << ", \t" << d << endl;
}
int InitList_Sq(SqList &L)
{
L.elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if (!L.elem)return 0;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return 1;
}
void input(SqList &L)
{
int i = 0;
for (i = 0; i<L.listsize; i++)
{
cout << " " << i << " :" << endl;
cin >> L.elem[i];
}
L.length = i;
}
void output(SqList &L)
{
cout << "
:
" << endl;
for (int j = 0; j<L.length; j++)
{
cout << " " << j << " :" << L.elem[j] << "\t";
}
cout << endl;
cout << " " << L.listsize << "\t" << " " << L.length << endl;//---------------------
}
//-------------------------- ----------------------------
int ListInsert_Sq(SqList &L, int i, int e)
{
if (i<1 || i>L.length + 1)
{
return 0;
}
if (L.length >= L.listsize)
{
int * newbase = (int *)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(int));
if (!newbase)
{
return 0;
}
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = &(L.elem[i - 1]);
for (p = &(L.elem[L.length - 1]); p >= q; --p)
{
*(p + 1) = *p;
}
*q = e;
++L.length;
return 1;
}
//------------------------ ------------------------------
int ListDelete_Sq(SqList &L, int i, int &e)
{
if ((i<1) || (i>L.length))
return 0;
p = &(L.elem[i - 1]);
e = *p;
q = L.elem + L.length - 1;
for (++p; p <= q; ++p)
*(p - 1) = *p;
--L.length;
return 1;
}