Two forms of linear form definition related operation (single linked list and sequence table)

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#pragma warning(disable:4996)
#define ERROR 0
#define OK 1
#define MAXSIZE 100

typedef int ElemType;
typedef int Status;
typedef struct LNode;


typedef struct {
    ElemType *elem;
    int length;
    int listsize;
}SqList;

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

Status InitSqlist(SqList &L) {//初始化顺序表
    L.elem = new ElemType[MAXSIZE];
    if (!L.elem) return ERROR;
    L.length = 0;
    L.listsize = MAXSIZE;
    return OK;
}
Status InitLinkList(LinkList &L) {//初始化单链表
    L = (LNode*)malloc(sizeof(LNode));
    if (!L)
        return ERROR;
    L->next =NULL;
     return the OK; 
} 

the Status addLinkList (LinkList & L, LNode * P) { // add data to a single linked list 
    LNode * E; 
    E = L;
     the while (E-> Next) { 
        E = E-> Next; 
    } 
    E -> Next = P; 
    P -> Next = NULL;
     return the OK; 
} 

the Status showLinkList (LinkList & L) { // display data a single list 
    the printf ( " list data sequence is: \ n- " ); 
    LNode * E = L ;
     the while(E-> Next) 
    { 
        the printf ( " % D " , E-> next-> Data); 
        E = E-> Next; 
    } 
    the printf ( " \ n- " );
     return the OK; 
} 
the Status showSqlist (SqList & L) { // display data sequence table of 
    the printf ( " order table data is: \ n- " );
     for ( int I = 0 ; I <L.length; I ++ ) 
    { 
        the printf ( " % D " , L.elem [I]) ;
    }
    printf("\n");
    return OK;
}
Status tranLinkList(LinkList &L) {//就地逆置单链表
    LNode *p,*q,*e;
    if (!L->next) return ERROR;
    p = L;
    q = p->next->next;
    p->next->next = NULL;
    while (q) {
        e = q->next;
        q->next = p->next;
        p->next = q;
        q = e;
    }    
    return OK;
}

The Status unitSqList (SqList & a, SqList & b, SqList & c) { // the sequence table the same number of a and b in ascending order into the order table (c), tables a and b in the data increment does not like 
    int I, J , K; 
    I = 0 ; 
    J = 0 ; 
    K = 0 ;
     the while ( to true ) {
         IF (I> J a.length ||> to b.length) {
             return the OK; 
        } 
        IF (a.elem [I]> B .elem [J]) { 
            J ++ ;
             Continue ; 
        } 
        IF (a.elem [I] < b.elem [J]) {
            i++;
            continue;
        }
        if (a.elem[i] == b.elem[j]) {
            j++;
            c.elem[k] = a.elem[i];
            c.length++;
            k++;
            continue;
        }
        
        
    }
    

}
Status unitLinkList(LinkList &a, LinkList &b, LinkList &c) {
    LNode *p, *q, *r;
    p = a->next;
    q = b->next;
    while (true)
    {
        if (!p || !q) {
            return OK;
        }
        if (p->data > q->data) {
            q = q->next;
            continue;
        }
        if (p->data < q->data) {
            p = p->next;
            continue;
        }
        if (p->data == q->data) {
            r = (LNode*)malloc(sizeof(LNode));
            r->data = p->data;
            addLinkList(c, r);
            q = q->next;
            continue;
        }
    }
}

void main() {
    LinkList L,M,N;
    InitLinkList(L);
    InitLinkList(M);
    InitLinkList(N);
    int num1[10] = {1,2,3,4,5,6,7,8,9,10};
    int num2[10] = { 3,4,5,6,7,9,10,11,12 ,13};
    for (int i = 0; i < 10; i++) {
        LNode *p = (LNode*)malloc(sizeof(LNode));
        LNode *q = (LNode*)malloc(sizeof(LNode));
        p->data = num1[i];
        q->data = num2[i];
        addLinkList(L, p);
        addLinkList(M, q);
    }
    unitLinkList(L, M, N);
    showLinkList(L);
    showLinkList(M);
    showLinkList(N);


    
    SqList A,B,C;
    InitSqlist(A);
    InitSqlist(B);
    InitSqlist(C);
    for (int j = 0; j < 10; j++) {
        A.elem[j] = num1[j];
        A.length++;
        B.elem[j] = num2[j];
        B.length++;
    }
    unitSqList(A, B, C);
    showSqlist(A);
    showSqlist(B);
    showSqlist(C);
    


}

 

Guess you like

Origin www.cnblogs.com/chenglin520/p/jayCling.html