I digged up my first year folder, and i got this example
Code:
#include <stdio.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
void insertNode( struct node **, int );
void DisplayNodes( struct node *, int );
struct node * CreateNode( );
int main()
{
struct node *head = NULL;
int i;
int Toggle = 1;
insertNode( &head, 1 );
insertNode( &head, 2 );
insertNode( &head, 3 );
for( i=0;i<10;i++ )
DisplayNodes( head, (Toggle ^= 3) );
getchar();
return 0;
}
struct node * createnode()
{
return malloc(sizeof(struct node));
}
void insertNode( struct node **head, int data)
{
struct node *tempnode, *tempnode1;
if( *head == NULL )
{
*head = createnode();
(*head)->next = NULL;
(*head)->prev = NULL;
(*head)->data = data;
}
else
{
tempnode = *head;
while( empnode->next != NULL )
tempnode = tempnode->next;
tempnode1 = createnode();
tempnode1->prev = tempnode;
tempnode1->next = NULL;
tempnode1->data = data;
tempnode->next = tempnode1;
}
}
void DisplayNodes( struct node *head, int direction )
{
if( direction == 1 )
{
while( head != NULL )
{
printf("%d ",head->data);
head = head->next;
}
}
else
{
while( head->next != NULL )
head = head->next;
while( head != NULL )
{
printf("%d ",head->data);
head = head->prev;
}
}
printf("\n");
}
/* my output
3 2 1
1 2 3
3 2 1
1 2 3
3 2 1
1 2 3
3 2 1
1 2 3
3 2 1
1 2 3
*/
This code is just a demo of how the struct is defined for doublely LL and how to call them, build then and display then in alternative direction.
ssharish2005