C語言 實現鏈表

jopen 9年前發布 | 4K 次閱讀 C/C++ C語言

向順序表插入元素的時候需要移動大量的數據

鏈表采用動態存儲分配,

可以根據需要申請內存單元

#include "stdafx.h"

include "stdlib.h"

include "string.h"

typedef struct{ char key[15]; char name[20]; int age; }DATA;

typedef struct Node{ DATA data; struct Node * next;

}ChainListType; // 添加到節點的尾部 ChainListType ChainListAddEnd(ChainListType head,DATA data){ //head 為鏈表的頭指針,data為節點保存的數據 ChainListType node, h; //因為需要動態分配內存 所以需要引入 stdlib.h 頭文件 if (!(node = (ChainListType )malloc(sizeof(ChainListType)))){ printf("為保存的節點數據申請內存失敗"); return NULL; } node->data = data; node->next = NULL; if (head == NULL){ head = node; return head; } h = head; while (h->next!=NULL) h = h->next; h->next = node; return head; } //添加節點到首部 ChainListType ChainListAddFirst(ChainListType head,DATA data){ ChainListType node, h; if (!(node = (ChainListType )malloc(sizeof(ChainListType)))){ printf("為保存的節點數據申請內存失敗"); return NULL; } node->data = data; node->next = head; //指向頭指針所指節點 head = node; //頭指針指向新增節點 return head;

}
//按照關鍵字查找節點
ChainListType * ChainListFind(ChainListType * head,char *key){
    ChainListType *h;
    h = head;
    while (h)
    {
        if (strcmp(h->data.key, key) == 0){      //若節點的關鍵字與傳入關鍵字相同
            return h;                          //  返回該節點指針
            h = h->next;                       //  處理下一個節點
        }
    }
}
//插入節點到鏈表
ChainListType * ChainListInsert(ChainListType *head,char *findkey,DATA data){
    ChainListType * node, *node1;
    if (!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
        printf("為保存的節點數據申請內存失敗");
        return 0;
    }
    node->data = data;
    node1 = ChainListFind(head, findkey);
    if (node1){
        node->next = node1->next;
        node1->next = node;
    }
    else{
        free(node);
        printf("未找到插入位置\n");

    }
    return head;
}
//刪除節點
int ChainListDelete(ChainListType *head, char *key){
    ChainListType *node, *h;
    node = h = head;
    while (h){
        if (strcmp(h->data.key, key) == 0){
            node->next = h->next;
            free(h);
            return 1;
        }
        else{
            node = h;
            h = h->next;
        }
    }
    return 0;
}
void ChainListAll(ChainListType *head){
    ChainListType *h;
    DATA data;
    h = head;
    printf("鏈表所有的數據如下\n");
    while (h)
    {
        data = h->data;
        printf("%s%s%d\n", data.key, data.name, data.age);
        h = h->next;
    }
}
//統計鏈表的長度
int ChainListLength(ChainListType * head){
    ChainListType *h;
    int i = 0;
    h = head;
    while (h){
        i++;
        h = h->next;
    }
    return i;
}</pre> 


實現

int main(){
    ChainListType node, head = NULL;
    DATA data;
    char key[15], findkey[15];
    printf("輸入鏈表中的數據.包括關鍵字,姓名,年齡,關鍵字輸入0\n");
    do{
        fflush(stdin);
        scanf("%s", data.key);
        if (strcmp(data.key, "0") == 0) break; //若輸入0,則退出
        scanf("%s%d", data.name, &data.age);
        head = ChainListAddEnd(head, data);
    } while (1);
    ChainListAll(head);

printf("在鏈表中查找,請輸入關鍵字\n");
fflush(stdin);      //  清空輸入緩沖區
scanf("%s", key);
node = ChainListFind(head, key);
if (node){
    data = node->data;
    printf("關鍵字%s對應的節點數據(%s,%s,%d)\n",key,data.key,data.name,data.age);
}
else{
    printf("在鏈表中未找到關鍵字為%s的節點\n", key);
}

printf("在鏈表中刪除節點,輸入要刪除的關鍵字\n");
fflush(stdin);
scanf("%s", key);
ChainListDelete(head, key);
ChainListAll(head);
//getch();
system("pause");

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!