12.22双向链表

双向链表的头插、尾插、中间插入、删除等操作。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAN_ERR 0
#define MAN_OK 1

struct dnode
{
    int num;
    struct dnode *prior;
    struct dnode *next;
};
typedef struct dnode Dnode;
typedef struct dnode * link;

void create_link(link * head)
{
    *head = (link)malloc(sizeof(Dnode));
    (*head)->prior = (*head)->next = *head;
}
int is_malloc_ok(link new_dnode)
{
    if(new_dnode==NULL)
    {
        printf("内存分配失败,将重新分配!\n");
        return MAN_ERR;
    }
    else
    {
        return MAN_OK;
    }
}
void create_dnode(link * new_dnode,int i)
{
    (*new_dnode) = (link)malloc(sizeof(Dnode));
    while((is_malloc_ok(*new_dnode))==0)
    {
        (*new_dnode) = (link)malloc(sizeof(Dnode));
    }
    (*new_dnode)->num = i;

}
void insert_link_head(link head, link new_dnode)
{
    link p , q;
    p = head;
    q = p->next;

    p->next = new_dnode;
    q->prior = new_dnode;
    new_dnode->next = q;
    new_dnode->prior = p;

}
void insert_link_tail(link head, link new_dnode)
{
    link p;
    p = head;

    while(p->next != head)
    {
        p = p->next;
    }
    p->next = new_dnode;
    head->prior = new_dnode;
    new_dnode->next = head;
    new_dnode->prior = p;
}
void insert_link_mid(link head, link new_dnode,int loc)
{
    link p;
    p = head->next;

    while(p->num != loc && p->next != head)
    {
        p = p->next;
    }
    /*if(p == head)
    {
        head->next = new_dnode;
        head->prior = new_dnode;
        new_dnode->next = head;
        new_dnode->prior = head;
    }*/
    if( p->next == head && p->num != loc)
    {
        p->next = new_dnode;
        head->prior = new_dnode;
        new_dnode->next = head;
        new_dnode->prior = p;
    }
    else //(p->num == loc && p->next != head)
    {
        p->next->prior = new_dnode;
        new_dnode->next = p->next;
        p->next = new_dnode;
        new_dnode->prior = p->prior;
    }
}
void delete_dnode(link head, int val)
{
    link p;
    p = head->next;

    while( p->num != val && p->next != head)
    {
        p = p->next;
    }
    if(p->next == head && p->num != val)
    {
        printf("没有这个数,删除失败!\n");
    }
    else
    {
        p->prior->next = p->next;
        p->next->prior = p->prior;
    }
}
void back_display_link(link head)
{
    link p;
    p = head;

    if( p == NULL)
    {
        printf("link is empty!\n");
    }
    else
    {
        while( p->next != head)
        {
            printf("num=%d\n",p->next->num);
            p = p->next;
        }
    }

}
void release_link(link * head)
{
    link p, q;
    p = q = *head;

    if(p->next != *head)
    {
        q = p->next;

        while( q != *head)
        {
            (*head)->next = q->next;
            q->next->prior = *head;
            free(q);
            q = q->next;
        }
        free(*head);
        *head = NULL;
    }
    else
    {
        free(*head);
        *head = NULL;
    }
  
}
int main()
{
    link head = NULL;
    link new_dnode = NULL;
    int i;
    int loc, val;

    create_link(&head);

    for(i=0;i<10;i++)
    {
        create_dnode(&new_dnode,i);
       // insert_link_head(head,new_dnode);
        insert_link_tail(head,new_dnode);
    }
    printf("please input loc:");
    scanf("%d",&loc);
    printf("please input val:");
    scanf("%d",&val);

    create_dnode(&new_dnode,val);
    insert_link_mid(head,new_dnode,loc);

    printf("please input delete val is:");
    scanf("%d",&val);

    delete_dnode(head,val);

    back_display_link(head);

    release_link(&head);
    back_display_link(head);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值