#include <stdio.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void creat_link(Link *head)
{
//*head = NULL;
*head = (Link)malloc(sizeof(Node));
(*head)->next = *head;
}
void insert_head_node(Link *newnode, Link *head)
{
(*newnode)->next = (*head)->next;
(*head)->next = *newnode;
}
void insert_tail_node(Link *newnode, Link *head)
{
Link temp = *head;
while(temp->next != *head)
{
temp = temp->next;
}
temp->next = *newnode;
(*newnode)->next = *head;
}
int insert_mid_node(Link *newnode, Link *head, int num)
{
Link temp = (*head)->next;
while(temp != *head)
{
if(temp->num == num)
{
(*newnode)->next = temp->next;
temp->next = *newnode;
return 0;
}
temp = temp->next;
}
}
int delete_node(int num, Link *head)
{
Link ptr = *head;
Link temp = ptr->next;
while(temp != *head)
{
if(temp->num == num)
{
ptr->next = temp->next;
free(temp);
temp = NULL;
return 0;
}
ptr = temp;
temp = temp->next;
}
}
int reverse_link(Link *head)
{
if((*head)->next == *head || (*head)->next->next == *head)
{
return 0;
}
Link ptr = *head;
Link str = ptr->next;
Link temp = str->next;
while(temp != *head)
{
str->next = ptr;
ptr = str;
str = temp;
temp = temp->next;
}
str->next = ptr;
(*head)->next = str;
}
int display_link(Link *head)
{
Link temp = (*head)->next;
while(temp != *head)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
creat_link(&head);
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_head_node(&newnode,&head);
//insert_tail_node(&newnode,&head);
//insert_mid_node(&newnode,&head);
}
display_link(&head);
#if 1
newnode = (Link)malloc(sizeof(Node));
newnode->num = 5;
insert_mid_node(&newnode,&head,8);
display_link(&head);
delete_node(7,&head);
reverse_link(&head);
display_link(&head);
#endif
return 0;
}
双向循环链表
最新推荐文章于 2021-07-29 20:03:47 发布