程序员面试宝典之数据结构基础----⑥双链表的建立,插入和删除

本文介绍了双链表的基础知识,包括如何建立双链表,以及进行节点的插入和删除操作。文章提供了经过验证的代码示例,适用于使用GNU GCC Compile的编程环境。
摘要由CSDN通过智能技术生成

前几篇有了单链表的各种操作,双链表的操作与单链表类此,只是增加了一个前置指针而已,双链表的各种操作形同单链表,此处只给出双链表的创建,插入和删除操作:

代码已经过编译运行,确认无误(编译环境:codeblocks下的GNU GCC Compile)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
#define NUM 3
//对于双链表,注意,除最后一个节点的next指针指向NULL外,第一个节点的pre指针亦指向NULL
typedef struct NODE
{
    int data;
    struct NODE *next;
    struct NODE *pre;

}dnode;

dnode* dcreat()
{
    dnode *head,*p,*s;
    int x,cycle = 1;
    head = (dnode*)malloc(sizeof(dnode));
    p = head;
    while(cycle)
    {
        printf("\nPlease input the data:");
        scanf("%d",&x);
        if(0 != x)
        {
            s = (dnode*)malloc(sizeof(dnode));
            s->data = x;
            printf("%d",s->data);
            p->next = s;
            s->pre = p;
            p = s;
        }
        else
            cycle = 0;

    }
    head = head->next;
    head->pre = NULL;
    p->next = NULL;
   // printf("    %d",head->data);
    return head;
}

int length(dnode* head)
{
    int n= 0;
    dnode *p;
    p = head;
    while(p!=NULL)
    {
        p = p->next;
        n++;
    }
    return n;
}

//打印双链表:
void dprint(dnode* head)
{
    dnode* p;int n;
    n = length(head);
    p = head;
    if(head!= NULL)
    while(p!=NULL)
    {
        printf("%d\n",p->data);
        p = p->next;
    }
}

//双链表删除节点:
dnode* ddel_node(dnode* head,int num)
{
    dnode* p1;
    p1 = head;
    while(num != p1->data && NULL != p1->next)
    {
        p1 = p1->next;
    }
    if(num == p1->data)
    {
        if(p1 == head)
        {

            head = head->next;
            head->pre = NULL;
            free(p1);
        }
        else if(NULL == p1->next)
        {
            p1->pre->next = NULL;
            free(p1);
        }
        else
        {
            p1->pre->next = p1->next;
            p1->next->pre = p1->pre;
            free(p1);
        }
    }
    else
    {
        printf("\nCould not find the num %d",num);
    }
    return head;
}

//双链表插入节点
dnode* dinsert(dnode* head,int num)
{
    dnode* p0,*p1;
    p1 = head;
    p0 = (dnode*)malloc(sizeof(dnode));
    p0->data = num;
    while(p0->data > p1->data && p1->next != NULL)
    {
        p1 = p1->next;
    }
    if(p0->data <= p1->data)
    {
        if(head == p1)
        {

            p0->next = p1;
            p0->pre = NULL;
            p1->pre = p0;
        }
        else
        {
            p1->pre->next = p0;
            p0->next = p1;
            p0->pre = p1->pre;
            p1->pre = p0;
        }
    }
    else
    {
        p1->next = p0;
        p0->pre = p1;
        p0->next =NULL;
    }
    return head;
}

int main()
{
    dnode* new_head = dcreat();
    dprint(new_head);
    dnode* head = ddel_node(new_head,NUM);
    dprint(head);
    head = dinsert(head,4);
    dprint(head);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值