Advertisement
kobikirmayer

Untitled

Mar 27th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. struct node
  2. {
  3.     int data;
  4.     struct node *next;
  5.     struct node *prev;
  6. };
  7. struct node *GetNewNode(int x);
  8. void InsertAtHead(int x);
  9. struct node *head;
  10.  
  11. struct node *GetNewNode(int x){
  12.     struct node *newNode= (struct node *)malloc(sizeof(struct node));
  13.     newNode->data=x;
  14.     newNode->next=NULL;
  15.     newNode->prev=NULL;
  16.     return newNode;
  17. }
  18.  
  19. void InsertAtHead(int x){
  20.      struct node *newNode = GetNewNode(x);
  21.      if (head==NULL){
  22.         head=newNode;
  23.         return;
  24.      }
  25.      head->prev=newNode;
  26.      newNode->next=head;
  27.      head=newNode;
  28. }
  29.  
  30. void print(){
  31.      struct node *temp=head;
  32.      printf("Forward: ");
  33.      while (temp!=NULL){
  34.         printf("%i " , temp->data);
  35.         temp=temp->next;
  36.      }
  37.      printf("\n");
  38. }
  39.  
  40. void printReverse(){
  41.      struct node *temp=head;
  42.      if (temp==NULL) return;
  43.      while (temp->next!=NULL) temp=temp->next;
  44.      printf("Reverse: ");
  45.      while (temp!=NULL){
  46.         printf("%i " , temp->data);
  47.         temp=temp->prev;
  48.      }
  49.      printf("\n");
  50. }
  51.  
  52. int main()
  53. {
  54.     InsertAtHead(5);
  55.     InsertAtHead(8);
  56.     InsertAtHead(9);
  57.     print();
  58.     printReverse();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement