Road to Coder _LinkList

周次算法

學習時間windows

新編寫代碼行數ide

博客量(篇)函數

學到知識點學習

         

第6周spa

 

約400code

1blog

單鏈表操做排序

 

#include"stdio.h"
#include "stdafx.h"
#include"stdlib.h"
#include"windows.h"


#pragma warning(disable :4996)

typedef int Elemtype;

typedef struct Node
{
    Elemtype date;
    struct Node* next;

}Node, *LinkList;

int ListLength(LinkList L);                                        //計算鏈表長度
void InitList(LinkList L);                                        //初始化列表
void CreateFromHead(LinkList L);                                //頭插法建立單鏈表
void CreateFromTail(LinkList L);                                //尾插法建立單鏈表
void SearchDate(LinkList L, Elemtype e);                        //單鏈表查找_按內容
void Searchsequ(LinkList L, int e);                                //單鏈表查找_按序號
void Insert(LinkList L, int local, Elemtype e);                    //單鏈表插入
void Myprint(LinkList L);                                        //單鏈表輸入函數
void MyprintLC(LinkList L);                                        //合併鏈表後的LC的輸出函數
int DelList(LinkList L, int local);                                //刪除
void mergeLinkList(LinkList LA, LinkList LB, int len);            //合併鏈表
void Reverse(LinkList L);                                        //頭插法逆置
void Divide(LinkList L);

int main()
{
    LinkList head;
    LinkList LB;
    int i;
    head = (Node*)malloc(sizeof(Node));
    LB = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    LB->next = NULL;
    InitList(head);
    //CreateFromHead(head);
    CreateFromTail(head);
    Myprint(head);
    printf("\n逆置算法:");
    Reverse(head);
    Myprint(head);

    printf("\n拆分算法:\n");
    Divide(head);
    /*
    Elemtype seek = 0;
    printf("\n請輸入查找的數值:");
    scanf("%d", &seek);
    SearchDate(head, seek);
    printf("\n請輸入查找的數值的序號:");
    scanf("%d", &seek);
    Searchsequ(head, seek);
    */
    int local;
    int insertelem;
    printf("\n請輸入要插入的位置和值:");
    scanf("%d%d", &local, &insertelem);
    Insert(head, local, insertelem);
    Myprint(head);
    Elemtype *e = 0;

    int result = -1;
    printf("\n請輸入要刪除的函數的序號:");
    scanf("%d", &local);
    result = DelList(head, local);
    if (result != 0)
    {
        printf("\n刪除成功!刪除的元素爲%d\n", result);
    }
    else
        printf("\n刪除失敗,刪除位置不合法!\n");

    printf("\nLA:\t");
    Myprint(head);
    /*            //頭插法
    Node *r;
    Elemtype temp;
    printf("請輸入時按從大到小的順序輸入,爲了後面的合併排序功能,由於只能合併2個升序鏈表");
    printf("\n請輸入LB的的值(數量與LA一致):");
    result = ListLength(head);
    for (i = 0; i < result; i++)
    {
    scanf("%d", &temp);
    r = (Node*)malloc(sizeof(Node));
    r->date = temp;
    r->next = LB->next;
    LB->next = r;
    }
    */
    Node *r, *s;
    Elemtype temp;
    r = LB;
    //printf("請輸入時按從大到小的順序輸入,爲了後面的合併排序功能,由於只能合併2個升序鏈表");
    printf("\n請輸入LB的的值(數量與LA一致):");
    result = ListLength(head);
    for (i = 0; i < result; i++)
    {
        scanf("%d", &temp);

        s = (Node*)malloc(sizeof(Node));
        s->date = temp;
        r->next = s;
        r = s;
    }
    r->next = NULL;
    printf("\nLB:\t");
    Myprint(LB);
    printf("\n\n按任意鍵進入合併單鏈表!");
    system("pause");

    mergeLinkList(head, LB, result);
    system("pause");
    return 0;
}

void InitList(LinkList L)
{
    L = (Node*)malloc(sizeof(Node));
    L->next = NULL;
}
void CreateFromHead(LinkList L)                        //頭插法建表
{
    printf("\n");
    printf("------------------------------ 單鏈表練習 --------------------------------------\n");
    Node *s;
    int  date;
    int flag = 1;
    printf("\n請輸入若干個整數(按0結束):");
    while (flag)
    {
        scanf("%d", &date);
        if (date != 0)            //按0退出
        {
            s = (Node*)malloc(sizeof(Node));
            s->date = date;
            s->next = L->next;
            L->next = s;
        }
        else
            flag = 0;
    }
}
void CreateFromTail(LinkList L)
{
    printf("\n");
    printf("------------------------------ 單鏈表練習 --------------------------------------\n");
    Node *r, *s;
    int date;
    int flag = 1;
    r = L;
    printf("\n請輸入若干個整數(按0結束):");
    while (flag)
    {
        scanf("%d", &date);
        if (date != 0)
        {
            s = (Node*)malloc(sizeof(Node));
            s->date = date;
            r->next = s;
            r = s;
        }
        else
        {
            flag = 0;
            r->next = NULL;
        }
    }
}

