C言語は双方向チェーンテーブル削除ノード、挿入ノード、双方向出力などの操作を実現する


#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: 
"); 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("

"); break; } } p->next = NULL; head = head ->next; head->pre = NULL; return head; } // 、 void printDLink(DlinkedList_Node *head) { DlinkedList_Node *p,*s; p = head; printf(" :
"); while(p) { printf("%d ",p->data); s = p; p = p->next; } printf("
:
"); while(s) { printf("%d ",s->data); s = s->pre; } printf("

"); } // 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("
"); 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: 1991 please input the data: 1992 please input the data: 2013 please input the data: 2014 please input the data: 512 please input the data: 420 please 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 ******************************/