链表
记录一下 这几天总结的用C语言写的链表的一下操作:
//准备工作 /* 定义一个表示链表的结构体指针 */ struct list { int id; /* 标识这个元素方便查找 */ int data; /* 链表中包含的元素 */ struct list *next; /* 指向下一个链表的指针 */ }; /* 定义一个链表头部 */ static struct list *list_head = NULL; static struct list *list_head1 = NULL; /* 为了保证每一个链表元素的id不同,特意把id定义成一个全局静态变量 */ static int list_id = 0;
//添加 static void list_add(struct list **head, struct list *list) { struct list *temp; /* 判断链表是否为空 */ if(NULL == *head) { /* 为空 */ *head = list; (*head)->next = NULL; } else { /* 不为空 */ temp = *head; while(temp) { if(NULL == temp->next) { temp->next = list; list->next = NULL; } temp = temp->next; } } }
//输出 static void list_print(struct list **head) { struct list *temp; temp = *head; printf("list information :\n"); while(temp) { printf("\tlist %d : %d\n", temp->id, temp->data); temp = temp->next; } }
//main int i = 0; struct list *lists = NULL;//创建一个空表 /* 分配10个元素 */ lists = (struct list*)malloc(sizeof(struct list) * 5); if(NULL == lists) { printf("malloc error!\n"); // return -1; } int arr[5] = {1,4,3,5,2}; // char* arr = new char[5]{1,4,2,5,3}; /* 将分配的10个元素依次填充数据并加入到链表当中 */ for(i = 0; i < 5; i++) { lists[i].id = list_id++; // sprintf(lists[i].data, "%d",arr[i]); lists[i].data = arr[i]; list_add(&list_head, &lists[i]); } // for(i = 0; i < 10; i++) // { // lists[i].id = list_id++; //// sprintf(lists[i].data, "TECH-PRO - %d", i); // lists[i].data = i; // list_add(&list_head, &lists[i]); // } /* 遍历链表,把链表中每个元素的信息都打印出来 */ list_print(&list_head);
//反转 static void list_reverse(struct list **head) { struct list *temp = *head;; list *prev = NULL; list *next; while(temp != NULL) { next = temp->next; temp->next = prev; prev = temp; temp = next; } *head = prev; }
//升序排序 void swap(int *a,int *b){ int t=*a; *a=*b; *b=t; } list *partion(list *left,list *right){ if(left==right||left->next==right) return left; int key = left->data; //选择pBegin作为基准元素 list *p=left,*q=left; while(q!=right){ //从pBegin开始向后进行一次遍历 if(q->data < key){ p=p->next; swap(&p->data,&q->data); } q=q->next; } swap(&p->data,&left->data); return p; } void quick_sort(list *left,list *right){ if(left==right||left->next==right) return; list *mid=partion(left,right); quick_sort(left,mid); quick_sort(mid->next,right); } list * sortList(list* head) { if(head==NULL||head->next==NULL) return head; quick_sort(head,NULL); return head; }
//创建一个有环的单链表 list * CreatListTail(list * L, int n){ list *p,*r; int i; srand(time(0)); /* 初始化随机数种子 */ L = (struct list*)malloc(sizeof(struct list)); /* L为整个线性表 */ r = L; /* r为指向尾部的结点 */ for (i = 0; i < n; i++) { p = (struct list*)malloc(sizeof(struct list)); /* 生成新节点 */ p->data = rand()%100 +1; /* 随机生成100以内的数字 */ r->next = p; /* 将表尾终端结点的指针指针指向新节点 */ r = p; /* 将当前的新节点定义为表尾终端结点 */ } r->next = (L)->next->next; /* 尾部指向第二个结点(如果无环r->next = null) */ return r; }
//判断链表是否有环 int testLinkRing(list *head) { list *t1 = head, *t2 = head; while(t1->next && t2->next) { t1 = t1->next; if (NULL == (t2 = t2->next->next)) return 0; //无环 if (t1 == t2) return 1; } return 0; }
//合并两个有序的单链表(递归) list* Merge2(list* head1, list* head2) { list* res; if(head1 == NULL) return head2; if(head2 == NULL) return head1; if(head1->data < head2->data) { res = head1; res->next = Merge2(head1->next, head2); } else { res = head2; res->next = Merge2(head1, head2->next); } return res; }
//main struct list *firstList = NULL;//创建一个空表 firstList = (struct list*)malloc(sizeof(struct list) * 5); int firstArr[5] = {2,4,6,8,10}; for(i = 0; i < 5; i++) { firstList[i].id = list_id++; firstList[i].data = firstArr[i]; list_add(&list_head, &firstList[i]); } struct list *secondLists = NULL;//创建一个空表 secondLists = (struct list*)malloc(sizeof(struct list) * 5); int secondArr[5] = {1,3,5,7,9}; for(i = 0; i < 5; i++) { secondLists[i].id = list_id++; secondLists[i].data = secondArr[i]; list_add(&list_head1, &secondLists[i]); } list_print(&list_head); list_print(&list_head1); struct list *mergeLists1 = NULL;//创建一个空表 mergeLists1 = (struct list*)malloc(sizeof(struct list) * 5); mergeLists1 = Merge2(list_head, list_head1); NSLog(@"11111111111111------------------------"); list_print(&mergeLists1); NSLog(@"222222222222222------------------------");