循環單鏈表定義初始化及創建(C語言)


#include <stdio.h>
#include <stdlib.h>

/**
* 含頭節點循環單鏈表定義,初始化 及創建 
*/

#define OK 1;
#define ERROR 0;

//函數返回類型,表示函數運行結果的狀態 
typedef int Status;
//定義數據元素類型
typedef char ElemType;

//循環單鏈表定義 
typedef struct LoopLnode {
    ElemType data;        //數據域,這里是char類型變量 
    struct LoopLnode *next;        //指針域,結構體類型指針                                            
} LoopLnode, *LoopLinkList;

//循環單鏈表初始化
Status InitList(LoopLinkList *list) {
    (*list)=(LoopLinkList)malloc(sizeof(LoopLnode));
    (*list)->next=(*list);
    (*list)->data='T'; //測試用,可不寫 
    return OK;
}

//1."頭插法"創建僅含"頭指針"的單向循環鏈表
Status CreateList_H(LoopLinkList *list,ElemType arrData[],int length){
    int j;
    for(j=length-1;j>=0;j--){
        //新建結點 
        LoopLnode *node;
        node=(LoopLnode*)malloc(sizeof(LoopLnode));
        node->data=arrData[j];
        node->next=NULL;
        
        //插入循環鏈表 
        node->next=(*list)->next;
        (*list)->next=node;  //list始終指向頭結點 
    }
    return OK;
}

//2."尾插法"創建僅含"頭指針"的單向循環鏈表
Status CreateList_R(LoopLinkList *list,ElemType arrData[],int length){
    LoopLnode *r;
    r=*list; 
    int j;
    for(j=0;j<length;j++) {
        //新建結點 
        LoopLnode *node;
        node=(LoopLnode*)malloc(sizeof(LoopLnode));
        node->data=arrData[j];
        node->next=NULL;
        
        //插入循環鏈表 
        node->next=r->next;
        r=node;
    }
    return OK;
}

//3."頭插法"創建僅含"尾指針"的單向循環鏈表
Status BuildList_H(LoopLinkList *list,ElemType arrData[],int length){
    int j;
    for(j=length-1;j>=0;j--){
        //新建結點 
        LoopLnode *node;
        node=(LoopLnode*)malloc(sizeof(LoopLnode));
        node->data=arrData[j];
        node->next=NULL;
        
        //node插入1號結點,list為尾指針 
        node->next=(*list)->next->next;  //node->next=頭結點->next 
        if((*list)->next==(*list)) (*list)=node;  //當只有頭結點時(插入第一個結點時,手動設置node為尾指針) 
        (*list)->next->next=node;  //頭結點->next=node;
    }
    return OK;
}

//4."尾插法"創建僅含"尾指針"的單向循環鏈表
Status BuildList_R(LoopLinkList *list,ElemType arrData[],int length) {
    int j;
    for(j=0;j<length;j++) {
        //新建結點 
        LoopLnode *node;
        node=(LoopLnode*)malloc(sizeof(LoopLnode));
        node->data=arrData[j];
        node->next=NULL;
        
        node->next=(*list)->next; //node->next=頭結點 
        (*list) = node; //尾指針 —> node 
    }
    return OK;
}

int main(void){
    //產生待插入到鏈表的數據
    ElemType data1='A',data2='B',data3='C';
    ElemType waitInserted[]={data1,data2,data3,};
    //獲得數組長度 
    int arrLength=sizeof(waitInserted)/sizeof(waitInserted[0]);
    
    
    /**1.頭插法建立只含頭指針循環單鏈表**/
    //定義鏈表並初始化 
    LoopLinkList list1;
    InitList(&list1);
    //按既定數據建立鏈表
    CreateList_H(&list1,waitInserted,arrLength);
    //測試 
    printf("%c\n",list1->next->next->next->next->next->next->data); //B 
    
    /**2.尾插法建立只含頭指針循環單鏈表**/
    //定義鏈表並初始化 
    LoopLinkList list2;
    InitList(&list2);
    //按既定數據建立鏈表
    CreateList_R(&list2,waitInserted,arrLength);
    //測試 
    printf("%c\n",list1->next->next->next->next->next->next->data); //B
    
    
    /**3.頭插法建立只含尾指針循環單鏈表**/
    //定義鏈表並初始化 
    LoopLinkList list3; 
    InitList(&list3);
    //按既定數據建立鏈表
    BuildList_H(&list3,waitInserted,arrLength);    //list3指向表尾
    //測試 
    printf("%c\n",list3->next->next->next->next->next->next->next->next->next->data); //T
    
    
    /**4.尾插法建立只含尾指針循環單鏈表**/
    //定義鏈表並初始化 
    LoopLinkList list4;  
    InitList(&list4);
    //按既定數據建立鏈表
    BuildList_H(&list4,waitInserted,arrLength);    //list4指向表尾
    //測試 
    printf("%c\n",list4->next->next->next->next->next->next->next->next->next->next->data); //A 
    
    printf("\nEND!");
    return 0;
} 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM