当前位置:编程学习 > C/C++ >>

c语言版创建单循环链表

print?Node *create() 

    int n = 20; 
 
    Node *pNew, *pTail, *pHead; 
     
    pHead = (Node *)malloc(sizeof(Node)); 
    pHead->next = pHead;//空链表 自己指向自己  
 
    pTail = pHead;  //pTail指针指向pHead节点  
 
    for(int i = 1; i <= n; i++) 
    { 
        pNew = (Node *)malloc(sizeof(Node)); 
        if(pNew == NULL) 
        { 
            printf("error!!"); 
            exit(0); 
        } 
        pNew->data = i; 
         
        if(pHead->next == pHead)//此时为空链表  
        { 
            pHead->next = pNew; 
            pTail = pNew; //pTail指向新添加的那个节点  
        }else{//不是空表的时候  
            pTail->next = pNew; 
            pTail = pNew; 
        } 
    } 
     
    pTail->next = pHead;//添加完以后把尾指针的指针域指向头节点  
 
    return pTail; 

Node *create()
{
 int n = 20;

 Node *pNew, *pTail, *pHead;
 
 pHead = (Node *)malloc(sizeof(Node));
 pHead->next = pHead;//空链表 自己指向自己

 pTail = pHead; //pTail指针指向pHead节点

 for(int i = 1; i <= n; i++)
 {
  pNew = (Node *)malloc(sizeof(Node));
  if(pNew == NULL)
  {
   printf("error!!");
   exit(0);
  }
  pNew->data = i;
  
  if(pHead->next == pHead)//此时为空链表
  {
   pHead->next = pNew;
   pTail = pNew; //pTail指向新添加的那个节点
  }else{//不是空表的时候
   pTail->next = pNew;
   pTail = pNew;
  }
 }
 
 pTail->next = pHead;//添加完以后把尾指针的指针域指向头节点

 return pTail;
}

 

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,