实现双向链表 C语言

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

int len;

//定义双向链表的节点
typedef struct Node
{
    int data;
    struct Node *prior;
    struct Node *next;
}Node;

//初始化一个链表的节点、
Node* create_node(void)
{
    Node *p;
    p = (Node*)malloc(sizeof(Node));
    if(p == NULL)
    {
        printf("动态内存分配失败!\n");
        exit(0);
    }
    scanf("%d",&(p->data));
    p->prior = NULL;
    p->next = NULL;
    return (p);
}

//建立含有N个结点的双向链表
Node* create_list(int n)
{
    Node *p = NULL,*new1,*head = NULL;
    int i;
    if(n >= 1)                    //结点的个数 >= 1 的时候,先生成第一个结点
    {
        new1 = create_node();
        head = new1;
        p = new1;
    }
    for(i = 2;i <= n;i++)    //生成第一个结点以后的结点,并建立双向链表的关系
    {
        new1 = create_node();
        p->next = new1;
        new1->prior = p;
        p = new1;
    }
    len = n;
    if(n >= 1)
        return (head);
    else
        return 0;
}



//链表的长度
int len_list(int len)
{
    return len;
}
 
//定位到链表的任意位置
Node* pos_list(Node *head,int n)
{
    int i = 1;
    Node *p = NULL;
    if(i <= n)
    {
        p = head;
        for(i = 2;i <= n;i++)
            p = p->next;
    }
    return p;
}

//按序号查找元素
void findnum(Node *head)
{
    printf("请输入要查找的序号:");
    int num;
    scanf("%d",&num);
    printf("查找到的值为%d\n",pos_list(head, num)->data);
}

//按值查找元素
void findval(Node *head)
{
    printf("请输入要查找的值:");
    int val,i = 1;
    scanf("%d",&val);
    while(head->data!=val)
    {
        head = head->next;
        i ++;
    }
    printf("该值的序号为%d\n",i);
}

//正向遍历一个链表
void out_front_list(Node *head)
{
    if(head == NULL)
    {
        printf("输入的链表信息有误,链表不存在!\n");
    }
    else
    {
        Node *p;
        p = head;
        while(p != NULL)
        {
            printf("%d  ",p->data);
            p = p->next;
        }
    }
    printf("\n");
}
 
//在链表的头部插入结点
Node* start_insert_list(Node *head)
{
    Node *p;
    p = create_node();
    p->next = head;
    head->prior = p;
    head = p;
    len++;
    return (p);
}
 
//在链表的尾部插入结点
Node* end_insert_list(Node *head)
{
    int n;
    n = len_list(len);
    Node *p,*new1;
    new1 = create_node();
    p = pos_list(head,n);
    p->next = new1;
    new1->prior = p;
    len++;
    return (head);
}

//插入到任意位置之前
Node* insert_befor_list(Node *head)
{
    int a,newlen;
    Node *pos,*p;
    printf("请输入要插入结点的位置:");
    scanf("%d",&a);
    printf("请输入要插入的结点的值:");
    newlen = len_list(len);
    if(a > newlen)
    {
        head = end_insert_list(head);
    }
    else
    {
        if(a <= 1)
        {
            head = start_insert_list(head);
        }
        else
        {
            pos = pos_list(head,a);
            p = create_node();
            pos->prior->next = p;
            p->prior = pos->prior;
            p->next = pos;
            pos->prior = p;
        }
    }
    len++;
    return (head);
}

//删除头结点
Node* delect_start_list(Node *head)
{
    Node *pos;
    pos = head;
    head = head->next;
    head->prior = NULL;
    free(pos);
    len--;
    return(head);
}

//删除尾结点
Node* delect_end_list(Node *head)
{
    Node *p,*pos;
    int newlen;
    newlen = len_list(len);
    pos = pos_list(head,newlen);
    p = pos;
    p = p->prior;
    p->next = NULL;
    free(pos);
    len--;
    return (head);
}

//删除指定位置的节点
Node* delect_list(Node *head)
{
    int newlen,i;
    Node *pos;
    newlen = len_list(len);
    printf("请输入要删除结点的位置:\n");
    scanf("%d",&i);
    if(i <= 1)
        head = delect_start_list(head);
    else if(i >=newlen)
        head = delect_end_list(head);
    else
    {
        pos =pos_list(head,i);
        pos->prior->next = pos->next;
        pos->next->prior = pos->prior;
        free(pos);
    }
    len--;
    return(head);
}

//删除链表
void deletelist(Node *head)
{
    head = delect_start_list(head);
    len --;
    for(int i = 2; i <= len; i ++)
    {
        Node *pos;
        pos =pos_list(head,i);
        pos->prior->next = pos->next;
        pos->next->prior = pos->prior;
        free(pos);
        len --;
    }
    printf("链表已删除\n");
}

int main()
{
    //int newlen;
    Node *head;
    
    printf("请输入要建立双向链表的长度:\n");
    scanf("%d",&len);
    printf("请为双向链表赋值:\n");
    head = create_list(len);
    printf("正向遍历双向链表:\n");
    out_front_list(head);
    head = insert_befor_list(head);
    printf("正向遍历双向链表:\n");
    out_front_list(head);
    head = delect_list(head);
    printf("正向遍历双向链表:\n");
    out_front_list(head);
    findnum(head);
    findval(head);
    deletelist(head);
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值