双链表程序

#include<stdio.h>
#include<iostream>
#include<malloc.h>
#define NULL 0

typedef struct node
{
    int data;
    struct node*prio;
    struct node*next;

}ElemSN;

//创建双向链表
ElemSN*Creatlink(int a[],int n)
{
    ElemSN*h, *p, *q; int i ;
    p=h = (ElemSN*)malloc(sizeof(ElemSN));
    h->data = a[0];
    h->prio = NULL;
    for (i = 1; i < n; i++)
    {
        q = (ElemSN*)malloc(sizeof(ElemSN));
        q->data = a[i];
        p->next = q;
        q->prio = p;
        q->next = NULL;
        p = q;

    }
    return h;
}
//删除链表中的结点
ElemSN*Delnode(ElemSN*h,int x)
{
    ElemSN*p, *q; q = h;
        for (p = h; p&&p->data - x;q = p,p = p->next);
        {
            if (p - h&&p->next!=NULL)
            {
                q->next = p->next;
                q = p->next->prio;
                free(p);
            }
             else if  (p->next == NULL)
            {
                q->next = NULL;
                free(p);

            }
            else
            {
                h = h->next;
                h->prio = NULL;
                free(p);
            }
        }
        return h;
}
//插入结点
ElemSN*InsertNode(ElemSN*h, ElemSN*s)
{   
    ElemSN*p, *q; q =h ;
    for (p = h; p && (p->data < s->data); q = p, p = p->next);
        if (p == h)
        {
            s->next = h;
            s->prio = NULL;
            h = s;
        }
        else if (!p)
        {
            s->next=q->next;
            q->next = s;
            s->prio = q;
        }
        else
        {
            s->next = q->next;
            q->next = s;
            s->prio = q;
            p->prio = s;

        }
    return h;
}

 
//打印链表
void Printlink(ElemSN*h)
{
    ElemSN*p;
    for (p = h; p; p = p->next)
    {
        printf("%d", p->data);
    }
    system("pause");
}

void main()
{
    int a[5] = { 1,3,5,7,9 };
    //int    x;
    ElemSN*head,*h,*s;
    head = Creatlink(a,5);
    Printlink(head);
    //printf("请输入要删除节点的值\n");
    //std::cin >> x;
    //head=Delnode(head, x);
    //Printlink(head);
    s = (ElemSN*)malloc(sizeof(ElemSN));
    printf("请输入插入节点的值:\n");
    std::cin >> s->data;
    s->prio = NULL; s->next = NULL;
    h=InsertNode(head, s);
    Printlink(h);
    getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值