双方向チェーンの添削チェック

3512 ワード

//
// Created by Zhongqi.Shao on 2016/12/13.
//

#ifndef DOUBLELINK_DOUBLELINK_H
#define DOUBLELINK_DOUBLELINK_H

typedef int DataType;

typedef struct LinkNode {
    DataType data;
    struct LinkNode *next;
    struct LinkNode *pre;
} Node;

typedef struct DoubleLink {
    Node *pHead;
    Node *pTail;
} DLink;


extern void addDataBack(DLink *link, DataType data);

extern void addDataHead(DLink *link, DataType data);

extern void deleteData(DLink *link, DataType data);

extern void showLinkBack(DLink *link);

extern void showLinkHead(DLink *link);


#endif //DOUBLELINK_DOUBLELINK_H
 
  
 
  
 
  
//
// Created by Zhongqi.Shao on 2016/12/13.
//
#include"DoubleLink.h"
#include 
#include 
#include 


extern void addDataBack(DLink *link, DataType data) {
    if (link == NULL) {
        return;
    }
    Node *newNode = (Node *) malloc(sizeof(Node));
    newNode->data = data;
    newNode->pre = NULL;
    newNode->next = NULL;
    if (link->pHead == NULL || link->pTail == NULL) {
        link->pHead = newNode;
        link->pTail = newNode;
    } else {
        link->pTail->next = newNode;
        newNode->pre = link->pTail;
        link->pTail = newNode;
    }
}

extern void addDataHead(DLink *link, DataType data) {
    if (link == NULL) {
        return;
    }
    Node *newNode = (Node *) malloc(sizeof(Node));
    newNode->data = data;
    newNode->pre = NULL;
    newNode->next = NULL;
    if (link->pHead == NULL || link->pTail == NULL) {
        link->pTail = newNode;
        link->pHead = newNode;
    } else {
        newNode->next = link->pHead;
        link->pHead->pre = newNode;
        link->pHead = newNode;
    }
}

extern void showLinkBack(DLink *link) {
    if (link == NULL) {
        return;
    }
    Node *tmp = link->pHead;
    while (tmp != NULL) {
        printf("Node Data = %d
", tmp->data); tmp = tmp->next; } } extern void showLinkHead(DLink *link) { if (link == NULL) { return; } Node *tmp = link->pTail; while (tmp != NULL) { printf("Node Data = %d
", tmp->data); tmp = tmp->pre; } } extern void deleteData(DLink *link, DataType data) { if (link == NULL || link->pTail == NULL || link->pHead == NULL) { return; } Node *tmp = link->pHead; while (tmp != NULL) { if (tmp->data == data) { break; } tmp = tmp->next; } if (tmp == link->pHead) { link->pHead->next->pre = NULL; link->pHead = link->pHead->next; free(tmp); } else if (tmp == link->pTail) { link->pTail->pre->next = NULL; link->pTail = link->pTail->pre; free(tmp); } else { tmp->pre->next = tmp->next; tmp->next->pre = tmp->pre; free(tmp); } }