双向循环链表

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值