You are on page 1of 1

#include <stdio.

h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
}; //*head, *tail;
void insertLast(struct Node *head, struct Node *tail, int item) {
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->data = item;
node->next = NULL;
if(head == NULL && tail == NULL) {
node->prev = NULL;
head = tail = node;
} else {
tail->next = node;
node->prev = tail;
tail = node;
}
}
void printList(struct Node *head) {
struct Node* curr = head;
if(head == NULL)
printf("List is empty!");
else {
do {
printf("%d\t", curr->data);
curr = curr->next;
} while(curr != NULL);
}
printf("\n");
}
int main () {
struct Node *head, *tail;
head = tail = NULL;
int n;
scanf("%d", &n);
for(int i=0; i<n; i++) {
int item;
scanf("%d", &item);
insertLast(head, tail, item);
}
printList(head);
//getch();
return 0;
}

You might also like