C語言實現雙向鏈表刪除節點、插入節點、雙向輸出等操作

來源:互聯網
上載者:User

標籤:

#include<cstdio>#include<cstdlib>typedef struct DoubleLinkedList{    int data;    struct DoubleLinkedList *pre;    struct DoubleLinkedList *next;}DlinkedList_Node;//建立鏈表DlinkedList_Node* createDLink(){    DlinkedList_Node *head,*p,*s;    int x;    head = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));    p = head;    while(1)    {        printf("please input the data: \n");        scanf("%d",&x);        if(x != 65535)        {            s = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));            s ->data = x;            s-> pre = p;            p->next = s;            p=s;        }        else            {                printf("\n資料輸入結束\n");                break;            }    }    p->next = NULL;    head = head ->next;    head->pre = NULL;    return head;}//順序、反序列印鏈表void printDLink(DlinkedList_Node *head){    DlinkedList_Node *p,*s;    p = head;    printf("正序輸出雙向鏈表:\n");    while(p)    {        printf("%d ",p->data);        s = p;        p = p->next;    }    printf("\n 逆序輸出雙向鏈表: \n");    while(s)    {        printf("%d ",s->data);        s = s->pre;    }    printf("\n \n");}//刪除一個結點DlinkedList_Node* deleteDlinkedList_Node(DlinkedList_Node *head,int i){    DlinkedList_Node *p;    p = head;    if(p->data == i)    {        head = p->next;        head->pre = NULL;        free(p);        return head;    }    while(p)    {        if(p->data == i)        {        p->pre->next = p->next;        p->next->pre = p->pre;        free(p);        return head;        }        p = p->next;    }    printf("沒有找到想要刪除的資料\n");    return head;}//插入一個結點DlinkedList_Node* insertDlinkedList_Node(DlinkedList_Node *head,int i){    DlinkedList_Node *p,*temp;    p = head;    temp = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));    temp ->data = i;    if(i < p->data)//比頭結點資料小,插入到鏈表頭部    {        head = temp;        head->next = p;//此處p為原來的head        head->pre = NULL;        p->pre = head;//此處p為原來的head        return head;    }    while(p != NULL && i > p->data)//尋找合適的插入位置    {        p = p->next;    }    if(i < p->data)//在鏈表中間某處找到合適插入位置    {        temp ->next = p;        temp ->pre = p->pre;        p ->pre->next = temp;        p ->pre = temp;        return head;    }    else//沒有找到合適的位置,只有將資料插入到鏈表尾部    {        p->next = temp;  //遍曆到鏈表尾部,p==NULL        temp ->pre = p;        temp ->next = NULL;        return head;    }}int main(){    DlinkedList_Node *head;    head = createDLink();    printDLink(head);    head = insertDlinkedList_Node(head,1012);    head = deleteDlinkedList_Node(head,1991);    printDLink(head);}/*****************************運行結果如下:please input the data:1991please input the data:1992please input the data:2013please input the data:2014please input the data:512please input the data:420please input the data:65535資料輸入結束正序輸出雙向鏈表:1991 1992 2013 2014 512 420 逆序輸出雙向鏈表:420 512 2014 2013 1992 1991正序輸出雙向鏈表:1012 1992 2013 2014 512 420 逆序輸出雙向鏈表:420 512 2014 2013 1992 1012******************************/

C語言實現雙向鏈表刪除節點、插入節點、雙向輸出等操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.