Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct linkedlist
  4. {
  5. int data;
  6. struct linkedlist *next;
  7. } Node;
  8.  
  9. Node *makeNewNode(int data)
  10. {
  11. Node *new = (Node*) malloc(sizeof(Node));
  12. new->data = data;
  13. new->next = NULL;
  14. return new;
  15. }
  16.  
  17. Node *insert_after_cur(Node *cur, int data)
  18. {
  19. Node *new = makeNewNode(data);
  20. if(cur == NULL) cur = new;
  21. else if(cur->next == NULL) cur->next = new;
  22. else{
  23. new->next=cur->next;
  24. cur->next=new;
  25. }
  26. cur = new;
  27. return cur;
  28. }
  29.  
  30. void split(Node *head, Node **L1, Node **L2)
  31. {
  32. Node *cur, *cur1, *cur2;
  33. cur = head;
  34. cur1=NULL;
  35. cur2=NULL;
  36. while(cur!=NULL){
  37. if((cur->data)%2!=0){
  38. if((*L1)==NULL){
  39. cur1 = cur;
  40. *L1 = cur1;
  41. }
  42. else {
  43. cur1->next=cur;
  44. cur1 = cur;
  45. }
  46. }
  47. else{
  48. if((*L2)==NULL){
  49. cur2 = cur;
  50. *L2 = cur2;
  51. }
  52. else {
  53. cur2->next=cur;
  54. cur2 = cur;
  55. }
  56. }
  57. cur=cur->next;
  58. }
  59. if(cur1 != NULL) cur1->next = NULL;
  60. if(cur2 != NULL) cur2->next = NULL;
  61. }
  62.  
  63. int main()
  64. {
  65. Node *L, *cur;
  66. Node **L1, **L2;
  67. int i;
  68.  
  69. L = NULL;
  70. L1 = malloc(sizeof(Node*));
  71. L2 = malloc(sizeof(Node*));
  72. *L1 = NULL;
  73. *L2 = NULL;
  74.  
  75. cur = L;
  76. L = insert_after_cur(cur,0);
  77. cur = L;
  78. for(i=1; i<20; i++){
  79. cur = insert_after_cur(cur,i);
  80. }
  81. split(L,L1,L2);
  82.  
  83.  
  84. Node *tempnode = (*L1);
  85. while(tempnode!=NULL)
  86. {
  87. printf("%d\n",tempnode->data);
  88. tempnode = tempnode->next;
  89. }
  90.  
  91. tempnode = (*L2);
  92. while(tempnode!=NULL){
  93. printf("%d\n", tempnode->data);
  94. tempnode = tempnode->next;
  95. }
  96. return 0;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement