双向链表基本操作(C语言)

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

//双向链表
typedef int Elemtype;
typedef struct DNode
{
    Elemtype data;
    struct DNode *prior,*next;
} DNode,*DLinkList;


//尾插法创建链表
DLinkList CreateList()
{
    int x;
    DLinkList head=(DLinkList)malloc(sizeof(DLinkList));//创建双向链表,头节点
    DNode* p=head,*s;//p为遍历节点,s为当前节点
    p->prior=NULL;
    printf("创建链表,输入9999表示结束\n");
    scanf("%d",&x);
    while(x!=9999)
    {
        s=(DNode*)malloc(sizeof(DNode*));
        s->data=x;
        s->prior=p;
        p->next=s;
        p=s;
        scanf("%d",&x);
    }
    s->next=NULL;//不要忘记制空尾指针
    return head;//返回头节点
}

//遍历链表
void PrintList(DLinkList L)
{
    DNode *p;
    p=L->next;//找到头节点下一个节点
    printf("链表元素如下:\n");
    while(p)
    {
        printf("%d<---->",p->data);
        p=p->next;
    }
    printf("\n");
}

//双链表头插法
DLinkList InsertListHead(DLinkList L,Elemtype e)
{
    DNode *p=(DNode*)malloc(sizeof(DNode*));
    p->data=e;
    p->prior==L;
    p->next=L->next;
    if(p->next!=NULL)
    {
        L->next->prior=p;
    }
    L->next=p;
    return L;
}
//队尾插入
DLinkList InsertListTail(DLinkList L,Elemtype e)
{
    DNode *s=(DNode*)malloc(sizeof(DNode*));//s为待插入节点
    DNode *p=L->next;//p为遍历节点
    while(p->next)
    {
        p=p->next;
    }
    p->next=s;
    s->data=e;
    s->prior=p;
    s->next=NULL;
    return L;
}

//获取链表长度
int GetLength(DLinkList L)
{
    int len=0;
    DNode *r=(DNode*)malloc(sizeof(DNode*));
    r=L->next;
    while(r)
    {
        r=r->next;
        len++;
    }
    return len;
}

//在指定位置插入节点
bool  InsertPosition(DLinkList L,int pos,Elemtype e)
{

    if(pos<1||pos>GetLength(L)+1)return false;
    DNode *r,*n;//r遍历节点
    n=(DNode*)malloc(sizeof(DNode*));//插入节点
    n->data=e;
    n->next=NULL;
    r=L;
    while(--pos>0)
    {
        r=r->next;
    }

    if(r->next==NULL) //在结尾插入
    {
        r->next=n;
        n->prior=r;
        return true;
    }
    n->next=r->next;
    r->next->prior=n;
    r->next=n;
    n->prior=r;
    return true;
}
//删除指定节点元素
bool DeletePosition(DLinkList L,int pos,Elemtype *e)
{
    if(pos<1||pos>GetLength(L))return false;
    DNode *r=L,*p;//r遍历节点,p为待删除节点
    while(--pos)
    {
        r=r->next;
    }
    p=r->next;
    *e=p->data;
    if(p->next==NULL)
    {
        r->next=NULL;
        free(p);
        return true;
    }
    r->next=p->next;
    p->next->prior=r;
    free(p);
    return true;

}
int main()
{
    DLinkList L=CreateList();
    PrintList(L);
    printf("头插入节点\n");
    InsertListHead(L,-1);
    PrintList(L);
    printf("尾插法插入节点\n");
    InsertListTail(L,-1);
    PrintList(L);
    printf("链表长度  %d\n",GetLength(L));
    printf("在指定位置插入元素\n");
    InsertPosition(L,5,-99);
    PrintList(L);
    printf("删除指定位置元素\n");
    int *e;
    DeletePosition(L,5,e);
    printf("删除元素为:%d\n",*e);
    PrintList(L);



    return 0;
}

  • 9
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值