#ifndef LINKEDLIST_H
#define LINKEDLIST_H
..
typedef int ElementType;
typedef struct tagNode
{
ElementType Data;
struct tagNode* NextNode;
} Node;
..
#endif
Node* SLL_CreateNode(ElementType NewData)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->Data = NewData;
NewNode->NextNode = NULL;
return NewNode;
}
void SLL_DestoryNode(Node* Node)
{
free(Node);
}
void SLL_AppendNode(Node** Head, Node* NewNode)
{
if ( (*Head) == NULL )
{
*Head = NewNode;
}
else
{
Node* Tail = (*Head);
while ( Tail->NextNode != NULL )
{
Tail = Tail->NextNode;
}
Tail->NextNode = NewNode;
}
}
void SLL_InsertAfter(Node* Current, Node* NewNode)
{
NewNode->NextNode = Current->NextNode;
Current->NextNode = NewNode;
}
void SLL_InsertNewHead(Node** Head, Node* NewHead)
{
if ( Head == NULL )
{
(*Head) = NewHead;
}
else
{
NewHead->NextNode = (*Head);
(*Head) = NewHead;
}
}
void SLL_RemoveNode(Node** Head, Node* Remove)
{
if ( *Head == Remove )
{
*Head = Remove->NextNode;
}
else
{
Node* Current = *Head;
while ( Current != NULL && Current->NextNode != Remove )
{
Current = Current->NextNode;
}
if ( Current != NULL )
Current->NextNode = Remove->NextNode;
}
}
Node* NewNode = NULL;
NewNode = SLL_CreateNode(0);
SLL_AppendNode(&List, NewNode);
NewNode = SLL_CreateNode(-1);
SLL_InsertNewHead(&List, NewNode);
Node* Current = NULL;
Current = SLL_GetNodeAt(List, 2);
NewNode = SLL_CreateNode(3000);
SLL_InsertAfter(Current, NewNode);
int Count = SLL_GetNodeCount(List);
for (int i = 0; i<Count; i++ )
{
Current = SLL_GetNodeAt(List, 0);
if ( Current != NULL )
{
SLL_RemoveNode(&List, Current);
SLL_DestoryNode(Current);
}
}