fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct node{
  4. int val;
  5. struct node* next;
  6. struct node* prev;
  7. }NODE;
  8.  
  9. struct node* head=0;
  10. struct node* tail=0;
  11. int addNode(int v){
  12. if(head==0){
  13. struct node* n=malloc(sizeof(NODE));
  14. n->val=v;
  15. head=n;
  16. tail=n;
  17. n->prev=0;
  18. }
  19. else{
  20. struct node* temp=head;
  21. while(temp->next != 0)
  22. {
  23. temp=temp->next;
  24. }
  25. struct node* n=malloc(sizeof(NODE));
  26. n->val=v;
  27. n->prev=temp;
  28. temp->next=n;
  29. n->next=0;
  30. tail=n;
  31. }
  32. }
  33.  
  34. int addBeg(int v){
  35. struct node* n=malloc(sizeof(NODE));
  36. n->val=v;
  37. n->next=head;
  38. head->prev=n;
  39. head=n;
  40. n->prev=0;
  41. }
  42.  
  43. int addValAt(int v,int p){
  44. int c=1;
  45. struct node* temp=head;
  46. while(c != p)
  47. {
  48. c++;
  49. temp=temp->next;
  50. }
  51. struct node* n=malloc(sizeof(NODE));
  52. n->val=v;
  53. n->next=temp->next;
  54. temp->next->prev=n;
  55. n->prev=temp;
  56. temp->next=n;
  57.  
  58. }
  59.  
  60. void print(){
  61. struct node* temp=head;
  62. while(temp != 0){
  63. printf("%d",temp->val);
  64. printf("-->");
  65. temp=temp->next;
  66. }
  67. printf("\n\n");
  68. }
  69. void printRev(){
  70. struct node* temp=tail;
  71. while(temp != 0){
  72. printf("%d",temp->val);
  73. printf("-->");
  74. temp=temp->prev;
  75. }
  76. printf("\n\n");
  77. }
  78.  
  79.  
  80.  
  81. int main(void) {
  82. int i=0;
  83. for(;i<10;i++){
  84. addNode(i);
  85. }
  86. addBeg(5);
  87. addValAt(7,2);
  88. printRev();
  89. print();
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
9-->8-->7-->6-->5-->4-->3-->2-->1-->7-->0-->5-->

5-->0-->7-->1-->2-->3-->4-->5-->6-->7-->8-->9-->