struct Node { int data; Node* next; }; struct Node* circlecreate(void) {//创建双向链表 struct Node* head; head = (Node*)malloc(sizeof(Node)); head->next = head;//只有一个元素,所以首尾相连 head->data = 0; return head; }
二:插入操作(删除操作跟其类似)
int insert_node(Node* head, int num) { Node* new_node = NULL; struct Node* temp = head; new_node = (Node*)malloc(sizeof(Node)); if (!new_node) { return -1; } new_node->data = num; //除了使用for来循环遍历,也可以使用while来遍历,只不过用for的会有元素个数 while (temp->next) { temp = temp->next; } temp->next = new_node; new_node->next = temp; return 0; }
三:遍历循环链表:
int show_list(struct Node* head) { Node* temp = head->next; if (temp->next == head)//判断是不是只有一个元素,因为只有这种情况才会有if中的这种情况 return -1; while (temp != head) temp = temp->next; return 0; }