線性表代碼實現

如題;這是一套完整的可運行的代碼;需要讀者有一定的基礎去閱讀;

語言是用C語言實現;在C++環境中編寫;在C++中可直接運行;在C語言中需要改部分頭文件和輸出語句;

頭文件;這要是代碼的聲明部分;

# ifndef _SEQLIST_
# define _SEQLIST_

# include <iostream>
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;
}

 

發佈了36 篇原創文章 · 獲贊 0 · 訪問量 4221
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章