写给初学数据结构的同学之(循环双链表基本操作,创建,插入,删除,排序)
1.双向循环链表的创建:没什么好说的
NODE * Doublelinklist(){ NODE *head = NULL; return head;}
2.插入节点:
NODE *myinsert(NODE *head,NodeData Data){ NODE *last = head; if(head == NULL) { head = (NODE *)malloc(sizeof(NODE)); head->data = Data;head->next = head;head->pione = head; } else { NODE *newnode = (NODE *)malloc(sizeof(NODE));newnode->data = Data; while(last->next != head){ last = last->next;}last->next = newnode;newnode->pione = last;newnode->next = head;head->pione = newnode; } return head;}
3.删除节点:
NODE *myremove(NODE *head,int data){ NODE *q = head; if(head == NULL) { printf("该链表为空!无法再进行删除!\n");return head; } else { if(head->next == head){ if(head->data == data) { free(head);head = NULL;return head; } else { printf("没有相关结点!\n");return head; }}else{ if(head->data == data) { NODE *pre = head->pione;NODE *ne = head->next; head = head->next;pre->next = head;free(q);q = NULL;return head; } else { while((q->data != data) && (q->next != head)){ q = q->next;} if(q->data == data){ NODE *pre = q->pione; NODE *ne = q->next; pre->next = ne; ne->pione = pre; free(q); q = NULL; return head;}else{ printf("没有相关节点!\n"); return head;} }} }}
4.排序:我这里是用的冒泡排序,还可以用直接插入排序,选择插入排序,快速排序。
直接插入排序会比较容易实现,快速插入排序的基准有点蛋疼。
NODE *mysort(NODE *head){ NODE *p = head; NODE *max = head; NODE *ne = NULL; NODE *pre = NULL; int len = Doublelinklistlen(head); int i = len - 1; int j = len - 1; if((head == NULL) && (head->next == head)) { return head; } else { while(i--){ p = head; while(p->next != head) { if(p->next->data > p->data){ if(p == head) { head = p->next; } ne = p->next; pre = p->pione; p->next = p->next->next; p->next->pione = p; ne->next = p; p->pione = ne; ne->pione = pre; pre->next = ne;}else{ p = p->next; } }} } return head;}
实践出真知,好好先直接试着写写,写不出来再看别人的。或者先好好看,然后再直接试着写写。最好不要一边看,一边写~~~~~~~
下面,我会继续更新一些其他的数据结构的知识,我个人觉得,学习数据结构,多画画图,要知道一些数据结构的基本操作,单链表太简单,我只会说一下单链表的转置,
然后就栈和队列,树(以二叉树为主),排序等等~~~~~~希望有兴趣的同学可以关注~~~~~~~