数据结构——循环链表

xiaoxiao2021-02-28  78

#include <stdio.h> #include <stdlib.h> #define OK            0 #define ERROR        -1 #define MALLOC_ERROR -2  typedef int ElementType; typedef struct node { ElementType data;    // 结点的数据 struct node *next;   // 结点指针 }Node; typedef Node *PNode;     // 重命名结点指针类型 // 头插法创建链表 //int Create_List_Head(struct node *h, ElementType data) int Create_List_Head(PNode h, ElementType data) { if (h == NULL) { return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; node->next = h->next; h->next = node; return OK; } // 尾插法创建循环链表 int Create_List_Tail(PNode h, ElementType data) { if (h == NULL) { return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; //node->next = NULL; node->next = h; // 找最后最后一个结点 PNode temp = h; while (temp->next != h) { temp = temp->next; } temp->next = node; return OK; } // 在第 pos 个结点处插入一个数据 int Insert_Node(PNode h, int pos, ElementType data) { PNode temp = h; int k=0; // 找第pos-1个结点 while (temp && k < pos-1) { temp=temp->next; k++; } if (temp == h && h->next != h) { printf ("无效的插入点\n"); return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; node->next = temp->next; temp->next = node; return OK; } // 在链表中删除第 i 个结点 int Delete_Node(PNode h, int pos) { PNode temp = h; int k=0; // 找要删除的结点的前一个结点,就是temp while (temp->next != h && k < pos-1) { temp=temp->next; k++; } if (temp->next == h && h->next != h) { printf ("无效的删除点\n"); return ERROR; } PNode p = temp->next; temp->next = p->next; free(p); p = NULL; return OK; } // 链表逆序 int Inverse_List(PNode h) { if (h->next == h || h->next->next == h) { return OK; } PNode pre = h->next; PNode cur = pre->next; PNode next = NULL; while(cur != h) { next = cur->next; cur->next = pre; pre = cur; cur = next; } h->next->next = h; h->next = pre; return OK; } // 查找链表中的元素,找到后返回改结点的指针 PNode Search(PNode h, ElementType data) { if (h == NULL) { return NULL; } PNode temp = h->next; while(temp != h) { if (temp->data == data) { return temp; } temp = temp->next; } return NULL; } // 计算链表的长度 int ListLen(PNode h) { int len = 0; PNode temp = h->next; while (temp != h) { temp = temp->next; len++; } return len; } // 打印 void DisPlay(PNode h) { if (h == NULL) { return; } PNode temp = h->next;  // 链表第一个结点指针 while (temp != h) { printf ("M", temp->data); temp = temp->next; } printf ("\n"); } // 创建头结点,循环链表为空链表 PNode Create_HeadNode() { PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (head_node == NULL) { return NULL; } head_node->next = head_node;   // 空循环链表初始化 return head_node; } int main() { PNode head_node = Create_HeadNode();  // 获取头结点指针 if (head_node == NULL) { return -1; } //Node n;   // 头插法创建链表 int i = 0; for (i = 0; i < 10; i++) { /* // 头插法创建链表   if (Create_List_Head(head_node, i) != OK) { return ERROR; }   */ // 尾插法创建链表   if (Create_List_Tail(head_node, i) != OK) { return ERROR; }   } DisPlay(head_node); // 在第 pos 个结点处插入一个数据   if(Insert_Node(head_node, 8, 11) != OK) { return ERROR; } DisPlay(head_node);  // 将第 pos 个结点删除   if(Delete_Node(head_node, 8) != OK) { return ERROR; } printf ("删除结点:\n"); DisPlay(head_node);  // 将链表逆序 if(Inverse_List(head_node) != OK) { return ERROR; } DisPlay(head_node); // 查找链表中的元素,找到后返回改结点的指针 PNode p = Search(head_node, 4); if (p == NULL) { printf ("无此结点\n"); } else { printf("node data = %d\n", p->data); } // 计算链表的长度 int len = ListLen(head_node); printf ("len = %d\n", len); return 0; }
转载请注明原文地址: https://www.6miu.com/read-66911.html

最新回复(0)