#include <stdio.h>
#include <stdlib.h>
typedef struct node_t {
int data;
struct node_t* prev;
struct node_t* next;
} node;
node* init( void )
{
node* p;
p = (node *)malloc(sizeof(node));
if( p == NULL ) {
printf("malloc error\n");
return 0;
}
p->prev = NULL;
p->next = NULL;
p->data = 0;
return p;
}
/* insert a new node after specified postion, postion 0 is available */
int insert_node( node* head, int pos, int data )
{
node* p;
node* q;
if ( head == NULL || pos < 0 ) {
printf("parameter error\n");
return -1;
}
q = head;
while ( pos-- > 0 && q != NULL ) {
q = q->next;
}
if ( q == NULL ) {
printf("parameter error\n");
return -1;
}
p = (node *)malloc(sizeof(node));
if( p == NULL ) {
printf("malloc error\n");
return -1;
}
p->prev = q;
p->next = q->next;
p->data = data;
/* Is the last node ? */
if ( q->next )
q->next->prev = p;
q->next = p;
return 0;
}
/* insert a new node after specified postion */
int remove_node( node* head, int pos )
{
node* q;
if ( head == NULL || pos <= 0 ) {
printf("parameter error\n");
return -1;
}
q = head;
while ( pos-- > 0 && q != NULL ) {
q = q->next;
}
if ( q == NULL ) {
printf("parameter error\n");
return -1;
}
q->prev->next = q->next;
/* Is the last node ? */
if ( q->next )
q->next->prev = q->prev;
free( q );
return 0;
}
void print_node( node* head )
{
node* p;
p = head->next;
while ( p ) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main( void )
{
node* head;
int n = 5;
head = init();
while ( n > 0 ) {
insert_node( head, 0, n );
n--;
}
print_node( head );
insert_node( head, 10, 10);
insert_node( head, 0, 0);
print_node( head );
insert_node( head, 6, 6);
print_node( head );
remove_node( head, 0 );
remove_node( head, 10 );
remove_node( head, 6 );
print_node( head );
remove_node( head, 6 );
print_node( head );
return 0;
}
#include <stdlib.h>
typedef struct node_t {
int data;
struct node_t* prev;
struct node_t* next;
} node;
node* init( void )
{
node* p;
p = (node *)malloc(sizeof(node));
if( p == NULL ) {
printf("malloc error\n");
return 0;
}
p->prev = NULL;
p->next = NULL;
p->data = 0;
return p;
}
/* insert a new node after specified postion, postion 0 is available */
int insert_node( node* head, int pos, int data )
{
node* p;
node* q;
if ( head == NULL || pos < 0 ) {
printf("parameter error\n");
return -1;
}
q = head;
while ( pos-- > 0 && q != NULL ) {
q = q->next;
}
if ( q == NULL ) {
printf("parameter error\n");
return -1;
}
p = (node *)malloc(sizeof(node));
if( p == NULL ) {
printf("malloc error\n");
return -1;
}
p->prev = q;
p->next = q->next;
p->data = data;
/* Is the last node ? */
if ( q->next )
q->next->prev = p;
q->next = p;
return 0;
}
/* insert a new node after specified postion */
int remove_node( node* head, int pos )
{
node* q;
if ( head == NULL || pos <= 0 ) {
printf("parameter error\n");
return -1;
}
q = head;
while ( pos-- > 0 && q != NULL ) {
q = q->next;
}
if ( q == NULL ) {
printf("parameter error\n");
return -1;
}
q->prev->next = q->next;
/* Is the last node ? */
if ( q->next )
q->next->prev = q->prev;
free( q );
return 0;
}
void print_node( node* head )
{
node* p;
p = head->next;
while ( p ) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main( void )
{
node* head;
int n = 5;
head = init();
while ( n > 0 ) {
insert_node( head, 0, n );
n--;
}
print_node( head );
insert_node( head, 10, 10);
insert_node( head, 0, 0);
print_node( head );
insert_node( head, 6, 6);
print_node( head );
remove_node( head, 0 );
remove_node( head, 10 );
remove_node( head, 6 );
print_node( head );
remove_node( head, 6 );
print_node( head );
return 0;
}