数据结构 2 单循环链表
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* InitaNode()
{
Node* Head = (Node*)malloc(sizeof(Node));
Head->data = 0;
Head->next = Head;
return Head;
}
void HeadInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = list->next;
list->data++;
list->next = node;
}
void TailInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
Node* TempNode = list;
while (TempNode->next != list)
{
TempNode = TempNode->next;
}
TempNode->next = node;
node->next = list;
list->data++;
}
int DeleteNum(Node* list, int data)
{
Node* PreNode = list;
Node* CurrentNode = list->next;
while(CurrentNode->next != list)
{
if(CurrentNode->data == data)
{
PreNode->next = CurrentNode->next;
free(CurrentNode);
list->data--;
return TRUE;
}
PreNode = CurrentNode;
CurrentNode = CurrentNode->next;
}
return FALSE;
}
void PrintList(Node* list)
{
Node* node = list->next;
while(node->next != list)
{
printf("%d->",node->data);
node = node->next;
}
printf("NULL\n");
}
void main(void)
{
Node* HeadNode = InitaNode();
for(int i = 0; i < 6; i++)
{
HeadInsert(HeadNode,i);
}
for(int i = 1; i < 8; i++)
{
TailInsert(HeadNode,i);
}
DeleteNum(HeadNode,1);
PrintList(HeadNode);
}