The Linknode for Linear table | Data

简介: The Code in Data book (5th Edition) from the 49 page to 52 page

The Code in Data book (5th Edition) from the 49 page to 52 page

#define MaxSize 50
typedef int ElemType;

typedef struct LNode {
    ElemType data;   //Saved element values
    struct LNode* next;  //Push to the next node
} LinkNode;  //Single-list node type

//Head-inserting
void CreateListF(LinkNode *&L, ElemType a[], int n) {
    LinkNode *s;
    L = (LinkNode * )malloc(sizeof(LinkNode));
    L->next = NULL; //Create a head node, it's next domain is set to NULL
    for (int i = 0; i < n; i++) {   //Loop create data node s
        s = (LinkNode * )malloc(sizeof(LinkNode));
        s->data = a[i]; //Assignment
        s->next = L->next;  //Insert node s before the original first node, after the head node
        L->next = s;
    }
}

//Tail-insertion
void CreateListR(LinkNode *&L, ElemType a[], int n) {
    LinkNode *s, *r;
    L = (LinkNode *)malloc(sizeof(LinkNode));    //Create a head node
    r = L;  //r Always point to the end node
    for (int i = 0; i < n; i++) {   //Loop create node s
        s = (LinkNode *)malloc(sizeof(LinkNode));
        s->data = a[i]; //Assignment
        r->next = s;    //Insert node s after node r
        r = s;
    }
    r->next = NULL;        //Tail-insertion it's next domain set to NULL
}


//Initialization Linknode
void InitList(LinkNode*& L) {
    L = (LinkNode*)malloc(sizeof(LinkNode));
    L->next = NULL; //Create Head-inserting, it's next domain set to NULL
}

//Destroyed Linknode
void DestroyList(LinkNode*& L) {
    LinkNode *pre = L, * p = L->next;   //pre points to node p's forward node
    while (p != NULL) { //Scan Linknode L
        free(pre);  //Release pre node
        pre = p;    //pre,p sync and move a node
        p = pre->next;
    }
    free(pre);  //At the end of the loop p is NULL, pre points to the end node to release it
}

//Determine if a linear table is empty
bool ListEmpty(LinkNode* L) {
    return L->next == NULL;
}

//Calculation length of the Linknode
int ListLength(LinkNode* L) {
    int n = 0;
    LinkNode *p = L;    //p point to Head-inserting,set n is 0
    while (p->next != NULL) {   //Not empty, traverse is turn
        n++;
        p = p->next;
    }
    return n;   //return L's length n 
}

//Output Linknode
void Display(LinkNode* L) {
    LinkNode* p = L->next;  //p point to Head-inserting
    while (p != NULL) { //Not empty, traverse is turn
        printf("%d ", p->data);
        p = p->next;    //p move to next node
    }
    printf("\n");
}

//Find a data element value
bool GetData(LinkNode *L, int i, int &e) {
    int j = 0;
    LinkNode *p = L;  //p point to Head-inserting
    if (i <= 0) {   //If i error return false
        return false;
    }
    while (j < i && p != NULL) {    //find the ith node p
        j++;
        p = p->next;
    }
    if (p == NULL) {    //If doesn't
        return false;
    }
    else {  //If exust return true
        e = p->data;
        return true;
    }
}

//Find by element value
int LocateData(LinkNode *L, ElemType e) {
    int i = 1;
    LinkNode* p = L->next;  //p point to Head-insterting
    while (p != NULL && p->data != e) {  //Find for the data value with the serial number i
        p = p->next;
        i++;
    }
    if (p == NULL) {    //If doesn't
        return 0;
    }
    else {  //If exuse return number i
        return i;
    }
}

//Insert data elements
bool ListInsert(LinkNode *&L, int i, ElemType e) {
    int j = 0;
    LinkNode *p = L, *s;   //p point to Head-inserting
    if (i <= 0) {   //If i error return false
        return false;
    }
    while (j < i - 1 && p != NULL) {    //find the number i-1 node p
        j++;
        p = p->next;
    }
    if (p == NULL) {    //If not found
        return false;
    } else {  //Find number i-1 node p,insert new node and return true
        s = (LinkNode * )malloc(sizeof(LinkNode));
        s->data = e; //Create new node s, 创建新结点 s,value is data
        s->next = p->next;  //Head-inserting
        p->next = s;
        return true;
    }
}

//Delete data elements
bool ListDelete(LinkNode*& L, int i, ElemType &e) {
    LinkNode* p = L, * q;   //p point to Head-inserting
    if (i <= 0) {   //if i error return false
        return false;
    }
    int j = 0;  //set j is 0
    while (j < i - 1 && p != NULL) {    //Find number i node p
        j++;
        p = p->next;
    }
    if (p == NULL) {    //Not found
        return false;
    }
    else {  //Find number i-1 node p
        q = p->next;    //q point to number i th node
        if (q == NULL) {    //If number i node it doesn't
            return false;
        }
        e = q->data;
        p->next = q->next;  //Delete q node,number i-1 node point to number i+1 node
        free(q);    //Release q node
        return true;
    }
}
By Honework 2

如有侵权,请联系作者删除

目录
相关文章
|
6月前
【echarts报错】line series not exists,should be same with series name or data name
【echarts报错】line series not exists,should be same with series name or data name
237 0
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
|
JSON 数据格式
ValueError: With n_samples=0, test_size=0.15 and train_size=None, the resulting train set will be em
ValueError: With n_samples=0, test_size=0.15 and train_size=None, the resulting train set will be em
516 0
ValueError: With n_samples=0, test_size=0.15 and train_size=None, the resulting train set will be em
The Sqlist for Linear table | Data
The Code in Data book (5th Edition) from the 35 page to 37 page
99 0
The Double Linknode for Linear table | Data
The some code in Data book (5th Edition) from the 54 page to 55 page
95 0
|
数据挖掘 开发者
Data-Measuring Data Similarity and Dissimilarity| 学习笔记
快速学习 Data-Measuring Data Similarity and Dissimilarity。
Data-Measuring Data Similarity and Dissimilarity| 学习笔记
成功解决ValueError: With n_samples=0, test_size=0.3 and train_size=None, the resulting train set will be
成功解决ValueError: With n_samples=0, test_size=0.3 and train_size=None, the resulting train set will be
成功解决ValueError: With n_samples=0, test_size=0.3 and train_size=None, the resulting train set will be
|
数据库
Data truncation: Out of range value for column ‘estimate_score‘
Data truncation: Out of range value for column ‘estimate_score‘
成功解决linear_model\stochastic_gradient.py:128: FutureWarning: max_iter and tol parameters have been ad
成功解决linear_model\stochastic_gradient.py:128: FutureWarning: max_iter and tol parameters have been ad
|
Web App开发 Java 关系型数据库
Data truncation: Data too long for column &#39;xxx&#39; at row 1
Data truncation: Data too long for column 'xxx' at row 1 完整的错误内容可能是下面这样的: p.p1 {margin: 0.0px 0.0px 0.
2243 0