C Program
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
void insertStart(struct Node** head,int data)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->prev=NULL;
newNode->next=*head;
if(*head!=NULL)
(*head)->prev=newNode;
*head=newNode;
}
int main()
{
int n,i,x;
scanf("%d",&n);
struct Node* head=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
insertStart(&head,x);
}
struct Node* temp=head;
struct Node* last;
while(temp!=NULL)
{
printf("%d ",temp->data);
if(temp->next==NULL)
last=temp;
temp=temp->next;
}
printf("\n");
while(last!=NULL)
{
printf("%d ",last->data);
last=last->prev;
}
return 0;
}
0 Comments