Guest User

Untitled

a guest
Oct 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 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 *s, *e;
  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. s = start(head);
  30. e = end(head);
  31. print(head);
  32. printf("nnStart address of list: %pnEnd address of list: %pnnstart %dnend %d", s, e, *s, *e);
  33. return 0;
  34. }
  35.  
  36. list* init(int a) {
  37. list* root;
  38. root = (list*)malloc(sizeof(list));
  39. root->data = a;
  40. root->next = NULL;
  41. root->prev = NULL;
  42. return root;
  43. }
  44.  
  45. list* insert_before(list* pc, int num) {
  46. list* res, *p;
  47. res = (list*)malloc(sizeof(list));
  48. p = pc->prev;
  49. pc->prev = res;
  50. res->data = num;
  51. res->prev = p;
  52. res->next = pc;
  53. if (p != NULL) p->next = res;
  54. return res;
  55. }
  56.  
  57. list* start(list* pc) {
  58. while (pc->prev != NULL) {
  59. pc = pc->prev;
  60. }
  61. return pc;
  62. }
  63.  
  64. list* end(list* pc) {
  65. while (pc->next != NULL) {
  66. pc = pc->next;
  67. }
  68. return pc;
  69. }
  70.  
  71. void print(list* pc) {
  72. int i = 0;
  73. list* p;
  74. p = pc;
  75. do {
  76. printf("%dn", p->data);
  77. p = p->prev;
  78. } while (p != NULL);
  79. return;
  80. }
Add Comment
Please, Sign In to add comment