线性表c语言头文件,线性表代码实现

如题;这是一套完整的可运行的代码;需要读者有一定的基础去阅读;

语言是用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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值