Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6. int data;
  7. struct Node *next;
  8. };
  9.  
  10. Node* Insert(Node *head,int data);
  11. Node* Delete(Node *head,int position);
  12. Node* Reverse(Node *head);
  13. int CompareLists(Node *headA, Node *headB);
  14.  
  15. int main() {
  16. Node *headA = Insert(NULL, 5);
  17. headA = Insert(headA, 8);
  18. headA = Insert(headA, 12);
  19. Node *headB = Insert(NULL, 5);
  20. headB = Insert(headB, 8);
  21. //headB = Insert(headB, 12);
  22.  
  23. //head = Reverse(head);
  24. //cout << "Hello, World!" << endl;
  25. int result = CompareLists(headA, headB);
  26. return 0;
  27. }
  28.  
  29. Node* Delete(Node *head, int position)
  30. {
  31. if (position == 0) {
  32. return head->next;
  33. }
  34.  
  35. head->next = Delete(head->next, position - 1);
  36.  
  37. return head;
  38. }
  39.  
  40. Node* Insert(Node *head,int data)
  41. {
  42. Node *newHead;
  43.  
  44. newHead = new Node;
  45. newHead->data = data;
  46. newHead->next = head;
  47. head = newHead;
  48.  
  49. return head;
  50. }
  51.  
  52. int CompareLists(Node *headA, Node* headB)
  53. {
  54. int identical = 1;
  55.  
  56. if (headA == NULL || headB == NULL) {
  57. return (headA == NULL && headB == NULL) ? 1 : 0;
  58. }
  59.  
  60. while (headA->next != NULL && identical == 1) {
  61. if (headA->data != headB->data) {
  62. identical = 0;
  63. continue;
  64. }
  65.  
  66. headA = headA->next;
  67. headB = headB->next;
  68. }
  69.  
  70. return identical;
  71. }
  72.  
  73. Node* Reverse(Node *head)
  74. {
  75. if (head == NULL) {
  76. return head;
  77. }
  78.  
  79. Node *temp = new Node();
  80.  
  81. while (head != NULL) {
  82. temp->data = head->data;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement