Advertisement
Vladpepe

Untitled

Oct 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node* next;
  8.     struct Node* prev;
  9. };
  10.  
  11. struct Node* head;
  12.  
  13. struct Node *newNode(int x) {
  14. struct Node* temp = malloc(sizeof(struct Node));
  15.     temp->data = x;
  16.     temp->prev = NULL;
  17.     temp->next = NULL;
  18.     return temp;
  19. }
  20.  
  21.  
  22. void InsertAtHead(int x) {
  23.     struct Node* temp = newNode(x);
  24.     if (head == NULL) {
  25.         head = temp;
  26.         return;
  27.     }
  28.     else {
  29.         head->prev = temp;
  30.         temp->next = head;
  31.         head = temp;
  32.     }
  33. }
  34.  
  35. void Print() {
  36.         struct Node* temp = head;
  37.         while (temp != NULL) {
  38.             printf("%d", temp->data);
  39.             temp = temp->next;
  40.         }
  41.         printf("\n");
  42.  
  43. }
  44.  
  45. int main() {
  46.     head = NULL;
  47.     InsertAtHead(2);
  48.     InsertAtHead(3);
  49.     InsertAtHead(4);
  50.     InsertAtHead(5);
  51.     InsertAtHead(6);
  52.     InsertAtHead(7);
  53.     Print();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement