实现起来其实不难,于是我把代码做了优化,模仿了stm32的库函数哈哈哈,便于理解和修改。不足之处请指出
#include "stdio.h"
#include "stdlib.h"
#define ElementType int
#define uint unsigned int
ElementType pa[6] = {1, 2, 2, 3, 5, 7};
ElementType pb[5] = {2, 3, 4, 6, 8};
ElementType *pc;
uint MaxLength1 = 6;
uint MaxLength2 = 5;
uint structLength = 0;
typedef struct Node
{
ElementType data;
struct Node *next;
struct Node *prior;
} LNode, *LinkList;
LinkList presentNode;
LinkList headNode;
LinkList finalNode;
uint FLAG;
void structInit(LNode *pNode);
void printfStructData(LinkList pNode, uint nowStructLength);
void structInsert(LNode *pNode, ElementType insertLocation, uint insertData);
void structInsertArray(LinkList pNode, ElementType *pData, uint dataLength);
void deleteStructElement(LinkList pNode, uint deleteLocation);
void changeStructElement(LinkList pNode, uint changeLocation, ElementType structData);
void findStructElement(LinkList pNode, uint findLocation);
void checkNodeInsertLocation(uint flag);
void checkNodeCreat(LNode *Node);
void checkError(uint flag);
void checkNodeIsEmpty(LNode *Node);
int main()
{
LNode *Node1 = (LNode *)(malloc(sizeof(LNode)));
checkNodeCreat(Node1);
structInit(Node1);
structInsertArray(headNode, pa, MaxLength1);
printfStructData(headNode, structLength);
deleteStructElement(headNode, 2);
printfStructData(headNode, structLength);
findStructElement(headNode, 3);
printfStructData(headNode, structLength);
changeStructElement(headNode, 3, 99);
printfStructData(headNode, structLength);
while (1);
return 0;
}
void structInit(LNode *pNode)
{
pNode->data = 0;
pNode->prior = pNode;
pNode->next = pNode;
headNode = pNode;
finalNode = pNode;
presentNode = pNode;
}
void printfStructData(LinkList pNode, uint nowStructLength)
{
uint flag = 0;
while (nowStructLength != 0)
{
nowStructLength--;
pNode = pNode->next;
printf("%d", pNode->data);
if (nowStructLength != 0)
{
printf("->");
}
}
printf("\n");
}
void checkError(uint flag)
{
if (flag == 401)
printf("\nERROR:401. Insert location is wrong. Can not be 0 or greater than structLength.\n");
else if (flag == 501)
printf("\nERROR:501. Creat struct is defated\n.");
else if (flag == 502)
printf("\nError:502. List is empty.\n");
}
void checkNodeCreat(LNode *Node)
{
if (Node == NULL)
{
checkError(501);
}
}
void checkNodeIsEmpty(LNode *Node)
{
if (Node->next == Node && Node->prior == Node)
{
checkError(502);
}
}
void checkNodeInsertLocation(uint insertLocation)
{
if (insertLocation < 1 || insertLocation > structLength + 1)
checkError(401);
}
void structInsert(LinkList pNode, ElementType insertLocation, uint insertData)
{
int i = 0;
LNode *newNode = (LNode *)(malloc(sizeof(LNode)));
checkNodeCreat(newNode);
checkNodeInsertLocation(insertLocation);
for (i = 0; i < insertLocation; i++)
{
pNode = pNode->next;
}
newNode->data = insertData;
pNode->prior->next = newNode;
newNode->prior = pNode->prior;
newNode->next = pNode;
pNode->prior = newNode;
structLength++;
}
void structInsertArray(LinkList pNode, ElementType *pData, uint dataLength)
{
uint flag;
for (flag = 0; flag < dataLength; flag++)
{
structInsert(pNode, structLength + 1, *pData++);
}
}
void
deleteStructElement(LinkList pNode, uint deleteLocation)
{
int i = 0;
checkNodeCreat(pNode);
checkNodeInsertLocation(deleteLocation);
for (i = 0; i < deleteLocation; i++)
{
pNode = pNode->next;
}
printf("要删除的结点值为:%d\n", pNode->data);
pNode->prior->next = pNode->next;
pNode->next->prior = pNode->prior;
free(pNode);
structLength--;
}
void findStructElement(LinkList pNode, uint findLocation)
{
int i = 0;
checkNodeCreat(pNode);
checkNodeInsertLocation(findLocation);
for (i = 0; i < findLocation; i++)
{
pNode = pNode->next;
}
printf("要查找的元素值为%d\n", pNode->data);
}
void changeStructElement(LinkList pNode, uint changeLocation, ElementType structData)
{
int i = 0;
checkNodeCreat(pNode);
checkNodeInsertLocation(changeLocation);
for (i = 0; i < changeLocation; i++)
{
pNode = pNode->next;
}
pNode->data = structData;
printf("删除的元素为%d\n", pNode->data);
}
|