#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
LNode *initList()
{
LNode *list = (LNode *)malloc(sizeof(LNode));
if(NULL == list)
{
printf("malloc list node failed\n");
return NULL;
}
list->data = 0;
list->next = list;
return list;
}
void headInsert(LNode *list, int data)
{
LNode *node = (LNode *)malloc(sizeof(LNode));
node->data = data;
node->next = list->next;
list->next = node;
return;
}
void tailInsert(LNode *list, int data)
{
LNode *tail = list;
LNode *node = (LNode *)malloc(sizeof(LNode));
node->data = data;
while(tail->next != list)
{
tail = tail->next;
}
node->next = list;
tail->next = node;
return ;
}
int deleteNode(LNode *list, int data)
{
LNode *pre = list;
LNode *node = list->next;
while (node != list)
{
if(node->data == data)
{
//delete data node
pre->next = node->next;
}
pre = node;
node = node->next;
}
}
void printList(LNode *list)
{
LNode *node = list->next;
while (node != list)
{
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main()
{
LNode *list = initList();
headInsert(list, 0);
headInsert(list, 1);
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
tailInsert(list, 5);
tailInsert(list, 6);
printList(list);
deleteNode(list, 2);
deleteNode(list, 5);
printList(list);
}
1