List.h
#ifndef LIST_H
#define LIST_H
#define LEN sizeof(struct ListNode_T)
struct staff
{
int iStaffID;
char acName[20];
char acPasswd[10];
};
typedef struct ListNode_T
{
void *data;
struct ListNode_T *next;
}ListNode;
ListNode* ListInit(void *data);
ListNode* ListLocate(ListNode *head);
void ListAdd(ListNode *head,void *data);
int ListGetCount(ListNode *head);
ListNode* ListGetNode(int iIndex, ListNode *head);
int ListFree(ListNode *head);
ListNode *ListDel(int iIndex,ListNode *head);
ListNode* ListDel2(int iIndex,ListNode *head);
#endif
List.c
#include<stdio.h>
#include<stdlib.h>
#include "List.h"
typedef void(*pPrintEvent)(void *data);
ListNode* ListInit(void *data)
{
ListNode *pNode = (ListNode *)malloc(LEN);
pNode->data = data;
pNode->next = NULL;
return pNode;
}
ListNode* ListLocate(ListNode *head)
{
ListNode *pCurrent = head;
if(pCurrent == NULL)
return NULL;
while(pCurrent->next != NULL)
{
pCurrent = pCurrent->next;
}
return pCurrent;
}
void ListAdd(ListNode *head,void *data)
{
ListNode *p = NULL;
ListNode *pNode = NULL;
p = ListLocate(head); //找到最后个非空的节点
pNode = ListInit(data); //初始化一个新的节点
if(p == NULL)
{
printf("头节点未分配内存\n");
exit(1);
}
if(p->data == NULL)
{
head->data = data;
head->next = NULL;
}
else
{
p->next = pNode;
}
}
void PrintStaff(void *data)
{
struct staff *st = (struct staff*)data;
printf("%d %s %s\n",st->iStaffID,st->acName,st->acPasswd);
}
void print(ListNode *head,pPrintEvent PrintEvent)
{
ListNode *pCurrent = head;
while(pCurrent != NULL)
{
PrintEvent(pCurrent->data);
pCurrent = pCurrent->next;
}
}
int ListGetCount(ListNode *head)
{
ListNode *pCurrent = head;
int iCount = 0;
while(pCurrent != NULL)
{
iCount++;
pCurrent = pCurrent->next;
}
return iCount;
}
/*节点存在返回该节点,否则返回NULL指针*/
ListNode* ListGetNode(int iIndex, ListNode *head)
{
ListNode *pCurrent = head;
int i = 0;
while(i < iIndex && pCurrent != NULL)
{
pCurrent = pCurrent->next;
i++;
}
if(pCurrent == NULL)
{
printf("该节点不存在\n");
return NULL;
}
else
return pCurrent;
}
int ListFree(ListNode *head)
{
int iCount = 0;
ListNode *pCurrent = head;
while(head != NULL)
{
head = head->next;
iCount++;
free(pCurrent->data);
free(pCurrent);
pCurrent = head;
}
return iCount;
}
ListNode *ListDel(int iIndex,ListNode *head)
{
ListNode *pPrior = NULL;
ListNode *pNext = NULL;
ListNode *pCurrent = head;
int iCount =1;
while(pCurrent->next != NULL && pCurrent != NULL)
{
if(iIndex == 0)
{
pPrior = head;
head = head->next;
free(pPrior->data);
free(pPrior);
return head;
}
pPrior = pCurrent;
pCurrent = pCurrent->next;
if(iCount == iIndex)
{
pNext = pCurrent->next;
break;
}
iCount++;
}
if(pCurrent != NULL || pCurrent->next != NULL)
{
pPrior->next = pNext;
free(pCurrent->data);
free(pCurrent);
return head;
}
else
{
printf("无能删除的节点\n");
return head;
}
}
ListNode* ListDel2(int iIndex,ListNode *head)
{
ListNode *pPrior = NULL;
ListNode *pNext = NULL;
ListNode *pCurrent = head;
int iCount =1;
while(pCurrent->next != NULL && pCurrent != NULL)
{
if(iIndex == 0)
{
pPrior = head;
head = head->next;
free(pPrior->data);
free(pPrior);
return head;
}
pPrior = pCurrent;
pCurrent = pCurrent->next;
if(iCount == iIndex)
{
pNext = pCurrent->next;
break;
}
iCount++;
}
if(pCurrent != NULL || pCurrent->next != NULL)
{
pPrior->next = pNext;
pCurrent->next = NULL;
return pCurrent;
}
else
{
printf("无能删除的节点\n");
return NULL;
}
}
int main()
{
struct staff *st[10];
int i = 0;
int iCount = 0;
ListNode *pNode = NULL;
ListNode *head = ListInit(NULL);
for(i=0;i<3;i++)
{
st[i] = (struct staff *)malloc(sizeof(struct staff));
scanf("%d %s %s",&(st[i]->iStaffID),st[i]->acName,st[i]->acPasswd);
ListAdd(head,st[i]);
}
print(head,PrintStaff);
printf("\n");
iCount = ListGetCount(head);
printf("%d\n",iCount);
for(i=0;i<iCount;i++)
{
pNode = ListGetNode(i,head);
print(pNode,PrintStaff);
printf("\n");
}
pNode = ListDel2(1,head);
print(pNode,PrintStaff);
printf("______________________\n");
print(head,PrintStaff);
i = ListFree(head);
printf("%d\n",i);
return 0;
}
头结点没有独立出来为空,直接拿来用了。