带头结点链表大例题

来源:互联网 发布:符文战争桌游淘宝 编辑:程序博客网 时间:2024/05/16 04:54
```#include <stdio.h>#include <malloc.h>#include"MECTool.h

typedef struct POINT{
int row;
int col;
struct POINT *next;
}POINT;

boolean compare(POINT node1, POINT node2);
boolean initPointLinkList(POINT *head);
boolean isDataLeagal(int row, int col);
void setPointRowAndCol(POINT *newNode, int row, int col);
POINT *setPointInstance();
void showPoints(POINT head);
void showOnePoint(POINT node);
boolean destroyPointLinkList(POINT *head);
void insertPoints(POINT *head, boolean (*equals)(POINT, POINT));
void insertAPoint(POINT *head, boolean(*equals)(POINT, POINT));
POINT *findThePrePoint(POINT head, POINT oldPoint, boolean (*equals)(POINT, POINT));
void sortByRow(POINT *head);
boolean delePointsInTheLinkList(POINT *head, boolean (*equals)(POINT, POINT));
boolean deleAPointInTheLinkList(POINT *head, boolean (*equals)(POINT, POINT));
boolean deleCompare(POINT node1, POINT node2);

boolean deleCompare(POINT node1, POINT node2) {
return node1.row == node2.row && node1.col == node2.col;
}

boolean deleAPointInTheLinkList(POINT *head, boolean (*equals)(POINT, POINT)) {
int row;
int col;
POINT delePoint;
POINT *pre = NULL;
POINT *tmp;

printf("请输入要删除坐标值:");scanf("%d %d", &row, &col);setPointRowAndCol(&delePoint, row, col);pre = findThePrePoint(*head, delePoint, equals);if(NULL == pre) {    pre = head;} else if(pre->next == NULL) {    return FALSE;       // 已经找到最后一个结点,都不是,说明没有该点}tmp = pre->next;pre->next = tmp->next;free(tmp);return TRUE;

}

boolean delePointsInTheLinkList(POINT *head, boolean (*equals)(POINT, POINT)) {
printf(“删除一个点坐标功能!\n”);
if(TRUE == deleAPointInTheLinkList(head, equals)){
printf(“删除一个点功能已完成!\n”);
return TRUE;
}
return FALSE;
}

void sortByRow(POINT *head) {
POINT *p;
POINT *last;
POINT tmp;
POINT *ptmp;

for(p = head->next; p != NULL; p = p->next) {    for(last = p->next; last != NULL; last = last->next) {        if(p->row > last->row) {            tmp = *p;            *p = *last;            *last = tmp;            ptmp = p->next;            p->next = last->next;            last->next = ptmp;            //将链域值值交换,保持原链域顺序不变。        }    }}

}

POINT *findThePrePoint(POINT head, POINT oldPoint, boolean(*equals)(POINT, POINT)) {
POINT *cur;
POINT *pre = NULL;

for(cur = head.next; cur != NULL; cur = cur->next) {    if(TRUE == (equals)(*cur, oldPoint)) {        return pre;    }    pre = cur;}return pre;// 若返回值为NULL,则说明是第一个有效结点,没有前驱结点;若返回值不为NULL,1、pre->next == NULL则为最后一个,追加// 2、pre->next != NULL时正常。

}

void insertAPoint(POINT *head, boolean (*equals)(POINT, POINT)) {
POINT *pre = NULL;
int row;
int col;
int oldRow;
int oldCol;
POINT *insertPoint;
POINT oldPoint;

printf("请输入要插入的点坐标:");scanf("%d %d", &row, &col);while(FALSE == isDataLeagal(row, col)) {    printf("请重新输入点的坐标:");    scanf("%d %d", &row, &col);}insertPoint = setPointInstance();setPointRowAndCol(insertPoint, row, col);printf("请输入要插入点的位置信息(左插入):");scanf("%d %d", &oldRow, &oldCol);setPointRowAndCol(&oldPoint, oldRow, oldCol);pre = findThePrePoint(*head, oldPoint, equals);if(NULL == pre) {    pre = head;}insertPoint->next = pre->next;pre->next = insertPoint;

}

void insertPoints(POINT *head, boolean (*equals)(POINT, POINT)) {
printf(“现在开始插入一个点:”);
insertAPoint(head, equals);
printf(“插入功能已完成!”);
}

boolean destroyPointLinkList(POINT *head) {
POINT *p = NULL;

if(NULL == head->next) {    return FALSE;}p = head->next;while(p != NULL) {    head->next = p->next;    free(p);    p = head->next;}return TRUE;

}

void showOnePoint(POINT node) {
printf(“(%d, %d) “, node.row, node.col);
}

void showPoints(POINT head) {
POINT *node = NULL;

node = head.next;while(node != NULL) {    showOnePoint(*node);    node = node->next;}printf("\n");

}

POINT *setPointInstance() {
POINT *newNode;

newNode = (POINT *)malloc(sizeof(POINT));newNode->next = NULL;return newNode;

}

void setPointRowAndCol(POINT *newNode, int row, int col) {
newNode->row = row;
newNode->col = col;
}

boolean isDataLeagal(int row, int col) {
return row > 0 && col > 0 && row <= 25 && col <= 80;
}

boolean initPointLinkList(POINT *head) {
int row;
int col;
POINT *newNode = NULL;
POINT *lastNode = NULL;

if(head == NULL) {    return FALSE;}if(head->next == NULL){    printf("请输入点的坐标(例如 1 2),且行值在1到25之间,列值在1到80之间,任一一个值为0时停止输入:");    scanf("%d %d", &row, &col);    while(isDataLeagal(row, col) == TRUE) {        newNode = (POINT *)calloc(sizeof(POINT), 1);        setPointRowAndCol(newNode, row, col);        if(head->next == NULL) {            head->next = newNode;        } else {            lastNode->next = newNode;        }        lastNode = newNode;        printf("请输入点的坐标(例如 1 2),且行值在1到25之间,列值在1到80之间,任一一个值为0时停止输入:");        scanf("%d %d", &row, &col);    }}return TRUE;

}

boolean compare(POINT node1, POINT node2) {
return node1.row == node2.row && node1.col == node2.col;
}

void main(void) {
POINT head = {0};

initPointLinkList(&head);printf("\n以下是点坐标的信息:\n");showPoints(head);printf("现在插入一个点坐标:\n");insertPoints(&head, compare);printf("插入完成后的坐标信息如下:\n");showPoints(head);   sortByRow(&head);printf("排序结果如下所示:\n");showPoints(head);printf("现在删除一个点坐标:\n");delePointsInTheLinkList(&head, deleCompare);showPoints(head);destroyPointLinkList(&head);

}

/*
1 2
23 67
21 56
13 45
17 78
23 56
15 69
9 0
19 78
13 45
*/
“`

0 0