#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct Item
{
int stuNum;
}ITEM;
typedef struct NODE
{
ITEM item;
struct NODE *pNext;
}NODE_STRU;
typedef struct List
{
int itemNum;
NODE_STRU *pHead;
}LIST_STRU;
void CreateList(LIST_STRU *pList)
{
pList->itemNum = 0;
pList->pHead = NULL;
return;
}
int AddNode(LIST_STRU *pList,ITEM item)
{
NODE_STRU *pPos = pList->pHead;
NODE_STRU *pNode = NULL;
pNode = (NODE_STRU *)malloc(sizeof(NODE_STRU));
if(pNode == NULL)
{
return 0;
}
pList->itemNum++;
pNode->item.stuNum = item.stuNum;
pNode->pNext = NULL;
if(pPos == NULL)
{
pList->pHead = pNode;
return 1;
}
while(pPos->pNext != NULL)
{
pPos = pPos->pNext;
}
pPos->pNext = pNode;
return 1;
}
int DelNode(LIST_STRU *pList, int Key)
{
NODE_STRU *pPos = pList->pHead;
NODE_STRU *pTemp = NULL;
if(pPos == NULL)
{
return 0;
}
if(pPos->item.stuNum == Key)
{
pList->itemNum--;
pList->pHead = pPos->pNext;
free(pPos);
return 1;
}
while(pPos->pNext != NULL)
{
if(pPos->pNext->item.stuNum == Key)
{
pTemp = pPos->pNext;
pPos->pNext = pPos->pNext->pNext;
free(pTemp);
pList->itemNum--;
return 1;
}
pPos = pPos->pNext;
}
return 0;
}
void TraverseList(LIST_STRU *pList)
{
NODE_STRU *pNode = pList->pHead;
printf("Total:%d\r\n",pList->itemNum);
while(pNode != NULL)
{
printf("N0:%d\r\n",pNode->item.stuNum);
pNode = pNode->pNext;
}
}
int main(void)
{
LIST_STRU stList = {0};
ITEM stItem = {0};
ITEM stItem1 = {1};
CreateList(&stList);
AddNode(&stList,stItem);
AddNode(&stList,stItem1);
TraverseList(&stList);
DelNode(&stList,1);
TraverseList(&stList);
system("pause");
return 0;
}