#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct dblnode
{
int num;
struct dblnode * prior, * next;
};
typedef struct dblnode Dblnode;
typedef Dblnode * Dblink;
enum result_val{RET_OK = 1000,RET_NO};
int is_malloc_ok( Dblink new_node)
{
if(new_node == NULL)
{
return RET_NO;
}
else
{
return RET_OK;
}
}
int create_node( Dblink * new_node)
{
*new_node = ( Dblink)malloc((unsigned)sizeof(Dblnode));
if(RET_NO == is_malloc_ok(*new_node))
{
return RET_NO;
}
else
{
return RET_OK;
}
}
void create_link( Dblink * head)
{
create_node(head);
(*head)->prior = *head;
(*head)->next = *head;
}
void display_link( Dblink head)
{
Dblink p =NULL;
if(head == NULL)
{
printf("no link !\n");
}
else
{
p = head->next;
if(head->next == head)
{
printf("the link is empty !\n");
return;
}
else
{
while(p != head)
{
printf("%d\n",p->num);
p = p->next;
}
}
}
}
void insert_node_head( Dblink head, Dblink new_node)
{
new_node->next = head->next;
new_node->prior = head;
head->next = new_node;
head->prior = new_node;
}
void insert_node_mind_sort( Dblink head, Dblink new_node)
{
Dblink p = NULL;
Dblink q = NULL;
p = (head)->next;
q = head;
while(p != head && p->num < new_node->num)
{
q = p;
p = p->next;
}
new_node->next = p;
new_node->prior =q;
q->next = new_node;
new_node->next->prior = new_node;
}
void insert_node_tail( Dblink head, Dblink new_node)
{
new_node->next = head;
new_node->prior = head->prior;
head->prior->next =new_node;
head->prior = new_node;
}
void insert_node_mind_front( Dblink head, Dblink new_node,int loc)
{
Dblink p = NULL;
p = head->next;
while(p != head && p->num != loc)
{
p = p->next;
}
new_node->next = p;
new_node->prior =p->prior;
p->prior->next = new_node;
new_node->next->prior = new_node;
}
void insert_node_mind_behand(Dblink head,Dblink new_node,int loc)
{
Dblink p = NULL;
p = head->next;
while(p != head && p->num != loc)
{
p = p->next;
}
new_node->next = p->next;
new_node->prior =p;
p->next = new_node;
new_node->next->prior = new_node;
}
void delete_node( Dblink head,int num)
{
Dblink p = NULL;
p = head->next;
while(p != head && p->num != num)
{
p = p->next;
}
if(p == head)
{
printf("NO find\n");
}
else
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
}
int length_link( Dblink head)
{
Dblink p = NULL;
int count = 0;
p = head->next;
while(p != head)
{
count++;
p = p->next;
}
return count;
}
Dblink find( Dblink head,int num)
{
Dblink p = NULL;
p = head->next;
while(p != head && p->num != num)
{
p = p->next;
}
if(p == head)
{
return NULL;
}
else
{
return p;
}
}
void make_empty( Dblink head)
{
Dblink p =NULL;
p = head->next;
while(head->next != head)
{
p = head->next;
head->next = head->next->next;
free(p);
}
}
void release_link( Dblink * head)
{
make_empty(*head);
while(*head != NULL)
{
if((*head)->next == *head)
{
free(*head);
}
*head = NULL;
}
}
int main()
{
Dblink head = NULL;
Dblink new_node = NULL;
Dblink p = NULL;
int i;
int loc;
int num;
int count=0;
srand((unsigned)time(NULL));
create_link(&head);
for(i = 0; i < 10; i++)
{
if(RET_OK == create_node(&new_node))
{
new_node->num = rand() % 100;
//new_node->num = i+1;
insert_node_mind_sort(head,new_node);
//insert_node_head(head,new_node);
// insert_node_tail(head,new_node);
}
else
{
i--;
}
}
display_link(head);
/*count = length_link(head);//长度
printf("the length is %d\n",count);
display_link(head);*/
/*printf("please input the num\n");//查找
scanf("%d",&num);
p = find(head,num);
if(p == NULL)
{
printf("no found\n");
}
else
{
printf("the result is %d\n",p->num);
}*/
printf("\nplease input the num\n");//删除
scanf("%d",&num);
printf("\nthe result is \n");
delete_node(head,num);
display_link(head);
/*if(RET_OK == create_node(&new_node))//插入
{
printf("please input the loc\n");
scanf("%d",&loc);
printf("please input the num\n");
scanf("%d",&new_node->num);
printf("\n");
//insert_node_mind_front(head,new_node,loc);
insert_node_mind_behand(head,new_node,loc);
display_link(head);
}*/
make_empty(head);
display_link(head);
release_link(&head);
display_link(head);
}
|