list_op.h
/************************************************************************* > File Name: list_op.h > Author: zhoulin > Mail: [email protected] > Created Time: Sat 23 Apr 2016 09:23:40 AM CST ************************************************************************/ #ifndef _LIST_OP_H typedef struct _listNode { int v; struct _listNode *next; }listNode; #define rebuild(p,h,t) \ { if(h == NULL) { h = t = p; }else{ t->next = p; t = p; } t->next = NULL; } //打印链表,时间复杂度O(n) void listPrt(listNode *p); //链表初始化,时间负责度O(n) listNode *listInit(listNode *a,int n); //删除链表P中有过重复的元素 listNode *listDRepeat(listNode *p); //删除链表中重复元素 listNode *listNRepeat(listNode *p); //给定一个值,要求链表左边小于该值,链表右边大于该值,且链表的相对位置不改变 listNode *listQuick(listNode *p,int v); #define _LIST_OP_H #endif
list_op.c:
/************************************************************************* > File Name: list_op.c > Author: zhoulin > Mail: [email protected] > Created Time: Sat 23 Apr 2016 09:15:55 AM CST ************************************************************************/ #include "list_op.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define listSize 14 listNode *listQuick(listNode *p,int v) { fprintf(stdout,"**************************v=%d,大于v的节点在v节点右侧,否则在左侧*****************************\n",v); listNode *b_h,*b_t; listNode *s_h,*s_t; listNode *r_h,*r_t; b_h = b_t = s_h =NULL; s_t = r_h = r_t =NULL; while(p != NULL) { listNode *pnext = p->next; if(p->v == v) { rebuild(p,r_h,r_t); } else if(p->v > v) { rebuild(p,b_h,b_t); }else{ rebuild(p,s_h,s_t); } p = pnext; } if(s_t != NULL) s_t->next = r_h; if(r_t != NULL) r_t->next = b_h; return s_h; } listNode *listNRepeat(listNode *p) { fprintf(stdout,"**************************删除链表重复元素*****************************\n"); listNode *cur = p; int flag[listSize]; memset((int *)&flag,-1,sizeof(int)*listSize); while(cur != NULL) { flag[cur->v] = 0; listNode *pnext = cur->next; if(pnext != NULL && flag[pnext->v] == 0) { cur->next = pnext->next; }else { cur = pnext; } } return p; } listNode *listDRepeat(listNode *p) { fprintf(stdout,"*************************删除链表有重复元素*****************************\n"); int flag[listSize]; int i = 0; memset(&flag,-1,listSize*sizeof(int)); listNode *rp = p; listNode *head,*tail; head = tail = NULL; while(p != NULL) { if(flag[p->v] < 0) { flag[p->v] = 0; } listNode *pnext = p->next; if(pnext != NULL){ if(flag[pnext->v] == 0) { ++flag[pnext->v]; } } p = pnext; } while(rp != NULL) { listNode *next = rp->next; if(flag[rp->v] == 0) { rebuild(rp,head,tail); } rp = next; } return head; } listNode *listInit(listNode *a,int n) { if(n <= 0) { return NULL; } if(n > listSize) { n = listSize; } int i = 0; fprintf(stdout,"**************************原始链表*****************************\n"); for(i = 0;i < n;i++) { a[i].v = rand()%10; listNode *cur = &a[i]; if(i == listSize -1){ a[i].next = NULL; fprintf(stdout,"%d\n",cur->v); break; } a[i].next = &a[i+1]; fprintf(stdout,"%d ->",cur->v); } fprintf(stdout,"\n"); return a; } void listPrt(listNode *p) { listNode *cur = p; while(cur != NULL) { if(cur->next == NULL) { fprintf(stdout,"%d\n\n",cur->v); break; } fprintf(stdout,"%d ->",cur->v); cur = cur->next; } } int main(void) { listNode a[listSize]; listNode *tmp = NULL; listNode *ai = listInit(&a[0],19); tmp = listQuick(ai,rand()%10); listPrt(tmp); ai = listInit(&a[0],19); tmp = listNRepeat(ai); listPrt(tmp); ai = listInit(&a[0],19); tmp = listDRepeat(ai); listPrt(tmp); return 0; }
测试结果:
**************************原始链表***************************** 3 ->6 ->7 ->5 ->3 ->5 ->6 ->2 ->9 ->1 ->2 ->7 ->0 ->9 **************************v=3,大于v的节点在v节点右侧,否则在左侧***************************** 2 ->1 ->2 ->0 ->3 ->3 ->6 ->7 ->5 ->5 ->6 ->9 ->7 ->9 **************************原始链表***************************** 6 ->0 ->6 ->2 ->6 ->1 ->8 ->7 ->9 ->2 ->0 ->2 ->3 ->7 **************************删除链表重复元素***************************** 6 ->0 ->2 ->1 ->8 ->7 ->9 ->3 **************************原始链表***************************** 5 ->9 ->2 ->2 ->8 ->9 ->7 ->3 ->6 ->1 ->2 ->9 ->3 ->1 *************************删除链表有重复元素***************************** 5 ->8 ->7 ->6
时间: 2024-12-11 16:41:10