c數據結構之單鏈表操作

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


typedef struct Node{
       int data;
       struct Node *next;       
}Linklist;




void createLink(Linklist *h){
    Linklist  *p1, *p;
    p1 = p = h;
    int linkLength;
    printf("請輸入鏈表的長度:  ");
    scanf("%d", &linkLength);
    int i;
    for(i = 0;i < linkLength; i ++){
           printf("請輸入鏈表的第%d結點的值:  ", i + 1);
           scanf("%d", &p->data);
            //p->data = 2 + i;
            p = (Linklist *)malloc(sizeof(Linklist));
            p1->next = p;
            p1 = p;
    }
    p1->next = NULL;
}


void insertNodeByIndex(Linklist * list){
    int index;
    printf("請輸入要插入的位置下標:  ");
    scanf("%d", &index);
    int i ;
    for(i = 0; i < index - 2; i ++){
          list = list->next;
    }
    Linklist  *p;
    p = (Linklist *)malloc(sizeof(Linklist));
    printf("請輸入要插入的值:  ");
    scanf("%d", &p->data);
    p->next = list->next;
    list->next = p;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
}


void deleteNodeByIndex(Linklist * list){
     int index;
     printf("請輸入要刪除的位置下標:  ");
     scanf("%d", &index);
     int i;
     for(i = 0; i < index - 2; i ++ ){
             list = list->next;
     } 
     list->next = list->next->next;
}




void printLink(Linklist *list){
     while(list->next !=  NULL){
                  printf("%d\t", list->data);
                  list = list->next;
    }
    printf("\n");
}




void sortLink(Linklist *list){
     int count = getLinkLength(list);
     if(count == 0){
              printf("沒有結點");
     }else if(count == 2){
           if(list->data > list->next->data){
                         list->next->next = list;
                         list->next = NULL;
           }
     }else if(count > 2){
           int i;
           for(i = 0; i < count - 1; i ++){
                 if(list->data > list->next->data){//如果前面大於後面 
                      list->next = list->next->next;
                      list->next = list;  
                 }
           }
     }
     
}


int getLinkLength(Linklist *list){
    int count = 0;
    while(list->next !=  NULL){            
        count ++; 
        list = list->next;
    }
    printf("鏈表的長度爲: %d \n",count); 
    return count;
}


int main(){
    Linklist *list;
    list = (Linklist *)malloc(sizeof(Linklist));
    int index;
    while(index != 0){
                 printf("\n 1 建立鏈表 \n 2 插入結點 \n 3 刪除結點 \n 4 排序鏈表 \n 5 打印鏈表\n 6 打印長度 \n 請輸入選項:  ");
                 scanf("%d", &index); 
                 switch(index){
                      case 1 :    createLink(list); break;
                      case 2 :    insertNodeByIndex(list); break;
                      case 3 :    deleteNodeByIndex(list); break;
                      case 4 :    sortLink(list); break;  
                      case 5 :    printLink(list); break;
                      case 6 :    getLinkLength(list); break; 
                 }
    }
    system("pause");
    return 0;
}
發佈了23 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章