#include <stdio.h>
struct node {
unsigned short int data;
struct node *next;
struct node *prev;
};
void display_f(struct node *head){ // foraward printing
// display forward
struct node *temp_hdr;
while (head != NULL){
printf (" >> %d ", head->data);
head=head->next;
}
printf("\n \n ");
}
void display_r(struct node *head){ // reverse printing
while (head->next != NULL) // shift the head to the last elem so that we can perform head->prev
head=head->next;
while(head != NULL){ // Now iterate through the list reverse with prev pointer
printf(" << %d", head->data);
head=head->prev;
}
}
void insert(struct node **head, int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
if(*head == NULL){
temp->next=NULL;
temp->prev=NULL;
*head = temp;
return;
}
temp->next= *head;
(*head)->prev=temp;
temp->prev=NULL;
*head=temp;
}
int main() {
//code
struct node *head=NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
insert(&head, 50);
display_f(head);
display_r(head);
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.