12 - 09循环链表等

#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值