#include <iostream> using namespace std ; #define NR(x) (sizeof(x)/sizeof(x[0])) class node { public : int number ; node *next ; node *prev ; void Init_header(node **header); void traver_links(node *header); void top_append_links(node **header , int value); void tail_append_links(node **header , int value); int delete_links(node **header , int value); }; int main(void) { int array[] = {1,2,3,4,5,6,7,8,9}; node *header = NULL ; node list ; list.Init_header(&header); int i ; for(i = 0 ; i < NR(array) ; i++) { // list.top_append_links(&header ,array[i]) ; list.tail_append_links(&header , array[i]); } list.traver_links(header); int num ; cout << "Pls input a number to delete:" << endl ; cin >> num ; list.delete_links(&header , num); list.traver_links(header); system("pause"); return 0 ; } void node::Init_header(node **header) { *header = new node[10] ; if(NULL == header) return ; (*header)->next = *header ; (*header)->prev = *header ; } void node::top_append_links(node **header , int value) { node *New = NULL ; New = new node[10] ; if(NULL == New) return ; New->number = value ; New->next = (*header)->next ; (*header)->next = New ; New->prev = *header ; New->next->prev = New ; } void node::traver_links(node *header) { node *tmp; for(tmp = header->next ; tmp != header ; tmp = tmp->next ) { cout<<' '<< tmp->number ; } cout << endl ; } void node::tail_append_links(node **header , int value) { node *New = NULL ; New = new node[10] ; if(NULL == New) return ; New->number = value ; New->prev =(*header)->prev ; (*header)->prev = New ; New->next = *header ; New->prev->next = New ; } int node::delete_links(node **header , int value) { node *tmp; node *fr ; for(tmp = (*header)->next ; tmp != *header ; ) { if(tmp->number == value) { fr = tmp->next; tmp->prev->next = tmp->next ; tmp->next->prev = tmp->prev ; delete tmp ; tmp = fr ; continue ; } tmp = tmp->next ; } }
时间: 2024-09-11 05:47:25