#include <stdio.h>
#include <stdlib.h>
struct node
{
int num ;
struct node *next ;
};
void creat_link(struct node **head)
{
*head = (struct node *)malloc(sizeof(struct node));
(*head)->next = *head;
}
void insert_head(struct node *head,struct node *new_node)
{
if(head->next==head)
{
head->next = new_node;
new_node->next = head;
}
else
{
new_node->next = head->next ;
head->next = new_node;
}
}
void display_node(struct node *head)
{
struct node *p ;
p = head->next ;
if (p ==NULL)
{
printf ("link is empty\n");
}
else
{
while(p != head)
{
printf("num = %d\n" , p->num);
p = p->next;
}
}
}
void insert_mid(struct node *head , struct node *new_node , int a)
{
struct node *p,*q;
q = head ;
p = head->next ;
if(p == head)
{
head->next = new_node ;
new_node->next = head ;
}
else
{
while (p->num != a && p->next != head)
{
q = p ;
p = p->next ;
}
if ( p->num != a && p->next == head)
{
p->next = new_node;
new_node->next = head;
}
else
{
new_node->next = q->next ;
q->next = new_node;
}
}
}
void delate_node(struct node *head , int a)
{
struct node *p ,*q ;
q = head ;
p = head->next ;
if (head == NULL)
{
printf(“not find struct\n”);
}
else if ( p == head)
{
printf(“link is empty\n”);
}
else
{
while( p->num != a && p->next != head)
{
q = p ;
p = p->next ;
}
if (p->next == head && p->num != a)
{
printf(“not find %d\n”,a);
}
else
{
q->next = p->next;
free§;
}
}
}
void release_link(struct node **head )
{
struct node *p, *q ;
q = (*head);
p = (*head)->next ;
if (q == NULL)
{
printf(“link is empty\n”);
}
else
{
while ( p != *head)
{
q = p ;
p = p->next ;
free(q);
}
if(p == *head)
{
printf(“link has been relesed\n”);
}
}
}
int main()
{
struct node *head = NULL;
struct node *new_node = NULL;
int i , a , b ;
printf( “please input num location :\n”);
scanf("%d" , &a) ;
printf( “please input num value :\n” ) ;
scanf("%d" , &b) ;
creat_link( &head );
for( i = 0 ; i < 10 ; i++)
{
new_node = (struct node *)malloc(sizeof(struct node));
if( new_node == NULL )
{
printf (“malloc error\n”);
exit(-1) ;
}
new_node->num = i ;
insert_head(head , new_node);
}
new_node = (struct node *)malloc(sizeof(struct node));
if( new_node == NULL )
{
printf ("malloc error\n");
exit(-1) ;
}
new_node->num = b ;
insert_mid(head , new_node, a);
display_node(head);
printf("please input num to delate:\n");
scanf("%d" , &a) ;
delate_node(head , a) ;
display_node(head);
release_link( &head );
return 0;
}