#include
#include
struct node
{
int num;
struct node* next;
};
typedef struct node Node;
typedef Node* Link;
void init_Link(Link* head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = *head;
}
void insert_head_node(Link* head,Link newnode)
{
newnode -> next = (*head)->next;
(*head)->next = newnode;
}
void insert_tail_node(Link* head,Link newnode)
{
Link temp = *head;
while(temp -> next != *head)
{
temp = temp -> next;
}
temp -> next = newnode;
newnode -> next = *head;
}
int insert_mid_node(Link head,Link newnode,int num)
{
Link temp = head->next;
while(temp != head)
{
if(temp -> num == num)
{
newnode -> next = temp -> next;
temp -> next = newnode;
return 1;
}
temp = temp -> next;
}
return -1;
}
int del_node(Link* head,int num)
{
Link temp = *head;
Link front = temp;
temp = temp->next;
while(temp != *head)
{
if(temp->num == num)
{
front->next = temp->next;
free(temp);
temp = NULL;
return 0;
}
else
{
front = temp;
temp = temp->next;
}
}
return -1;
}
int reverse(Link* head)
{
if((*head)->next->next == *head || (*head)->next == *head)
{
return -1;
}
else
{
Link p = *head;
Link s = p->next;
Link t = s->next;
while(t != *head)
{
s->next = p;
p = s;
s = t;
t = t->next;
}
s->next = p;
(*head)->next = s;
return 0;
}
}
void display(Link head)
{
Link temp = head->next;
while(temp != head)
{
printf("%d\n",temp -> num);
temp = temp -> next;
}
}
int main()
{
Link head;
Link newnode;
int i = 0;
init_Link(&head);
for(i = 0;i < 10; i++)
{
newnode = (Node*)malloc(sizeof(Node));
newnode -> num = i + 1;
insert_head_node(&head,newnode);
//insert_tail_node(&head,newnode);
//insert_mid_node(head,newnode,5);
}
printf("insert head:\n");
display(head);
return 0;
}