如题;这是一套完整的可运行的代码;需要读者有一定的基础去阅读;
语言是用C语言实现;在C++环境中编写;在C++中可直接运行;在C语言中需要改部分头文件和输出语句;
头文件;这要是代码的声明部分;
# ifndef _SEQLIST_
# define _SEQLIST_
# include using namespace std;
# define MAXSIZE 64
typedef int DataType;
typedef struct
{
DataType array[MAXSIZE];
int length;
}SeqList, * PSeqList;
PSeqList Init_SeqList(void);
int Length_SeqList(PSeqList p);
bool Empty_SeqList(PSeqList p);
bool Full_SeqList(PSeqList p);
int Search_SeqList(PSeqList p, DataType x);
int Insert_SeqList_Pos(PSeqList p, int pos, DataType x);
int Push_SeqList(PSeqList p, DataType x);
int Delete_SeqList_Pos(PSeqList p, int pos, int * val);
int Pop_SeqList(PSeqList p, int * val);
void Traversal_SeqList(PSeqList p);
void Inversion_SeqList(PSeqList p);
void Bubble_Sort_SeqList(PSeqList p);
void Select_Sort_SeqList(PSeqList p);
#endif
实现文件;主要是代码的实现;
# include "SeqList.h"
PSeqList Init_SeqList(void)
{
PSeqList p = (PSeqList)malloc(sizeof(SeqList));
if (NULL != p)
{
p->length = 0;
return p;
}
else
{
cout << "Memory allocate is error! " << endl;
system("pause");
exit(0);
}
}
int Length_SeqList(PSeqList p)
{
return p->length;
}
bool Empty_SeqList(PSeqList p)
{
if (0 == Length_SeqList(p))
{
return true;
}
else
{
return false;
}
}
bool Full_SeqList(PSeqList p)
{
if (p->length >= MAXSIZE)
{
return true;
}
else
{
return false;
}
}
int Search_SeqList(PSeqList p, DataType x)
{
int i = 0;
while ((i < p->length) && (p->array[i] != x))
{
i++;
}
if (i >= p->length)
{
return -1;
}
else
{
return (i + 1);
}
}
int Insert_SeqList_Pos(PSeqList p, int pos, DataType x)
{
if (Full_SeqList(p))
{
cout << "SeqList is full! " << endl;
return -1;
}
if ((pos < 1) || (pos > p->length + 1))
{
cout << "Insert position is not legal! " << endl;
return -2;
}
for (int i = p->length - 1; i >= pos - 1; i--)
{
p->array[i + 1] = p->array[i];
}
p->array[pos - 1] = x;
p->length++;
return 0;
}
int Push_SeqList(PSeqList p, DataType x)
{
if (Full_SeqList(p))
{
cout << "SeqList is full! " << endl;
return -1;
}
p->array[p->length] = x;
p->length++;
return 0;
}
int Delete_SeqList_Pos(PSeqList p, int pos, int * val)
{
if (Empty_SeqList(p))
{
cout << "SeqList is empty! " << endl;
return -1;
}
if ((pos < 1) || (pos >p->length))
{
cout << "Delete position is not leagl! " << endl;
return -2;
}
*val = p->array[pos - 1];
for (int i = pos - 1; i < p->length - 1; i++)
{
p->array[i] = p->array[i + 1];
}
p->length--;
return 0;
}
int Pop_SeqList(PSeqList p, int * val)
{
if (Empty_SeqList(p))
{
cout << "SeqList is empty! " << endl;
return -2;
}
*val = p->array[p->length - 1];
p->length--;
return 0;
}
void Traversal_SeqList(PSeqList p)
{
for (int i = 0; i < p->length; i++)
{
cout << p->array[i] << " ";
}
cout << endl;
return;
}
void Inversion_SeqList(PSeqList p)
{
int tem = 0;
int i = 0;
int j = p->length - 1;
while (i < j)
{
tem = p->array[i];
p->array[i] = p->array[j];
p->array[j] = tem;
i++;
j--;
}
return;
}
void Bubble_Sort_SeqList(PSeqList p)
{
int tem = 0;
int len = p->length;
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (p->array[j] > p->array[j + 1])
{
tem = p->array[j];
p->array[j] = p->array[j + 1];
p->array[j + 1] = tem;
}
}
}
return;
}
void Select_Sort_SeqList(PSeqList p)
{
int k = 0;
int tem = 0;
int len = p->length;
for (int i = 0; i < len - 1; i++)
{
k = i;
for (int j = i + 1; j < len; j++)
{
if (p->array[k] < p->array[j])
{
k = j;
}
}
if (k != i)
{
tem = p->array[k];
p->array[k] = p->array[i];
p->array[i] = tem;
}
}
return;
}
Main函数;
# include "SeqList.h"
int main(int argc, char ** argv)
{
int val = 0;
PSeqList p = Init_SeqList();
for (int i = 0; i < 10; i++)
{
Push_SeqList(p, rand() % 1000);
}
Traversal_SeqList(p);
Select_Sort_SeqList(p);
Traversal_SeqList(p);
system("pause");
return 0;
}