Common operations of list and time complexity of algorithms

Source: Internet
Author: User

List_op.h

/************************************************************************* > File name:list_op.h > Author:zhoulin > Mail: [email protected] > Created time:sat April 09:23:40 AM CST ************ ************************************************************/#ifndef _list_op_htypedefstruct_listnode{intv; struct_listnode *Next;} ListNode;#defineRebuild (p,h,t) \{                           if(H = =NULL) {h= T =p; }Else{T->next =p; T=p; } t->next =NULL; }//Print linked list, time complexity O (n)voidLISTPRT (ListNode *p);//linked list initialization, time responsibility O (n)ListNode *listinit (ListNode *a,intn);//Delete a duplicate element in the list PListNode *listdrepeat (ListNode *p);//Delete a repeating element in a linked listListNode *listnrepeat (ListNode *p);//given a value, requires that the left side of the list is less than the value, the right side of the list is greater than the value, and the relative position of the list does not changeListNode *listquick (ListNode *p,intv);#define_list_op_h#endif

LIST_OP.C:

/************************************************************************* > File name:list_op.c > Author:zhoulin > Mail: [email protected] > Created time:sat April 09:15:55 AM CST ************ ************************************************************/#include"List_op.h"#include<stdio.h>#include<stdlib.h>#include<string.h>#defineListsize 14ListNode*listquick (ListNode *p,intv) {fprintf (stdout,"**************************v=%d, the node greater than V is on the right side of the V node, otherwise on the left *****************************\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; returnS_h;} ListNode*listnrepeat (ListNode *p) {fprintf (stdout,"************************** Delete Linked list repeating element *****************************\n"); ListNode*cur =p; intFlag[listsize]; memset ((int*) &flag,-1,sizeof(int)*listsize);  while(cur! =NULL) {Flag[cur-&GT;V] =0; ListNode*pnext = cur->Next; if(Pnext! = NULL && Flag[pnext->v] = =0) {cur->next = pnext->Next; }Else{cur=Pnext; }    }    returnp;} ListNode*listdrepeat (ListNode *p) {fprintf (stdout,"************************* Delete linked list with duplicate elements *****************************\n"); intFlag[listsize]; inti =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-&GT;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; }    returnHead;} ListNode*listinit (ListNode *a,intN) {    if(N <=0)    {        returnNULL; }    if(N >listsize) {N=listsize; }    inti =0; fprintf (stdout,"************************** original linked list *****************************\n");  for(i =0; I < n;i++) {A[I].V= rand ()%Ten; 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"); returnA;}voidLISTPRT (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; }}intMainvoid) {ListNode a[listsize]; ListNode*tmp =NULL; ListNode*ai = Listinit (&a[0], +); TMP= Listquick (Ai,rand ()%Ten);    LISTPRT (TMP); Ai= Listinit (&a[0], +); TMP=listnrepeat (AI);    LISTPRT (TMP); Ai= Listinit (&a[0], +); TMP=listdrepeat (AI);    LISTPRT (TMP); return 0;}

Test results:

Original Linked list *****************************3-6-7-5-3-5-6-2-9-1-2-7-0-9v=3, the node greater than V is on the right side of the V node, otherwise on the left *****************************2-1-2-0-3-3-6-7-5-5-6-9-7-9Original Linked list *****************************6-0-6-2-6-1-8-7-9-2-0-2-3-7Delete a linked list repeating element *****************************6-0-2-1-8-7-9-3Original Linked list *****************************5-9-2-2-8-9-7-3-6-1-2-9-3-1Delete Linked list has duplicate elements *****************************5-8-7-6

Common operations of list and time complexity of algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.