#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Elemtype;
typedef struct {
Elemtype* date;
int length;
int maxlength;
} Linear_List_t, *Linear_List_p;
Linear_List_p CreatList_p(int maxlength)
{
if (maxlength < 0)
return NULL;
Linear_List_p newList = (Linear_List_p)malloc(sizeof(Linear_List_t));
if (newList == NULL)
return NULL;
if (maxlength == 0) {
newList->maxlength = 0;
newList->length = 0;
newList->date = NULL;
return newList;
}
newList->date = (Elemtype*)malloc(sizeof(Elemtype) * maxlength);
if (newList->date == NULL) {
free(newList);
return NULL;
}
memset(newList->date, 0, maxlength * sizeof(Elemtype));
newList->length = 0;
newList->maxlength = maxlength;
return newList;
}
bool ExtendList(Linear_List_p list, const int ExtendLength)
{
if (ExtendLength < 0 || list == NULL)
return false;
if (ExtendLength == 0)
return true;
Elemtype* temp = list->date;
if ((list->date = (Elemtype*)malloc(sizeof(Elemtype) * (list->maxlength + ExtendLength))) == NULL) {
list->date = temp;
return false;
}
if (temp == NULL) {
list->maxlength += ExtendLength;
return true;
}
for (int i = 0; i < list->length; i++) {
list->date[i] = temp[i];
}
free(temp);
list->maxlength += ExtendLength;
return true;
}
bool ClearList(Linear_List_p listp)
{
if (listp == NULL || listp->date == NULL || listp->maxlength == 0)
return false;
memset(listp->date, 0, (listp->maxlength) * sizeof(Elemtype));
listp->length = 0;
return true;
}
bool IsEmptyList(Linear_List_p listp)
{
if (listp == NULL)
return -1;
return listp->length ? false : true;
}
int GetListLength(Linear_List_p listp)
{
if (listp == NULL)
return -1;
return listp->length;
}
bool DelList(Linear_List_p listp)
{
if (listp == NULL)
return true;
if (listp->date)
free(listp->date);
free(listp);
listp = NULL;
return true;
}
#define LISTFULL 1
#define INDEXERROR 2
#define INSERTSUCCESS 3
void RightMove(Elemtype* array, int length, int num)
{
Elemtype* temp = array + length;
while (num--) {
*temp = *(temp - length);
temp--;
}
}
int List_InsertElem(Linear_List_p l, const Elemtype* date, int num, int index, bool isExtend, void RightMove(Elemtype*, int, int))
{
if (index <= 0 || index >= l->length + 2)
return INDEXERROR;
if (l->length + num > l->maxlength) {
if (isExtend == true) {
ExtendList(l, num);
} else
return LISTFULL;
}
RightMove(&l->date[l->length - 1], num, l->length - index + 1);
for (int i = 0; i < num; i++) {
l->date[index - 1 + i] = date[i];
}
l->length += num;
return INSERTSUCCESS;
}
bool InterageEqual(Elemtype a, Elemtype b)
{
return a == b ? true : false;
}
int List_GetLocation(Linear_List_p l, const Elemtype date, bool isEqual(Elemtype, Elemtype))
{
if (l == NULL || l->date == NULL || l->length == 0)
return 0;
int i = 0;
while (i <= l->length - 1 && !isEqual(l->date[i], date)) {
i++;
}
if (i == l->length)
return 0;
return i + 1;
}
void LeftMove(Elemtype* array, int length, int num)
{
Elemtype* temp = array - length;
while (num--) {
*temp = *(temp + length);
temp++;
}
}
Elemtype* List_DelElem(Linear_List_p l, int num, int index, void LeftMove(Elemtype*, int, int))
{
if (l == NULL || l->date == NULL || num <= 0 || index <= 0 || index + num - 1 > l->length)
return NULL;
Elemtype* date = (Elemtype*)malloc(sizeof(Elemtype) * num);
for (int i = 0; i < num; i++) {
date[i] = l->date[index - 1 + i];
}
LeftMove(&l->date[index - 1 + num], num, l->length - index + 1 - num);
l->length -= num;
return date;
}
void _List_DelElem(Linear_List_p l, int num, int index, void LeftMove(Elemtype*, int, int))
{
if (l == NULL || l->date == NULL || num <= 0 || index <= 0 || index + num - 1 > l->length)
return;
}
Linear_List_p MergeList(Linear_List_p la, Linear_List_p lb)
{
if (la == NULL || lb == NULL)
return NULL;
Linear_List_p temp = CreatList_p(GetListLength(la) + GetListLength(lb));
if ((la->date == NULL && lb->date == NULL) || (la->length == 0 && lb->length == 0))
return temp;
if (la->length == 0) {
memcpy(temp->date, lb->date, sizeof(Elemtype) * lb->length);
temp->length += lb->length;
}
if (lb->length == 0) {
memcpy(temp->date, lb->date, sizeof(Elemtype) * lb->length);
temp->length += la->length;
}
if (la->length != 0 && lb->length != 0) {
Linear_List_p gtemp = la->length >= lb->length ? la : lb;
Linear_List_p atemp = la->length >= lb->length ? lb : la;
int glength = la->length >= lb->length ? la->length : lb->length;
int alength = la->length >= lb->length ? lb->length : la->length;
memcpy(temp->date, gtemp->date, glength * sizeof(Elemtype));
temp->length += glength;
for (int i = 0; i < alength; i++) {
if (!List_GetLocation(gtemp, atemp->date[i], InterageEqual))
List_InsertElem(temp, &atemp->date[i], 1, temp->length + 1, false, RightMove);
}
}
return temp;
}
void visit(Elemtype* date)
{
printf("%d", *date);
}
void List_Info(Linear_List_p l, void visit(Elemtype*))
{
printf("------------------\n");
if (l == NULL)
printf("Error! the List is nonexistent.\n");
if (l->length == 0)
printf("The list is Empty.\n");
for (int i = 0; i < l->length; i++) {
visit(&l->date[i]);
printf(" ");
if (i + 1 % 10 == 0)
printf("\n");
}
printf("\n");
}
int Purse_List(Linear_List_p l, bool isEqual(Elemtype, Elemtype))
{
if (l == NULL)
return 1;
if (l->date == NULL || l->length == 0)
return 2;
printf("*****************************.\n");
for (int i = 0; i < l->length; i++) {
for (int j = i + 1; j < l->length; j++) {
if (isEqual(l->date[i], l->date[j])) {
Elemtype *date = List_DelElem(l,1,j+1,LeftMove);
printf("printf %d TheSamenumindex %d delete %d \n",l->date[i],j+1,*date);
free(date);
--j;
}
}
}
return 3;
}
int main(void)
{
int la_date[6] = { 1, 2, 3, 4, 5, 11 };
int lb_date[7] = { 1, 2, 3, 11, 4, 7, 8 };
Linear_List_p la = CreatList_p(6);
Linear_List_p lb = CreatList_p(7);
List_InsertElem(la, la_date, 6, 1, false, RightMove);
List_InsertElem(lb, lb_date, 7, 1, false, RightMove);
List_Info(la, visit);
List_Info(lb, visit);
Linear_List_p merge_list = MergeList(la, lb);
List_Info(merge_list, visit);
int l_purse_date[10] = { 1, 3, 1, 2, 7, 2, 7, 1, 2, 4 };
Linear_List_p l_purse = CreatList_p(10);
List_InsertElem(l_purse, l_purse_date, 10, 1, false, RightMove);
List_Info(l_purse, visit);
Purse_List(l_purse, InterageEqual);
List_Info(l_purse, visit);
DelList(la);
DelList(lb);
DelList(l_purse);
return 0;
}
线性表的实现
最新推荐文章于 2024-09-13 19:36:07 发布