Demo entry 6722514

LinkList.cpp

   

Submitted by anonymous on Mar 23, 2018 at 04:13
Language: C++. Code size: 6.6 kB.

 	生成一个链表(头节点):
 	LNode* initList()
{
    LinkList HNode = new LNode;
    HNode->data = -1;
    HNode->nex = NULL;
    return HNode;
}
 	填充链表数据:
 	void CreateList(LinkList& L, int n)
{
    printf(/*...提示输入*/);
    int data;
    LNode *p, *q;
    for(int i=1; i<=n; i++)
    {
        scanf(/*...data...*/);
        // create a new node
        if(/*头节点之后没有节点*/)
            // 直接接上
        else
       		// 接到p的nex
        q = p;
    }
    p->nex = NULL;
}
 	清空链表:
 	void ClearList(LinkList& L)
{
    LNode *p = L->nex;
    L->nex = NULL;
    while(p)
    {
        // delete node
    }
}
 	判断链表是否为空:
 	bool ListEmpty(LinkList L)
{
    if(L->nex == NULL)
        return 1;
    return 0;
}
 	返回链表长度:
 	int ListSize(LinkList L)
{
    LNode *p = L;
    int cnt = 0;
    while(p->nex)
    {
        cnt++;
        p = p->nex;
    }
    return cnt;
}
 	打印链表:
 	// 1. 普通打印
void PrintList(LinkList L)
{
    printf("Result:\n");
    LNode *p = L->nex;
    if(p == NULL)
        printf("Linked List is empty\n");
    while(p)
    {
        printf("data: %d\n",p->data);
        p = p->nex;
    }
}
// 2. 多项式打印
void PrintList(LinkList L)
{
    printf("Result:\n");
    LNode *p = L->nex;
    if(p == NULL)
        printf("Linked List is empty\n");
    int flag = 0;
    while(p)
    {
        if(p->coef > 0 && flag)
        {
            printf("+%d^%d",p->coef,p->expn);
            flag = 1;
        }
        else
        {
            printf("%d^%d",p->coef,p->expn);
            flag = 1;
        }
        p = p->nex;
    }
    cout << endl;
}
// 3. 长整数打印
 	通过节点位置获取节点:
 	LNode* GetElemByIdx(LinkList L, int idx)
{
    if(!ListEmpty(L) && idx>=1 && idx <= ListSize(L))
    {
        LNode *p = L;
        for(int i=1; i<=idx; i++)
        {
            p = p->nex;
        }
        return p;
    }
    else
        printf("Error!\n");
    return NULL;
}
 	将节点插入到头:
 	void ListInsertToFront(LinkList& L, int data)
{
    LNode *p = new LNode;
    p->data = data;
    p->nex = L->nex;
    L->nex = p;
}
 	链表遍历到尾部:
 	LNode* ListToTail(LinkList L)
{
    LNode *p = L;
    while(p->nex)
    {
        p = p->nex;
    }
    return p;
}
 	将节点插入到尾:
 	void ListInsertToTail(LinkList& L, int data)
{
    LNode *p = new LNode;
    p->data = data;
    LNode *tail = ListToTail(L);
    tail->nex = p;
    p->nex = NULL;
}
 	通过节点的数据出现的第一次位置插入节点
 	void ListInsertByData(LinkList& L, int L_data, int data)
{
    LNode *p = L;
    LNode *newNode = new LNode;
    newNode->data = data;
    while(p->nex)
    {
        p = p->nex;
        if(p->data == L_data)
        {
            newNode->nex = p->nex;
            p->nex = newNode;
            break;
        }
    }
    if(p->nex == NULL)
    {
        newNode->nex = NULL;
        p->nex = newNode;
    }
}
 	删除节点:
 	void ListDeleteByData(LinkList& L, int data)
{
    LNode *p = L;
    LNode *q;
    int flag = 0;
    while(p->nex)
    {
        q = p;
        p = p->nex;
        if(p->data == data)
        {
            flag = 1;
            q->nex = p->nex;
            LNode *tmp = p;
            delete tmp;
        }
    }
    if(!flag)
        printf("No that data!\n");
}
 	两个多项式相加:
 	void Plus(LinkList& L1, LinkList& L2)
{
    LNode *p,*q,*r;
    p = L1, q = L2, r = L1;
    if(!ListEmpty(L1))
        p = p->nex;
    if(!ListEmpty(L2))
        q = q->nex;
    while(p && q)
    {
        if(p->expn > q->expn)
        {
            // r->nex = q;
        }
        else if(p->expn < q->expn)
        {
            // r->nex = p;
        }
        else if(p->expn == q->expn)
        {
            if(p->coef+q->coef != 0)
            {
                // p->coef += q->coef;
                // r->nex = p;
            }
            else
            {
                delete p
            }
            // nex and delete
        }
    }
    if(p)
    {
        // r connect p
    }
    if(q)
    {
        while(q)
        {
            // r connect q
        }
    }
    r->nex = NULL;
}
 	长整数相加:
 	void Add(char s1[], char s2[])
{
    printf("Recall: Add\n");
    Exchange(s1,s2);
    LinkList L1 = initList();
    LinkList L2 = initList();
    CreateList(L1,s1);
    CreateList(L2,s2);
    LNode* p = ListToTail(L1);
    LNode* q = ListToTail(L2);

    while(p->pre)
    {
        int sum = p->data + q->data;
        if(sum > 9)
        {
            p->data = sum - 10;
            p->pre->data += 1;
        }
        else
        {
            p->data = sum;
        }
        /*cout << p->data << endl;*/
        LNode *tmp = q;
        p = p->pre;
        q = q->pre;
        delete tmp;
    }
    int sum = p->data + q->data;
    if(sum > 9)
    {
        LNode *newNode = new LNode;
        newNode->data = 1;
        p->data = sum - 10;
        newNode->nex = p;
        newNode->pre = NULL;
        p->pre = newNode;
        L1->nex = newNode;
        /*cout << p->data << endl;
        cout << newNode->data << endl;*/
    }
    else
    {
        p->data = sum;
        L1->nex = p;
        /*cout << p->data << endl;*/
    }
    LNode *r = L1->nex;
    int i = 0;
    while(r)
    {
        s1[i++] = static_cast<char>(r->data+'0');
        r = r->nex;
    }
}
 	长整数相减:
 	bool Subtract(char s1[], char s2[])
{
    printf("Recall: Subtract\n");
    int flag1 = Exchange(s1,s2);
    LinkList L1 = initList();
    LinkList L2 = initList();
    CreateList(L1,s1);
    CreateList(L2,s2);
    LNode* p = ListToTail(L1);
    LNode* q = ListToTail(L2);
    while(p->pre)
    {
        int diff = p->data - q->data;
        if(diff < 0)
        {
            p->data = diff + 10;
            p->pre->data -= 1;
        }
        else
        {
            p->data = diff;
        }
        /*cout << p->data << endl;*/
        LNode *tmp = q;
        p = p->pre;
        q = q->pre;
        delete tmp;
    }
    int diff = p->data - q->data;
    p->data = diff;
    L1->nex = p;
    LNode *r = L1->nex;
    int i = 0, flag2 = 1;
    while(r)
    {
        if(r->data != 0)
        {
            flag2 = 0;
        }
        if(r->data==0 && flag2)
        {
            r = r->nex;
        }
        else
        {
            s1[i++] = static_cast<char>(r->data+'0');
            r = r->nex;
        }
    }
    if(flag1)
        return 1;
    else
        return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).