// list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct list_node {
int data;
list_node *left;
list_node *right;
} Node;
Node *create_node(int data)
{
Node *node = new Node();
node->data = data;
node->left = node->right = NULL;
return node;
}
void delete_node( Node * p )
{
delete p;
p = NULL ;
}
void insert_tail(Node *root , int data)
{
Node *node = create_node( data );
Node *last_node = root;
while(last_node->right != NULL) last_node = last_node->right;
last_node->right = node;
node->left = last_node;
}
Node *find_node(Node *root , int data )
{
Node *node = root ;
while(node != NULL) {
if( data == node->data ) {
return node;
}
node = node->right;
}
return NULL;
}
void insert_back_node(Node *root, int dat , int data )
{
Node *node = find_node(root,dat);
if( node == NULL || node->left == NULL ) return;
Node *new_node = create_node( data );
node->left->right = new_node;
new_node->right = node;
}
void insert_front_node(Node *root, int dat , int data )
{
Node *node = find_node(root,dat);
if( node == NULL ) return;
Node *new_node = create_node( data );
new_node->right = node->right;
new_node->left = node;
node->right = new_node;
}
void remote_node ( Node *root,int data )
{
Node *node = find_node ( root , data );
if( node == NULL ) {
return ;
} else if ( node->left == NULL ) {
node->right->left = NULL;
} else if ( node->right == NULL ) {
node->left->right = NULL ;
} else {
node->left->right = node->right;
node->right->left = node->left;
}
delete_node( node );
}
void traversal( Node *root )
{
Node *last_node = root;
do{
cout<<last_node->data<<endl ;
last_node = last_node->right;
} while(last_node != NULL ) ;
}
int main(int argc, char* argv[])
{
Node *root = create_node(1);
insert_tail(root,4);
insert_tail(root,5);
insert_tail(root,800);
insert_tail(root,6);
insert_tail(root,7);
Node *p = find_node(root,5);
cout << p <<endl;
if(p!=NULL) cout<<p->data <<endl;
insert_back_node(root,5,20);
insert_front_node(root,1,30);
remote_node ( root , 6 ) ;
traversal( root );
return 0;
}
时间: 2024-10-24 03:56:37