- //Time : 2013-01-07
- //List
- //reference by nowamagic
- #include<stdio.h>
- #include"malloc.h"
- #include<stdlib.h>
- #include"time.h"
- #define ERROR 0
- #define OK 1
- #define FALSE 0
- #define TRUE 1
- #define MemoryAllocFail printf("\nMemory allocation failure\n")
- #define ERRORINSERT printf("\nERRORINSERT\n")
- #define ERRORDELETE printf("\nERRORDELETE\n")
- #define GETELEMERROR printf("\nGETELEMERROR\n")
- #define Status int
- typedef int ListElemType;
- typedef struct ListNode
- {
- ListElemType iData;
- struct ListNode *next;
- }ListNode,*LinkList;
- //List Initial
- Status InitList(LinkList *L)
- {
- // Note
- // *L is HeadNode
- // L is HeadPointer
- *L = (LinkList)malloc(sizeof(ListNode));
- if(!(*L))
- {
- MemoryAllocFail;
- return ERROR;
- }
- (*L)->next = NULL;
- return OK;
- }
- //Get the Length of List
- int ListLength(LinkList L)
- {
- LinkList p;
- int iLength;
- p = L->next;// pointer 'p' points to the first node of the list L
- iLength = 0;//the counter of the length
- while(p)
- {
- ++iLength;
- p = p->next;
- }
- return iLength;
- }
- //print the one elem of the list
- Status visit(ListElemType e)
- {
- printf("->%d",e);
- return OK;
- }
- //traverse the list
- Status ListTraverse(LinkList L)
- {
- //if(L->next == NULL)
- LinkList p;
- p = L->next;
- while(p)
- {
- visit(p->iData);
- p = p->next;
- }
- printf("\n");
- return OK;
- }
- //Insert 'e' into the list
- Status ListInsert(LinkList *L,int i,ListElemType e)
- {
- int j;
- LinkList p,s;
- p = *L;
- j = 0;
- while(p && j<i)
- {
- p = p->next;
- j++;
- }
- if(!p || j>i)
- {
- ERRORINSERT;
- return ERROR;
- }
- s = (LinkList)malloc(sizeof(ListNode));
- s->iData = e;
- s->next = p->next;
- p->next = s;
- return OK;
- }
- //delete
- Status ListDelete(LinkList *L,int i,ListElemType *e)
- {
- int j;
- LinkList p,q;
- p = *L;
- j = 1;
- while(p->next && j<i)
- {
- ++j;
- p = p->next;
- }
- if(!(p->next) || j>i)
- {
- ERRORDELETE;
- return ERROR;
- }
- q = p->next;
- *e = q->iData;
- free(q);//free the node which has been deleted
- return OK;
- }
- //get elem ('i' locatin) of the list
- Status GetElem(LinkList L,int i,ListElemType *e)
- {
- int j;
- LinkList p;
- j = 1;
- p = L->next;
- while(p && j<i)
- {
- p = p->next;
- ++j;
- }
- if(!p || j>i)
- {
- GETELEMERROR;
- return ERROR;
- }
- *e = p->iData;
- return OK;
- }
- //Location
- Status LocateElem(LinkList L,ListElemType e)
- {
- int i;
- LinkList p;
- i = 0;
- p = L->next;
- while(p)
- {
- ++i;
- if(p->iData == e)
- {
- return i;
- }
- p = p->next;
- }
- return ERROR;
- }
- //clear list
- Status ClearList(LinkList *L)
- {
- LinkList p,q;
- p = (*L)->next;
- while(p)
- {
- q = p->next;
- free(p);
- p = q;
- }
- (*L)->next = NULL;
- return OK;
- }
- //Create list
- Status CreateListFromHead(LinkList *L,int n)
- {
- LinkList s;
- int i;
- srand(time(0));
- *L = (LinkList)malloc(sizeof(ListNode));
- (*L)->next = NULL;
- for(i=0;i<n;i++)
- {
- s = (LinkList)malloc(sizeof(ListNode));
- s->iData = rand()%100 + 1;
- s->next = (*L)->next;
- (*L)->next = s;
- }
- return OK;
- }
- //CreateList
- Status CreateListFromHail(LinkList *L,int n)
- {
- LinkList p,r;
- int i;
- srand(time(0));
- *L = (LinkList)malloc(sizeof(ListNode));
- r=*L;
- for(i=0;i<n;i++)
- {
- p = (LinkList)malloc(sizeof(ListNode));
- p->iData = rand()%100 + 1;
- r->next = p;
- r = p;
- }
- r->next = NULL;
- return OK;
- }
- //ListReverse2
- LinkList ListReverse2(LinkList L)
- {
- LinkList current,p;
- if(L == NULL)
- {
- return L;
- }
- current=L->next;
- while(current->next != NULL)
- {
- p=current->next;
- current->next = p->next;
- p->next = L->next;
- L->next = p;
- }
- return L;
- }
- //getlastn
- LinkList getlastn(LinkList L,int n)
- {
- if(n<= 0 || n > ListLength(L))
- {
- printf("\nERROR\n");
- exit(0);
- }
- int i;
- LinkList firstNode,secNode;
- i=0;
- firstNode = L;
- //firstNode 先走n步
- while(i<n && firstNode->next != NULL)
- {
- //正数N个节点,firstNode指向正的第N个节点
- i++;
- firstNode = firstNode->next;
- //printf("%d\n",i);
- }
- secNode = L;
- //然后两个指针一起走
- while(firstNode != NULL)
- {
- firstNode = firstNode->next;
- secNode = secNode->next;
- }
- return secNode;//secNode即为倒数第n个结点
- }
- //GetMidNode
- Status GetMidNode(LinkList L,ListElemType *e)
- {
- LinkList search,mid;
- mid = search = L;
- while(search->next != NULL)
- {
- if(search->next ->next != NULL)
- {
- search=search->next ->next ;
- mid = mid->next ;
- }
- else
- {
- search = search->next ;
- }
- }
- *e = mid->next ->iData ;
- return OK;
- }
- //Loop
- int HasLoop1(LinkList L)
- {
- int step1 = 1;
- int step2 = 2;
- LinkList p = L;
- LinkList q = L;
- while (p != NULL && q != NULL && q->next != NULL)
- {
- p = p->next;
- if (q->next != NULL)
- q = q->next->next;
- printf("p:%d, q:%d \n", p->iData, q->iData);
- if (p == q)
- return 1;
- }
- return 0;
- }
- //loop
- int HasLoop3(LinkList L)
- {
- int step1 = 1;
- int step2 = 2;
- LinkList p = L;
- LinkList q = L;
- while(p != NULL && q != NULL && q->next != NULL)
- {
- p = p->next ;
- if(q->next != NULL)
- q = q->next ->next ;
- if(p == q)
- return 1;
- }
- return 0;
- }
- //loop
- int HasLoop2(LinkList L)
- {
- LinkList cur1 = L;
- int pos1 = 0;
- while(cur1)
- {
- LinkList cur2 = L;
- int pos2 = 0;
- pos1 ++;
- while(cur2)
- {
- pos2 ++;
- if(cur2 == cur1)
- {
- if(pos1 == pos2)
- break;
- else
- return 1;
- }
- cur2 = cur2->next;
- }
- cur1 = cur1->next;
- }
- return 0;
- }
- //buildloop
- Status BuildListLoop(LinkList *L,int num)
- {
- int i=0;
- LinkList cur = *L;
- LinkList tail = NULL;
- if(num<=0 || L==NULL)
- {
- return ERROR;
- }
- for(i = 1;i<num;i++)
- {
- if(cur==NULL)
- {
- return ERROR;
- }
- cur = cur->next ;
- }
- tail = cur;
- while(tail->next)
- {
- tail = tail->next ;
- }
- tail->next = cur;
- return OK;
- }
- //remove node which duplicate
- LinkList RemoveDupNode(LinkList L)
- {
- LinkList p,q,r;
- p=L->next;
- while(p)
- {
- q=p;
- while(q->next)
- {
- if(q->next->iData==p->iData)
- {
- r=q->next;
- q->next=r->next;
- free(r);
- }
- else
- q=q->next;
- }
- p=p->next;
- }
- return L;
- }
- //remove node which duplicate
- LinkList RemoveDupNode2(LinkList L)
- {
- LinkList p,q,r;
- p=L->next ;
- while(p)
- {
- q=p;
- while(q->next)
- {
- if(q->next ->iData == p->iData )
- {
- r=q->next ;
- q->next = r->next ;
- free(r);
- }
- else
- q=q->next ;
- }
- p=p->next ;
- }
- return L;
- }
- //main
- Status main(void)
- {
- LinkList L;
- //LinkList tmp;
- int elem;
- //InitList(&L);
- //CreateListFromHead(&L,7);
- //ListTraverse(L);
- //BuildListLoop(&L,3);
- //if(HasLoop2(L))
- //printf("\nthe list has loop\n");
- //tmp = getlastn(L,8);
- //GetMidNode(L,&elem);
- //printf("\n%d\n",elem);
- //printf("\n%d\n",tmp->iData);
- //RemoveDupNode2(L);
- //ListTraverse(L);
- return OK;
- }