单向循环链表
#include <stdio.h>
#include <stdlib.h>
typedef struct SinglyCycleList
{
int data;
struct SinglyCycleList *next;
}Node,*pNode;
void CreateSinglyCycleList(pNode *head)
{
(*head) = (pNode)malloc(sizeof(Node));
if((*head) == NULL)
{
printf("内存分配失败\r\n");
}
(*head)->data = 10;
(*head)->next = (*head);
}
pNode CreateNode(int data)
{
pNode newnode = (pNode)malloc(sizeof(Node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
pNode GetLastNode(pNode head)
{
pNode ptr = NULL;
ptr = head->next;
while(ptr != head)
{
ptr = ptr->next;
}
return ptr;
}
int GetListLength(pNode head)
{
int i = 0;
pNode ptr = NULL;
ptr = head->next;
while(ptr != head)
{
i++;
ptr = ptr->next;
}
return i;
}
pNode FindPosNode(pNode head,int data)
{
int i,index = 0;
pNode ptmp = head;
pNode ptr = head->next;
while(ptr != head)
{
if(ptr->data == data)
break;
ptr = ptr->next;
index++;
}
if(index == GetListLength(head))
{
printf("Not find node data!!!\r\n");
return NULL;
}
for(i=0; i<index; i++)
ptmp = ptmp->next;
return ptmp;
}
int FindIndexNode(pNode head,int index)
{
int i = 0;
pNode ptr = head->next;
while(ptr != head)
{
if(i == index)
break;
i++;
ptr = ptr->next;
}
if((head->next == head) || i== GetListLength(head))
return 0;
else
return i;
}
void InsertHeadNode(pNode head, int data)
{
pNode newnode = CreateNode(data);
pNode ptr = head->next;
head->next = newnode;
newnode->next = ptr;
}
void InsertTailNode(pNode head, int data)
{
pNode newnode = CreateNode(data);
pNode ptr = head;
while(ptr->next != head)
{
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = head;
}
void InsertPosNode(pNode pos, int data)
{
pNode ptr = NULL;
pNode newnode = CreateNode(data);
ptr = pos->next;
pos->next = newnode;
newnode->next = ptr;
}
void SaveDataToList(pNode head, int *buf, int len)
{
int i;
pNode ptr = head;
for(i=0; i<len; i++)
{
if(ptr == head)
ptr = ptr->next;
ptr->data = buf[i];
ptr = ptr->next;
}
}
void DeleteNode(pNode head, int data)
{
int i,index = 0;
pNode ptmp = head;
pNode ptr = head->next;
while(ptr != head)
{
if(ptr->data == data)
break;
ptr = ptr->next;
index++;
}
if(index >= GetListLength(head))
printf("Not this data node!!!\n");
else
{
for(i=0; i<index; i++)
ptmp = ptmp->next;
ptmp->next = ptr->next;
free(ptr);
}
}
void DeleteIndexNode(pNode head, int index)
{
int i;
pNode ptmp = head;
pNode ptr = NULL;
if(ptmp->next == head)
return ;
if(index >= 0 && index < GetListLength(head))
{
for(i=0; i<index; i++)
ptmp = ptmp->next;
ptr = ptmp->next;
ptmp->next = ptr->next;
free(ptr);
}
else
printf("Not this node index!!!\r\n");
}
void TraverseList(pNode head)
{
pNode ptr = NULL;
ptr = head->next;
while(ptr != head)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
int main()
{
int i;
int buf[15];
pNode Head;
pNode Pos;
CreateSinglyCycleList(&Head);
for(i=1; i<=10; i++)
InsertTailNode(Head,0);
TraverseList(Head);
for(i=0; i<15; i++)
buf[i] = i+1;
SaveDataToList(Head, buf, 15);
TraverseList(Head);
InsertHeadNode(Head,13);
TraverseList(Head);
DeleteNode(Head,8);
TraverseList(Head);
DeleteIndexNode(Head,9);
TraverseList(Head);
printf("List len: %d\r\n",GetListLength(Head));
Pos = FindPosNode(Head,9);
InsertPosNode(Pos,99);
TraverseList(Head);
printf("index 0: %d\r\n",FindIndexNode(Head,5));
DeleteIndexNode(Head,FindIndexNode(Head,5));
TraverseList(Head);
return 0;
}