close

前言:設計一支程式具備以下功能

1. 在雙向鏈結串列中加入節點

2. 在雙向鏈結串列中刪除節點


#include<stdio.h>
#include<string.h>
struct node{

struct node *pre;

int data;

struct node *next;

};

void insertDoubleList(struct node **head,struct node *p,struct node *q);
void deleteDoubleList(struct node **head,struct node *p);
void printList(struct node *head);

int main(void) 
{

struct node *head=NULL;

struct node A={NULL,1,NULL};

struct node B={NULL,2,NULL};

struct node C={NULL,3,NULL};

struct node D={NULL,4,NULL};
struct node E={NULL,5,NULL};

struct node Q={NULL,17,NULL};

head=&A;

A.pre=NULL;

A.next=&B;

B.pre=&A;

B.next=&C;

C.pre=&B;

C.next=&D;

D.pre=&C;

D.next=&E;

E.pre=&D;

E.next=NULL;


printList(head);

insertDoubleList(&head,&B,&Q);

printList(head);

deleteDoubleList(&head,&B);

printList(head);

return 0;
}


void insertDoubleList(struct node **head,struct node *p,struct node *q)
{

if(*head==NULL)

*head=q;

else if(p->next==NULL){

q->pre=p;

p->next=q;

}else{

q->pre=p;

q->next=p->next;

p->next=q;

p->next->pre=q;

}

}

void deleteDoubleList(struct node **head,struct node *p)
{

if(*head==NULL)

printf("Linked List is empty.\n");

else if(p->next==NULL)

p->pre->next=p->next;

else{

p->pre->next=p->next;

p->next->pre=p->pre;

}

}

void printList(struct node *head)
{

struct node *P=head;


if(P==NULL){

printf("stack is empty.\n");

return;

}

printf("Linked List data:");

while(1)

{

printf("%d",P->data);

if(P->next==NULL)

{

printf("\n");

break;

}

else

{

P=P->next;

printf("->");

}

}

}


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 GavinHuang 的頭像
    GavinHuang

    Gavin的部落格

    GavinHuang 發表在 痞客邦 留言(0) 人氣()