#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KLen 30
int g_iCount = 0;
typedef struct tagNode
{
char name[KLen];
char phone[KLen];
struct tagNode *pNext;
}Node;
int InitList(Node **pHead);
void ClearList(Node *pHead);
void DestroyList(Node *pHead);
int ListEmpty(Node *pHead);
int ListLength(Node *pHead);
void ListTraverse(Node *pHead);
int GetElem(Node *pHead, int index, Node *pElem);
int LocateElem(Node *pHead, Node *pElem);
int ListInsert(Node *pHead, int index, Node *pElem);
int ListDelete(Node *pHead, int index, Node *pElem);
int main(void)
{
Node *pList = NULL;
Node node1 = {"Jim", "1234", NULL};
Node node2 = {"Merry", "4321", NULL};
Node node3 = {"James", "3456", NULL};
Node node4;
InitList(&pList);
ListInsert(pList, 0, &node1);
printf("%d \n", ListLength(pList));
ListInsert(pList, 1, &node2);
ListInsert(pList, 1, &node3);
printf("%d \n", ListEmpty(pList));
printf("%d \n", ListLength(pList));
ListTraverse(pList);
ListDelete(pList, 2, NULL);
printf("%d \n", ListLength(pList));
ListTraverse(pList);
DestroyList(pList);
system("pause");
return 0;
}
int InitList(Node **pHead)
{
*pHead = (Node *)malloc(sizeof(Node));
if(*pHead == NULL)
{
return 0;
}
(*pHead)->pNext = NULL;
return 1;
}
void ClearList(Node *pHead)
{
Node *pCurrentNode = NULL;
Node *pNextNode = NULL;
if(pHead->pNext != NULL)
{
pCurrentNode = pHead->pNext;
while(pCurrentNode != NULL)
{
pNextNode = pCurrentNode->pNext;
free(pCurrentNode);
pCurrentNode = pNextNode;
}
}
}
void DestroyList(Node *pHead)
{
ClearList(pHead);
free(pHead);
pHead = NULL;
}
int ListEmpty(Node *pHead)
{
if(g_iCount == 0)
{
return 1;
}
return 0;
}
int ListLength(Node *pHead)
{
return g_iCount;
}
void ListTraverse(Node *pHead)
{
Node *pCurrentNode = pHead->pNext;
while(pCurrentNode != NULL)
{
printf("姓名:%s ", pCurrentNode->name);
printf("电话:%s ", pCurrentNode->phone);
printf("\n");
pCurrentNode = pCurrentNode->pNext;
}
printf("\n\n");
}
int GetElem(Node *pHead, int index, Node *pElem)
{
int count = 0;
if(index < 0 || index > g_iCount)
{
return 0;
}
Node *pCurrentNode = pHead;
while(pCurrentNode != NULL)
{
if(count == index)
{
pElem = pCurrentNode;
return 1;
}
count++;
pCurrentNode = pCurrentNode->pNext;
}
return 1;
}
int LocateElem(Node *pHead, Node *pElem)
{
int index = 1;
Node *pCurrentNode = pHead->pNext;
while(pCurrentNode != NULL)
{
if(!strcmp(pCurrentNode->name, pElem->name)&& !strcmp(pCurrentNode->phone, pElem->phone))
{
return index;
}
pCurrentNode = pCurrentNode->pNext;
index++;
}
return -1;
}
int ListInsert(Node *pHead, int index, Node *pElem)
{
int count = 0;
Node *pNode = NULL;
Node *pCurrentNode = NULL;
if(index < 0 || index > g_iCount)
{
return 0;
}
pNode = (Node *)malloc(sizeof(Node));
if(pNode == NULL)
{
return 0;
}
strcpy(pNode->name, pElem->name);
strcpy(pNode->phone, pElem->phone);
pCurrentNode = pHead;
while(pCurrentNode != NULL)
{
if(count == index)
{
Node *pTemp = pCurrentNode->pNext;
pCurrentNode->pNext = pNode;
pNode->pNext = pTemp;
g_iCount++;
return 1;
}
count++;
pCurrentNode = pCurrentNode->pNext;
}
return 1;
}
int ListDelete(Node *pHead, int index, Node *pElem)
{
int count = 0;
Node *pCurrentNode = pHead;
Node *pPreNode = NULL;
if(index <= 0 || index > g_iCount)
{
return 0;
}
while(pCurrentNode != NULL)
{
if(count == index)
{
pPreNode->pNext = pCurrentNode->pNext;
if(pElem != NULL)
{
strcpy(pElem->name, pCurrentNode->name);
strcpy(pElem->phone, pCurrentNode->phone);
}
free(pCurrentNode);
g_iCount--;
return 1;
}
count++;
pPreNode = pCurrentNode;
pCurrentNode = pCurrentNode->pNext;
}
return 1;
}