双链表的增删改查的功能实现代码。

1. 双链表比单链表在结构上多了一个前屈指针。这个前驱指针会指向该节点的前一个结点。所以其中的任意一个结点都能找到他的前一个结点位置并进行访问,也能找到他的下一个结点位置并进行访问。

#include <stdio.h>

#include <stdlib.h>

typedef  struct Node

{

    int date ;

    struct Node *qu;

    struct Node *hj;

}Node;

typedef Node* Linklist;

void chuangjian (Linklist* L,int i)

{

    Linklist p;

    *L = (Linklist)malloc(sizeof(Node));

    (*L)->qu=NULL;

    (*L)->hj=NULL;

    int j=1;

    while (j<=i) {

        p=(Linklist)malloc(sizeof(Node));//linklist

        scanf("%d",&(p->date));

        p->hj=(*L)->hj;

        (*L)->hj=p;

        p->qu=(*L);

        j++;

    }

}

void chazhao(Linklist L,int i,int *e)

{

    

    int j=1;

    Linklist p;

    p=L->hj;

    while (p&&j<i) {

        p=p->hj;

        ++j;

    }

    *e = p->date;

}

void shuchu(Linklist l)

{

    while (l->hj != NULL) {

    l = l->hj;

    printf("%d\n",l->date);

}

}

void genggai(Linklist *L,int i,int e)

{

    int j=1;

    Linklist p;

    p = (*L);

    while (p&&j<i) {

        p = p->hj;

        j++;

    }

    p->hj->date = e;

      printf("更改后的数据链为:\n");

    while ((*L)->hj != NULL) {

        (*L) = (*L)->hj;

        printf("%d  ",(*L)->date);}

    printf("\n");

}

void tianjia(Linklist *L,int i,int e)

{

    int j = 1;

    Linklist p,q;

    p = (*L);

    while (p&&j<i) {

        p = p->hj;

        j++;

    }

    q = (Linklist)malloc(sizeof(Node));

    q->date = e;

    q->hj = p->hj;

    p->hj->qu = q;

    p->hj = q;

    q->qu = p;

    while ((*L)->hj != NULL)

    {

        (*L) = (*L)->hj;

        printf("%d   ",(*L)->date);

    }

    printf("\n");

}

void shanchu(Linklist *L,int i,int *e)

{

    int j = 1;

    Linklist p;

    p = (*L);

    while ((p->hj||p->qu)&&j<=i)

    {

        p=p->hj;

        j++;

    }

    *e=p->date;

    if (p->hj==NULL) {

        p->qu->hj=NULL;

        free(p);

    }

    else {p->qu->hj=p->hj;

    p->hj->qu=p->qu;

        free(p);}

}

int main(int argc, const char * argv[])

{

    Linklist l,s,h;

    printf("输入你要输入多少个数据\n");

    int many;

    scanf("%d",&many);

    chuangjian(&l, many);

    s=l;h=l;

    shuchu(l);

    

    int i=8;

    printf("输入你要查找第多少个数据\n");

    int t;

    scanf("%d",&t);

    chazhao(l,t, &i);

    printf("查找数据为%d\n",i);

    

    printf("输入修改位置,替换数据\n");

    int place,sold;

    scanf("%d,%d",&place,&sold);

    genggai(&l,place,sold);

    

    printf("输入添加第几个位置,添加数据\n");

    int p,m;

    scanf("%d,%d",&p,&m);

    tianjia(&s, p, m);

    

    printf("输入删除第几个位置\n");

    int w;

    scanf("%d",&w);

    shanchu(&h,w,&i);

    while (h->hj!=NULL) {

        h=h->hj;

        printf("%d   ",h->date);

    }

    printf("\n");

    printf("删除数据是%d\n",i);

    return 0;

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值