선형 표 의 체인 저장 과 실현
#include  
#include  
#define OK 1
#define ERROR -1
#define MAX_SIZE 20
typedef int ElemType;  노드 의 구조 체
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode;  링크 초기 화
ElemType Init_LNode(LNode *L)
{
    int a = -1;
    L->data = 0;
    L->next=NULL;
    if(L->next == NULL)
    {
        a = OK;
    }
    return a;
}  두 번 째 i 위치 에 e 삽입
ElemType Insert_LNode(LNode *L, int i, ElemType e)
{
    int count = 0;
    LNode *p;
    LNode *q;
    int a = -1;
    p = L;
    while(p->next != NULL && count < i - 1)
    {
        count++;
        p = p->next;
    }
    if(i > count + 1)//     i,           ,    
    {
        printf("overflow
");
    }
    else//      
    {
        q= (LNode*) malloc( sizeof(LNode));
        q->data = e;
        q->next = p->next;
        p->next = q;
        a= OK;
    }
    return a;
}  원소 위치 i 에 따라 원소 조회
ElemType Query(LNode *L, int i, ElemType count)//count     
{
    LNode *p = L;
    if(i <= 0 || i > count)//  
    {
        printf("Irregular position or this element does not exist!
");
    }
    else
    {
        int j;//   i ,   i   
        for(j = 0; j < i; j++)
        {
            p = p->next;
        }
        printf("The NO.%d number is :%d
", i, p->data);
    }
    return OK;
}  링크 i 번 째 요소 삭제
ElemType Delete_LNode(LNode *L, int i, int count)//count     
{
    LNode *p, *q;
    p = L;
    if(i <= 0 || i > count)
    {
        printf("This element does not exist!
");
        return ERROR;
    }
    else
    {
        int j;
        for(j = 0; j < i; j++)
        {
            q = p;
            p = p->next;
        }
        q->next = p->next;
        free(p);
    }
    return OK;
}  링크 를 옮 겨 다 니 며 원소 개 수 를 구하 다.
int Travel(LNode *p)
{
    int count = 0;
    while(p->next != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}  출력 링크 요소
void OutPut_LNode(LNode *L, ElemType count)
{
    LNode *p;
    int i = count - 1;
    p = L->next;
    while(p != NULL)
    {
        printf("The No.%d number is :%d
", count-i ,p->data);
        i--;
        p = p->next;
    }
}  두 개의 질서 있 는 링크 를 연결 한 후에 도 질서 가 있 습 니 다.
ElemType ListCombine(LNode *L1, LNode *L2, LNode *L3)
{
    int i;
    int a = 0;//    
    int pos = 0;//     
    int value = 0;//    
    int count = 0;//L3   
    LNode *p;
    LNode *q;
    //L1    
    printf("L1:");
    scanf("%d", &a);
    for(i = 0; i < a; i++)
    {
        printf("position:");
        scanf("%d", &pos);
        printf("value:");
        scanf("%d", &value);
        Insert_LNode(L1, pos, value);
    }
    //L2    
    printf("L2:");
    scanf("%d", &a);
    for(i = 0; i < a; i++)
    {
        printf("position:");
        scanf("%d", &pos);
        printf("value:");
        scanf("%d", &value);
        Insert_LNode(L2, pos, value);
    }
    //  L1 L2
    printf("----L1----
");
    count = Travel(L1);
    OutPut_LNode(L1, count);
    printf("----L2----
");
    count = Travel(L2);
    OutPut_LNode(L2, count);
    //  L1 L2
    p = L1->next;
    q = L2->next;
    i = 1;
    while(p != NULL && q != NULL)
    {
        if(p->data < q->data)
        {
            Insert_LNode(L3, i, p->data);
            p = p->next;
            i++;
        }
        else if(p->data > q->data)
        {
            Insert_LNode(L3, i, q->data);
            q = q->next;
            i++;
        }
        else if(p->data == q->data)
        {
            Insert_LNode(L3, i, p->data);
            p = p->next;
            i++;
            Insert_LNode(L3, i, q->data);
            q = q->next;
            i++;
        }
    }
              L3  
    while(1)
    {
        if(p != NULL && q == NULL)
        {
            Insert_LNode(L3, i, p->data);
            p = p->next;
            i++;
        }
        else if(q != NULL && p == NULL)
        {
            Insert_LNode(L3, i, q->data);
            q = q->next;
            i++;
        }
        else
        {
            break;
        }
    }
    printf("----L3----
");
    count = Travel(L3);
    printf("%d----
",count);
    OutPut_LNode(L3, count);
    return 0;
}  main 방법
int main()
{
    int i = 0;
    int a;//     
    int b;//    
    int j;//     
    int count = 0;
    int res = -1;
    LNode L;
    printf("1.Initialize
2.Insert Data
3.Travel
4.OutPut LNode
5.Delete
6.Query
7.combine
");
    while(1)
    {
        printf("Please enter the operation code :");
        scanf("%d", &b);
        printf("
");
        switch(b)
        {
            case 1:
                //   
                res = Init_LNode(&L);
                if(res == -1)
                {
                    printf("error
");
                }
                else
                {
                    printf("success
");
                }
                i++;
                break;
            case 2:
                if(i == 0)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    printf("Please enter number:");
                    scanf("%d", &a);
                    printf("
");
                    printf("Please enter place:");
                    scanf("%d", &j);
                    printf("
");
                    res = -1;
                    res = Insert_LNode(&L, j, a);
                    printf("
");
                    if(res == -1)
                    {
                        printf("Error
");
                    }
                    else
                    {
                        printf("Success
");
                    }
                    printf("--------------
");
                }
                break;
            case 3:
                if(i == 0)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    //       
                    count = Travel(&L);
                    printf("The count is %d
",count);
                }
                break;
            case 4:
                if(i == 0)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    //    
                    count = Travel(&L);
                    OutPut_LNode(&L, count);
                }
                break;
            case 5:
                if(i == 0)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    count = Travel(&L);
                    printf("***********Delete*********
");
                    printf("Please enter the place:");
                    scanf("%d", &a);
                    Delete_LNode(&L, a, count);
                }
                break;
            case 6:
                if(i == 0)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    printf("***********Query************
");
                    count = Travel(&L);
                    printf("Please enter the place:
");
                    scanf("%d", &a);
                    Query(&L, a, count);
                }
                break;
            case 7:
                if(i == -1)
                {
                    printf("Please initialize a LinkNode first!
");
                }
                else
                {
                    LNode L1;
                    LNode L2;
                    LNode L3;
                    //   
                    Init_LNode(&L1);
                    Init_LNode(&L2);
                    Init_LNode(&L3);
                    ListCombine(&L1, &L2, &L3);
                }
                break;
        }
        if(b > 7)
        {
            printf("Operation coding error!
Please enter again!
");
        }
    }
    return 0;
}  이상 은 링크 의 열 조작 입 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
선형 표 의 체인 저장 과 실현체인 식 저장 의 모든 노드 는 두 개의 도 메 인 을 포함 하 는데 그 중에서 데이터 요소 정 보 를 저장 하 는 도 메 인 을 데이터 도 메 인 이 라 고 부른다.직접 후계 저장 위 치 를 포인터 필드 라 고 합...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.