Guest User

Untitled

a guest
Oct 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct p {
  5. int data;
  6. struct p *next;
  7. struct p *prev;
  8. } list;
  9.  
  10. list* init(int a);
  11. list* insert_before(list* pc, int num);
  12. list* start(list* pc);
  13. list* end(list* pc);
  14. void print(list* pc);
  15.  
  16. int main() {
  17. list *start, *end;
  18. int a, n;
  19. printf("Input root value of list: ");
  20. scanf(" %d", &a);
  21. list *head = init(a);
  22. printf("Input quantity values to insert them before root: ");
  23. scanf(" %d", &n);
  24. int *values = (int*)malloc(n * sizeof(int));
  25. for (int i = 0; i < n; i++) {
  26. scanf(" %d", &values[i]);
  27. insert_before(head, values[i]);
  28. }
  29. start = start(head);
  30. end = end(head);
  31. print(start);
  32. return 0;
  33. }
  34.  
  35. list* init(int a) {
  36. list* root;
  37. root = (list*)malloc(sizeof(list));
  38. root->data = a;
  39. root->next = NULL;
  40. root->prev = NULL;
  41. return root;
  42. }
  43.  
  44. list* insert_before(list* pc, int num) {
  45. list* res, *p;
  46. res = (list*)malloc(sizeof(list));
  47. p = pc->prev;
  48. pc->prev = res;
  49. res->data = num;
  50. res->prev = p;
  51. res->next = pc;
  52. if (p != NULL) p->next = res;
  53. return res;
  54. }
  55.  
  56. list* start(list* pc) {
  57. while (pc->prev != NULL) {
  58. pc = pc->prev;
  59. }
  60. return pc;
  61. }
  62.  
  63. list* end(list* pc) {
  64. while (pc != NULL) {
  65. pc = pc->next;
  66. }
  67. return pc;
  68. }
  69.  
  70. void print(list* pc) {
  71. int i = 0;
  72. list* p;
  73. p = pc;
  74. do {
  75. printf("Element %d: %dn", ++i, p->data);
  76. p = p->next;
  77. } while (p != NULL);
  78. return;
  79. }
  80.  
  81. start = start(head);
  82. end = end(head);
Add Comment
Please, Sign In to add comment