单链表几种操作

转自:http://www.cnblogs.com/marrywindy/archive/2010/10/29/1864067.html

代码
 /*
 purpose:实现链表的几种操作
  */
 #include "stdio.h"
 #include "stdlib.h"
 #include "malloc.h"
 
  struct SNode 
 {
     int ivalue;
     struct SNode* next;
 };
 
 /************************************************************************/
 /*   Tail insert   */
 /************************************************************************/
 SNode* createLinkList()
 {
     SNode* head,*s,*p;
     int ix;
     char ch;
     head=(SNode*)malloc(sizeof(struct SNode));
     if(NULL==head)
         exit(1);
     head->next=NULL;
     p=head;
     while (scanf_s("%d",&ix)==1)
     {
         s=(SNode*)malloc(sizeof(struct SNode));
         if(NULL==s)
             exit(1);
         s->ivalue=ix;
         p->next=s;
         p=s;
     }
     p->next=NULL;  //No loop
     //p->next=head;      //Loop linklist
     return head;
 }
 
 /************************************************************************/
 /* print                                                                     */
 /************************************************************************/
 void printLinkList(SNode* pNode)
 {
     if(NULL==pNode)
         exit(1);
     SNode* p;
     p=pNode->next;
     //while (p!=pNode)  //Loop
     while(p!=NULL)        //No loop
     {
         printf("%d ",p->ivalue);
         p=p->next;
     }
     printf("\n");
 }
 
 /************************************************************************/
 /* IsLoop                                                                     */
 /************************************************************************/
 bool IsLoop(SNode* pNode)
 {
     SNode* p=pNode;
     SNode* q=pNode;
     while (q!=NULL && q->next!=NULL)
     {
         p=p->next;
         q=q->next->next;
         if (p==q)
             return true;
     }
     return false;
 }
 
 
 
 int main()
 {
     SNode* sNode;
     sNode=createLinkList();
     printLinkList(sNode);
     bool im=IsLoop(sNode);
     if(im)
         printf("true\n");
     else
         printf("false\n");
     return 0;
 }


原文地址:https://www.cnblogs.com/youngforever/p/3104639.html