#ifndef LIST_H
#define LIST_H
#define LIST_SIZE 10
enum LIST_TYPE
{
LIST_ERROR = -1,
LIST_OK
};
typedef int data_t;
typedef struct List List;
List *CreateList();
void DestoryList(List *pList);
int InsertItemList(List *pList, data_t tData, int iOffset);
List *ReverseList(List *pList);
int DelItem(List *pList, data_t *pData, int iOffset);
int UpdateItem(List *pList, data_t tNewData, data_t tOldData);
int SearchItem(List *pList, data_t tData);
void ShowList(List *pList);
#endif //_LIST_H_
#include
#include
#include
#include "list.h"
struct List
{
data_t data;
struct List *pNext;
};
List *CreateList()
{
List *pList = NULL;
pList = (List *) malloc (sizeof(List));
if (NULL == pList)
{
return pList;
}
memset(pList, 0, sizeof(List));
pList->pNext = NULL;
return pList;
}
void DestoryList(List *pList)
{
if (NULL == pList)
{
return;
}
//free data node
List *pNode = pList->pNext;
List *pTmp = NULL;
while(NULL != pNode)
{
pTmp = pNode->pNext;
free(pNode);
pNode = pTmp;
}
//free Header
free(pList);
pList = NULL;
}
int InsertItemList(List *pList, data_t tData, int iOffset)
{
if (NULL == pList)
{
return LIST_ERROR;
}
//create link node
List *pNode = (List *) malloc (sizeof(List));
if (NULL == pNode)
{
return LIST_ERROR;
}
memset(pNode, 0, sizeof(List));
pNode->data = tData;
pNode->pNext = NULL;
//insert node
pNode->pNext = pList->pNext;
pList->pNext = pNode;
return LIST_OK;
}
List *ReverseList(List *pList)
{
if (NULL == pList || NULL == pList->pNext)
{
return pList;
}
List *current = pList;
List *next = current->pNext;
List *prev = NULL;
while (next)
{
prev = next->pNext;
next->pNext = current;
current = next;
next = prev;
}
pList->pNext = NULL;
pList = current;
return pList;
}
int DelItem(List *pList, data_t *pData, int iOffset)
{
if (NULL == pList || NULL == pData)
{
return LIST_ERROR;
}
//free first data node
List *pDel = pList->pNext;
if (NULL != pDel)
{
*pData = pDel->data;
pList->pNext = pDel->pNext;
free(pDel);
pDel = NULL;
}
return LIST_OK;
}
int UpdateItem(List *pList, data_t tNewData, data_t tOldData)
{
if (NULL == pList)
{
return LIST_ERROR;
}
List *pNode = pList->pNext;
while(NULL != pNode)
{
if (pNode->data == tOldData)
{
pNode->data = tNewData;
return LIST_OK;
}
pNode = pNode->pNext;
}
return LIST_ERROR;
}
int SearchItem(List *pList, data_t tData)
{
if (NULL == pList)
{
return LIST_ERROR;
}
List *pNode = pList->pNext;
while(NULL != pNode)
{
if (pNode->data == tData)
{
return LIST_OK;
}
pNode = pNode->pNext;
}
return LIST_ERROR;
}
void ShowList(List *pList)
{
if (NULL == pList)
{
return;
}
List *pTmp = pList->pNext;
while(NULL != pTmp)
{
printf("%d ", pTmp->data);
pTmp = pTmp->pNext;
}
printf("\r\n");
}
#include
#include "list.h"
int main()
{
//create
List *pList = CreateList();
if (NULL == pList)
{
return -1;
}
printf("create ok\r\n");
//insert
int i = 5;
int iRet = -1;
while(i--)
{
iRet = InsertItemList(pList, i, 0);
if (LIST_ERROR == iRet)
{
printf("insert error\r\n");
break;
}
}
//show list
ShowList(pList);
pList = ReverseList(pList);
if (NULL == pList)
{
return -1;
}
printf("Reverse ok\r\n");
ShowList(pList);
//delete item
data_t tData = -1;
iRet = DelItem(pList, &tData, 2);
printf("iret %d tdata %d\r\n", iRet, tData);
ShowList(pList);
//search
iRet = SearchItem(pList, 3);
printf("index %d\r\n", iRet);
//update
iRet = UpdateItem(pList, 8, 1);
printf("update iret %d\r\n", iRet);
//pList->iCount = 100;
ShowList(pList);
//destory list
DestoryList(pList);
pList = NULL;
return 0;
}
编译结果
create ok
0 1 2 3 4
Reverse ok
3 2 1 0 0
iret 0 tdata 3
2 1 0 0
index -1
update iret 0
2 8 0 0
倒置为什么没有最后一个