資料結構 鏈表 c實現

來源:互聯網
上載者:User

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

typedef struct
{
    char key[10];
    char name[20];
    int age;
}Data;

typedef struct Node    //定義鏈表結構
{
    Data nodeData;
    struct Node *nextNode;
}CLType;

CLType *CLAddEnd(CLType *head,Data nodeData)//追加節點
{
    CLType *node,*htemp;
    if(!(node=(CLType*)malloc(sizeof(CLType))))
    {
        printf("申請記憶體失敗!\n");
        return NULL;
    }
    else
    {
        node->nodeData=nodeData;
        node->nextNode=NULL;
        if(head==NULL)
        {
            head=node;
            return head;
        }
        htemp=head;
        while(htemp->nextNode!=NULL)
        {
            htemp=htemp->nextNode;
        }
        htemp->nextNode=node;
        return head;
    }
}
CLType *CLAddFirst(CLType *head,Data nodeData)//插入頭結點
{
    CLType *node;
    if(!(node=(CLType*)malloc(sizeof(CLType))))
    {
        printf("申請記憶體失敗!\n");
        return NULL;
    }
    else
    {
        node->nodeData=nodeData;
        node->nextNode=head;
        head=node;
        return head;
    }
}
CLType *CLFindNode(CLType *head,char *key)//尋找節點
{
    CLType *htemp;
    htemp=head;
    while(htemp)
    {
        if(strcmp(htemp->nodeData.key,key)==0)
        {
            return htemp;
        }
        htemp=htemp->nextNode;
    }
    return NULL;
}

CLType *CLInsertNode(CLType *head,char *findkey,Data nodeData)//插入節點
{
    CLType *node,*nodetemp;
    if(!(node=(CLType*)malloc(sizeof(CLType))))
    {
        printf("申請記憶體失敗!");
        return NULL;
    }
    node->nodeData=nodeData;
    nodetemp=CLFindNode(head,findkey);
    if(nodetemp)
    {
        node->nextNode=nodetemp->nextNode;
        nodetemp->nextNode=node;
    }
    else
    {
        printf("未找到正確的插入位置!\n");
        free(node);
    }
    return head;
}
int CLDeleteNode(CLType *head,char *key)//刪除節點
{
    CLType *node,*htemp;
    htemp=head;
    node=head;
    while(htemp)
    {
        if(strcmp(htemp->nodeData.key,key)==0)
        {
            node->nextNode=htemp->nextNode;
            free(htemp);
            return 1;
        }else
        {
            node=htemp;
            htemp=htemp->nextNode;
        }
    }
    return 0;
}

int CLLength(CLType *head)//計算鏈表長度
{
    CLType *htemp;
    int Len=0;
    htemp=head;
    while(htemp)
    {
        Len++;
        htemp=htemp->nextNode;
    }
    return Len;
}

void CLAllNode(CLType *head)//顯示所有節點
{
    CLType *htemp;
    Data nodeData;
    htemp=head;
    printf("當前鏈表共有%d個節點。鏈表所有資料如下:\n",CLLength(head));
    while(htemp)
    {
        nodeData=htemp->nodeData;
        printf("節點(%s,%s,%d)\n",nodeData.key,nodeData.name,nodeData.age);
        htemp=htemp->nextNode;
    }
}

int main()
{
    CLType *node,*head=NULL;
    Data nodeData;
    char key[10];char findkey[10];

    printf("先輸入鏈表中各資料,格式為:學號 姓名 年齡\n");
    do
    {
        fflush(stdin);
        scanf("%s",nodeData.key);
        if(strcmp(nodeData.key,"0")==0)
        {
            break;
        }
        else
        {
            scanf("%s%d",nodeData.name,&nodeData.age);
            head=CLAddEnd(head,nodeData);
        }
    }while(1);
    CLAllNode(head);
    fflush(stdin);
    printf("\n輸入插入的節點的關鍵字:");
    scanf("%s",findkey);
    printf("\n輸入插入節點的資料:學號 姓名 年齡:");
    scanf("%s%s%d",nodeData.key,nodeData.name,&nodeData.age);
    head=CLInsertNode(head,findkey,nodeData);
    CLAllNode(head);

    printf("\n輸入要刪除節點的關鍵字:");
    //fflush(stdin);
    scanf("%s",key);
    CLDeleteNode(head,key);
    CLAllNode(head);

    printf("\n輸入尋找節點的關鍵字:");
    //fflush(stdin);
    scanf("%s",key);
    node=CLFindNode(head,key);
    if(node)
    {
        nodeData=node->nodeData;
        printf("關鍵字%s對應的節點為(%s,%s,%d)\n",key,nodeData.key,nodeData.name,nodeData.age);
    }
    else{
        printf("在鏈表中找不到關鍵字為%s的節點\n",key);
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.