Y

#### yang2006

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

class Node

{

friend class List;

public:

Node(int d,Node* p=NULL,Node*

n=NULL):data(d),prev(p),next(n){}

private:

int data;

Node* prev;

Node* next;

};

class List

{

public:

List():head(NULL){}

~List()

{

Node* cur=head;

Node* prev=NULL;

while(cur!=NULL)

{

prev=cur;

cur=cur->next;

delete prev;

}

}

void insert(int data)

{

Node* node=new Node(data);

if(head=NULL)

{

head=node;

return;

}

Node* cur=head;

while(cur->next!=NULL)

{

if(data<=cur->data)

{

if(cur->prev==NULL)

{

head=node;

head->next=cur;

cur->prev=head;

}

else

{

node->prev=cur->prev;

node->next=cur;

cur->prev->next=node; //???

}

return;

}

else

{

cur=cur->next;

}

}

if(cur->data<=data)

{

cur->next=node;

node->prev=cur;

}

else

{

node->next=cur;

node->prev=cur->prev;

cur->prev->next=node;

}

}

void DelNode(int da)

{

Node* cur=head;

while(cur->next!=NULL)

{

if(cur->data==da)

{

if(cur->prev==NULL)

{

head=cur->next;

delete cur;

}

else

{

cur->prev->next=cur->next;

cur->next->prev=cur->prev;

delete cur;

}

return;

}

else

{

cur=cur->next;

}

}

if(cur->data==da)

{

cur->prev->next=NULL;

delete cur;

}

}

void display()

{

Node* cur=head;

while(cur!=NULL)

{

printf("%d\n",cur->data);

cur=cur->next;

}

}

private:

Node* head;

};

int main(int argc, char *argv[])

{

List l;

l.insert(10);

l.insert(100);

l.insert(150);

l.insert(145);

l.insert(200);

l.insert(2);

l.insert(180);

l.insert(135);

l.insert(130);

l.insert(1);

l.insert(1);

l.insert(200);

l.display();

printf("*******************\n");

l.DelNode(200);

l.display();

system("PAUSE");

return 0;

}