void SearchDate(LinkList L, Elemtype e)                //按內容查找
{
    Node*p;
    p = L->next;
    while (p != NULL)
    {
        if (p->date == e)
            break;

        p = p->next;
    }
    if (p == NULL)
        printf("No Find\n");
    else
        printf(" Find\n");
}

void Searchsequ(LinkList L, int e)                //按序號查找
{
    Node*p;
    p = L;
    int i;
    for (i = 0; i < e; i++)
    {
        p = p->next;
    }
    if (p == NULL)
    {
        printf("查找位置不合法!");
    }
    else
        printf("第%d個元素是%d\n", e, p->date);
}

void Insert(LinkList L, int local, Elemtype e)            //插入函數
{
    Node *s;
    s = (Node*)malloc(sizeof(Node));
    s->date = e;

    Node *p;
    p = L;
    int i;
    for (i = 1; i < local; i++)
    {
        p = p->next;
    }
    s->next = p->next;
    p->next = s;


}

void Myprint(LinkList L)                //輸出函數
{
    Node *p;
    p = L;
    p = p->next;
    int i;
    int count = ListLength(L);
    for (i = 0; i < count; i++)
    {
        printf("%d\t", p->date);
        p = p->next;
    }
}
void MyprintLC(LinkList L)                //輸出函數
{
    Node *p;
    p = L;
    p = p->next;
    int i;
    int count = ListLength(L);
    while (p != NULL)
    {
        printf("%d\t", p->date);
        p = p->next;
    }
}
int ListLength(LinkList L)                //單鏈表長度
{
    int count = 0;
    Node *p;
    p = L;
    p = p->next;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

int DelList(LinkList L, int local)
{
    Node *p, *r;
    int i;
    p = L;
    Elemtype e;
    for (i = 1; i < local; i++)
    {
        if (p->next == NULL)
        {
            return 0;
        }
        p = p->next;
    }

    r = p->next;
    p->next = r->next;
    e = r->date;
    free(r);
    return e;
}

void mergeLinkList(LinkList LA, LinkList LB, int len)            //合併鏈表
{
    Node *pa, *pb, *r;
    LinkList LC;
    int i;
    pa = LA->next;
    pb = LB->next;

    LC = LA;
    LC->next = NULL;
    r = LC;

    while (pa != NULL && pb != NULL)
    {
        if (pa->date <= pb->date)
        {
            r->next = pa;
            r = pa;
            pa = pa->next;
        }
        else
        {
            r->next = pb;
            r = pb;
            pb = pb->next;
        }
        if (pa)
            r->next = pa;
        else
            r->next = pb;
    }
    MyprintLC(LC);
}


void Reverse(LinkList L)
{
    Node *p,*s;
    LinkList LB;
    int count = 0,i;
    LB=(Node*)malloc(sizeof(Node));
    LB->next = NULL;
    p = L;
    count = ListLength(L);
    for (i = 0;i < count;i++)
    {
        p = p->next;
        s = (Node*)malloc(sizeof(Node));
        s->date = p->date;
        s->next = LB->next;
        LB->next = s;
    }
    p = L;
    for (i = 0;i < count;i++)
    {
        p = p->next;
        LB = LB->next;
        p->date = LB->date;
    }
    free(LB);
}


void Divide(LinkList L)
{
    int i,count;
    Node *p,*pa,*pb;
    LinkList LA, LB;
    LB = (Node*)malloc(sizeof(Node));
    LB->next = NULL;
    p = L->next;
    LA = L;
    LA->next = NULL;
    pa = LA;
    pb = LB;
while(p!=NULL)
{
    if (p->date % 2 == 0)
    {
        pb->next = p;
        pb = p;
        p = p->next;
    }
    else
    {
        pa->next = p;
        pa = p;
        p = p->next;
    }
}
    pa->next = NULL;
    pb->next = NULL;
    printf("\nLA:");
    count = ListLength(LA);
    pa = LA->next;
    pb = LB->next;
    for (i = 0;i < count;i++)
    {
        printf("%d\t",pa->date);
        pa = pa->next;
    }
    printf("\nLB:");
    count = ListLength(LB);

    for (i = 0;i < count;i++)
    {
        printf("%d\t", pb->date);
        pb = pb->next;
    }
    system("pause");
}
相關文章
相關標籤/搜索