1. 程式人生 > >資料結構-單鏈表操作

資料結構-單鏈表操作

//
//  main.c
//  Link_List
//
//  Created by Smallflyfly on 2018/11/9.
//  Copyright © 2018 fang. All rights reserved.
//

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

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode, *LinkList;

LinkList Creat_Link_List_Tail()
{
    int n, e;
    LNode *L, *p, *q;
    L = p = (LNode *)malloc(sizeof(LNode));
    p->next = NULL;
    printf("Enter the length of link list:");
    scanf("%d", &n);
    while(n--)
    {
        q = (LNode *)malloc(sizeof(LNode));
        scanf("%d", &e);
        q->data = e;
        q->next = p->next;
        p->next = q;
        p = q;
    }
    return L;
}

void Print_LinkList(LinkList L)
{
    LNode *p;
    p = L;
    if(p->next == NULL)
        printf("The linklist is empty.\n");
    else
    {
        for(p=L->next; p->next!=NULL; p=p->next)
            printf("%d ", p->data);
        printf("%d\n", p->data);
    }
}

int LinkList_Length(LinkList L)
{
    LNode *p;
    int len = 0;
    p = L->next;
    while(p!=NULL)
    {
        len ++;
        p = p->next;
    }
    
    return len;
}

int Get_element(LinkList L, int i)
{
    LNode *p;
    p = L;
    while(i--)
        p = p->next;
    
    return p->data;
}

void Insert_LinkList(LinkList &L, int i, int e)
{
    LNode *p, *q;
    p = L;
    q = (LNode *)malloc(sizeof(LNode));
    i--;
    while(i--)
        p = p->next;
    q->data = e;
    q->next = p->next;
    p->next = q;
}

void Delete_ith_element(LinkList &L, int i)
{
    LNode *p, *q;
    q = p = L;
    while(i--)
    {
        q = p;
        p = p->next;
    }
    q->next = p->next;
    free(p);
}

int Find_Elem(LinkList L, int e)
{
    LNode *p;
    p = L->next;
    while(p && p->data!=e)
        p = p->next;
    if(p)
        return 1;
    else
        return 0;
}

LinkList Merge_LinkList(LinkList L1, LinkList L2)
{
    LNode *L, *p, *q, *r;
    r = L = (LNode *)malloc(sizeof(LNode));
    p = L1->next;
    q = L2->next;
    while(p && q)
    {
        if(p->data < q->data)
        {
            r->next = p;
            p = p->next;
        }
        else
        {
            r->next = q;
            q = q->next;
        }
        r = r->next;
    }
    while(p)
    {
        r->next = p;
        p = p->next;
    }
    while(q)
    {
        r->next = q;
        q = q->next;
    }
    
    return L;
}

int main()
{
    int length, i, e;
    LinkList L;
    L = Creat_Link_List_Tail();
    printf("The creat link list is:\n");
    Print_LinkList(L);
    length = LinkList_Length(L);
    printf("The length of the link list is %d.\n", length);
    printf("Get the ith element\n");
    scanf("%d", &i);
    if(i<1 || i>length)
        printf("Invalid i\n");
    else
    {
        e = Get_element(L, i);
        printf("The ith element is %d.\n", e);
    }
    printf("Insert a element to the link list.\n");
    printf("Enter the place and the element to insert the link list.\n");
    
    scanf("%d %d", &i, &e);
    if(i<1 || i>length)
        printf("Invalid i\n");
    Insert_LinkList(L, i, e);
    printf("After insert the element.\n");
    Print_LinkList(L);
    printf("Next delete element.\n");
    printf("Enter the ith element.\n");
    scanf("%d", &i);
    Delete_ith_element(L, i);
    Print_LinkList(L);
    printf("Enter a elenemt to find:");
    scanf("%d", &e);
    if(Find_Elem(L, e))
        printf("Element is in the linklist.\n");
    else
        printf("No found element.\n");
    LinkList L1, L2;
    L1 = Creat_Link_List_Tail();
    L2 = Creat_Link_List_Tail();
    L = Merge_LinkList(L1, L2); //L1 L2 must ordered
    Print_LinkList(L);
    
    
    return 0;
}