Linear_list.h
#ifndef LINEAR_LIST_H
#define LINEAR_LIST_H
#include"Status.h"
#define LIST_INIT_SIZE 100
#define LIST_INCREAMENT 10
typedef int ElemType;
typedef struct SqList
{
ElemType *elem;
int length;
int list_size;
}SqList,*Ptr;
//线性表的顺序存储
typedef Ptr SqListPtr;
Status List_Init(SqListPtr L); //初始化线性表
Status List_Retrival(SqListPtr L, int pos, ElemType* elem);
Status List_Locate(SqListPtr L, ElemType elem, int* pos);
Status List_Insert(SqListPtr L, ElemType elem, int pos);
Status List_Delete(SqListPtr L, int pos);
#endif
linear_list.cpp
#include "Linear_list.h"
#include"Linear_list.h"
//初始化线性表
Status List_Init(SqListPtr L) {
Status S = S_SUCCESS;
L->list_size = LIST_INIT_SIZE;
L->length = 0;
L->elem = (ElemType*)malloc(sizeof(ElemType) * L->list_size);
if (L->elem == NULL)
{
return S_FAIL;
}
return S;
}
Status List_Retrival(SqListPtr L, int pos, ElemType* elem)
{
Status S = RANGE_ERROR;
if (L)
{
if ((pos - 1) >= 0 && (pos - 1) < L->length)
{
*elem = L->elem[pos - 1];
S = S_SUCCESS;
}
}
else
{
S = S_FAIL
}
return S;
}
Status List_Locate(SqListPtr L, ElemType elem, int* pos)
{
Status S = RANGE_ERROR;
if (L)
{
for (int i = 0; i < L->length; ++i)
{
if (L->elem[i] == elem)
{
*pos = i + 1;
S = S_SUCCESS;
break;
}
}
}
else
{
S = S_FAIL;
}
return S;
}
Status List_Insert(SqListPtr L, ElemType elem, int pos)
{
Status S = RANGE_ERROR;
if ((pos-1)>0&&(pos-1)<L->length)
{
if (L&&L->length<L->list_size)
{
for (int i=L->length-1,i>=pos-1;--i)
{
L->elem[i + 1] = L->elem[i];
}
L->elem[pos - 1] = elem;
L->length++;
S = S_SUCCESS;
}
}
else
{
S = S_FAIL;
}
return S;
}
Status List_Delete(SqListPtr L, int pos)
{
Status S = RANGE_ERROR;
if ((pos-1)>0&&(pos-1)<L->length)
{
if (L&&L->length<L->list_size)
{
for (int i=pos-1;i<L->length;i++)
{
L->elem[i] = L->elem[i + 1];
}
L->length--;
S = S_SUCCESS;
}
}
else
{
S = S_FAIL;
}
return S;
}
List_node.h
#ifndef LIST_NODE_H
#define LIST_NODE_H
#include "Status.h"
typedef ElemType int;
typedef struct Node
{
ElemType elem;
struct Node* next;
}Node,*Ptr;
typedef Ptr* SqListPtr;
Status List_Retrieve(SqListPtr L, int pos, ElemType* elem); //查找
Status List_Locate(SqListPtr L,ElemType elem,int *pos); //按值查找
Status List_Insert(SqListPtr L, ElemType elem, int pos);
Status List_Retrival(SqListPtr L, int pos, ElemType* elem);
Status List_Delete(SqListPtr L, int pos);
#endif
List_node.cpp
#include "List_node.h"
Status List_Retrieve(SqListPtr L, int pos, ElemType* elem)
{
Status S = RANGE_ERROR;
Ptr p=(*L)->next;
int i=1;
while (p&&i<pos)
{
i++;
p=p->next;
}
if (p&&i==pos)
{
*elem=p->elem;
S=S_SUCCESS;
/* code */
}
return S;
}
Status List_Locate(SqListPtr L,ElemType elem,int *pos) //°´Öµ²éÕÒ
{
Status S = S_FAIL;
Ptr p=(*L)->next;
int i=1;
while (p)
{
if (p->elem==elem)
{
*pos=i;
S=S_SUCCESS;
break;
}
i++;
p=p->next;
}
return S;
}
Status List_Insert(SqListPtr L, ElemType elem, int pos)
{
Status S=RANGE_ERROR;
Ptr pHead=(*L);
Ptr pTemp;
int i=1;
while (pHead!=nullptr&&i<pos)
{
i++;
pHead=pHead->next;
}
if (i==pos)
{
pTemp=pHead->next;
if (pTemp)
{
Ptr pElem=(Ptr)malloc(sizeof(Ptr));
pHead->next=pElem;
pElem->next=pTemp->next;
S=S_SUCCESS;
}
else
S=S_FAIL;
}
return S;
}
Status List_Delete(SqListPtr L, int pos)
{
Status S=RANGE_ERROR;
Ptr pHead=(*L);
Ptr pTemp;
int i=1;
while (pHead!=nullptr&&i<pos)
{
i++;
pHead=pHead->next;
}
if (i==pos)
{
pTemp=pHead->next;
if (pTemp)
{
pHead->next=pTemp->next;
delete pTemp;
S=S_SUCCESS;
}
else
S=S_FAIL;
}
return S;
}
Status List_Retrival(SqListPtr L, int pos, ElemType* elem)
{
Status S=RANGE_ERROR;
Ptr pHead=(*L);
int i=1;
while (pHead!=nullptr&&i<pos)
{
i++;
pHead=pHead->next;
}
if (i==pos)
{
*elem=pHead->elem;
S=S_SUCCESS;
}
return S;
}