#endif
#include "LinkList.h"
#include <stdlib.h> #include <stdio.h> Node * Create_List() { Node *head = (Node*)malloc(sizeof(Node)/sizeof(char)); if (head == NULL) return NULL; head->next = head; // 空表 return head; } int Insert_Head(Node *h, LinkData data) { if (h == NULL) return FALSE; Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = h->next; h->next = node; return TRUE; } int Insert_Last(Node *h, LinkData data) { if (h == NULL) return FALSE; Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = h; Node* tmp = h; while (tmp->next != h) { tmp = tmp->next; } tmp->next = node; return TRUE; } int Insert_Pos(Node *h, int pos, LinkData data) { if (h == NULL || pos < 1) return FALSE; // 找要插入位置的前一个结点 Node *tmp = h; int i; for (i = 0; i < pos-1; i++) { if (tmp == h) break; tmp = tmp->next; } if (tmp == h) // 越界 { printf("插入位置越界\n"); return FALSE; } Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = tmp->next; tmp->next = node; return TRUE; } int Delete_Pos(Node* h, int pos) { if (h == NULL || pos < 1) return FALSE; // 找要插入位置的前一个结点 Node *tmp = h; int i; for (i = 0; i < pos-1; i++) { if (tmp->next == h) break; tmp = tmp->next; } if (tmp->next == h) // 越界 { printf("删除位置越界\n"); return FALSE; } Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Reverse_List(Node *h) { // h->next 空表 // h->next->next 只有一个结点 if (h == NULL || h->next == h || h->next->next == h) return FALSE; Node *pre = h->next; Node *cur = h->next->next; Node *tmp; while (cur == h) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } h->next->next = h; h->next = pre; return TRUE; } int Delete_Data(Node* h, LinkData data) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next != h) { if (tmp->next->data == data) break; tmp = tmp->next; } if (tmp->next == h) return FALSE; Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Find_Element(Node* h, LinkData data, int *x) { if (h == NULL) return FALSE; Node *tmp = h->next; int k = 1; while (tmp != h) { if (tmp->data == data) { *x = k; return TRUE; } k++; tmp = tmp->next; } return FALSE; } int Get_Element(Node* h, int pos, int *x) { if (h == NULL || pos < 1) return FALSE; int i; Node *tmp = h; for (i = 0; i < pos; i++) { if (tmp == h) break; tmp = tmp->next; } if (tmp == h) return FALSE; else *x = tmp->data; return TRUE; } int Get_Len(Node * h) { if (h == NULL) return 0; Node *tmp = h; int count = 0; while (tmp->next != h) { count++; tmp = tmp->next; } return count; } int Clean_List(Node * h) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next != h) { Delete_Pos(h, 1); } return 0; } void Display(Node *h) { if (h == NULL) return; int count = 0; Node *tmp = h->next; while (tmp != h) { if (count++ % 4 == 0) printf ("\n"); printf ("