OJ 系列之【中级】双链表基本操作

#include 

#define null 0
#define MAXSIZE 50

struct strlnode {
    int data;
    struct strlnode *plast;
    struct strlnode *pnext;
};

void create(struct strlnode **p, int x)  /*创建双链表(表头节点)*/
{
    struct strlnode *q;

    q = (struct strlnode *)malloc(sizeof(struct strlnode));
    q->data = x;
    q->plast = null;
    q->pnext = null;

    *p = q;

    return;
}

void insertnode(struct strlnode **p, int i, int x) /* 在链表第i个位置插入数据等于x的节点 */
{
    /* 代码在这里实现 */
    if(!*p||!p)
        return;

    struct strlnode *ptemp=*p,*psave=*p;
    struct strlnode *t = NULL;
    int pnodeLen=0;

    while(ptemp->pnext!=NULL) {
        pnodeLen++;
        ptemp=ptemp->pnext;
    }
    if(i<0||i>pnodeLen+1)
        return;

    /*申请该节点的地址*/
    struct strlnode *q=(strlnode *)malloc(sizeof(strlnode));

    q->data=x;
    /*插入为第一个位置时候*/
    if(i==0) {
        q->pnext=psave;
        q->plast=NULL;
        psave->plast=q;
        *p=q;

        return;
    /*最后位置*/    
    } else if(i==pnodeLen+1) {
        q->pnext = NULL;
        q->plast = ptemp;
        ptemp->pnext = q;

        return;
    /*中间位置*/
    } else {
        int pnodeCur=0;

        while(pnodeCur!=i) {
            pnodeCur++;
            t = psave;
            psave = psave->pnext;
        }
        q->pnext=t->pnext;
        psave->plast=q;
        q->plast=t;
        t->pnext=q;

        return;
    }


}

void deletenode(struct strlnode **p, int i) /* 删除链表中第i个节点 */
{
    /* 代码在这里实现 */   
    if(!*p||!p)
        return;
    if(i<0)
        return;

    struct strlnode *ptemp=*p,*psave=*p;
    struct strlnode *t = NULL;
    int pnodeLen=0;

    while(ptemp->pnext!=NULL) {
        pnodeLen++;
        ptemp=ptemp->pnext;
    }
    if(i<0||i>pnodeLen)
        return;

    /*删除为第一个位置时候*/
    if(i==0) {
        if(pnodeLen==0) 
            *p = NULL;
        t = psave->pnext;
        t->plast = NULL;
        free(psave);
        psave = NULL;
        *p = t; /*注意这里,记得返回*/

        return;

    /*其它位置*/
    } else {
        int pnodeCur=0;

        while(pnodeCur!=i) {
            pnodeCur++;
            t = psave;
            psave = psave->pnext;
        }
        t->pnext = psave->pnext;
        (psave->pnext)->plast = t;
        free(psave);

        return;
    }
}

int getnodenum(struct strlnode **p)  /*获取链表中节点个数*/
{

    if(!*p||!p)
        return 0;

    struct strlnode *plasttemp = *p;;
    int nodenum = 1;
    /* 代码在这里实现 */
    while(plasttemp->pnext!=NULL) {
        nodenum ++;
        plasttemp = plasttemp->pnext;
    }

    return nodenum;
}

/* 使用链表实现大整数相加 */
void bignumberplus(struct strlnode **plus, struct strlnode **p, struct strlnode **q)
{
    int lenp = getnodenum(p);
    int lenq = getnodenum(q);
    /*初始化为0很重要,便于后面的处理*/
    int a[10000] = {0};
    int b[10000] = {0};
    /*看哪个整数更大*/
    int len = lenp > lenq ? lenp : lenq;
    struct strlnode *pl = *p;
    struct strlnode *ql = *q;
    /*将链表数据取出来,分别存入不同数组中*/
    for(int i = lenp - 1; i>= 0; i--) {
        a[i] = pl->data;
        pl = pl ->pnext;
    }
    for(int i = lenq - 1; i>= 0; i--) {
        b[i] = ql->data;
        ql = ql ->pnext;
    }
    for(int i = 0; i < len; i++) {
        a[i] = a[i] + b[i];
        if(a[i]>=10) {
            a[i+1] += a[i] / 10;
            a[i] %= 10;
        }
    }

    if(a[len] != 0)//最后进位后会多出来的
        len++;
    /*从高到低依次存储*/
    (*plus)->data = a[len -1];
    int j = 1;
    for(int  i = len - 2; i>= 0 ;i--) {
        int data = a[i];
        insertnode(plus, j, data);
        j++;
    }

    return;
}

/* 将数组写入链表中,链表中的数据的先后顺序和数组中的顺序要保持一致 */
void readtolnode(struct strlnode **p, int *a, int size) 
{
    int j = 0;
    int data = 0;
    struct strlnode *s = *p;

    s->data = *(a + (size-1));

    for(j = 2; j < (size+1); j++) {
        data = *(a + (size-j));
        insertnode(p, 0, data);
    }

    return;
}

/* 将链表写入数组中,数组中的数据的先后顺序和链表中的顺序要保持一致 */
void writetosqlist(int *a, struct strlnode *p)  
{
    int j = 0;
    struct strlnode *s = p;

    while(s != null) {
        *(a + j) = s->data;
        s = s->pnext;
        j++;
    }

    return;
}






你可能感兴趣的:(软件训练营,数据结构和算法)