Created
October 4, 2015 12:45
-
-
Save kevtainer/f9ee34d1be158709535a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct Node { | |
int data; | |
struct Node *next; | |
}; | |
Node* Insert(Node *head,int data); | |
Node* Delete(Node *head,int position); | |
Node* Reverse(Node *head); | |
int CompareLists(Node *headA, Node *headB); | |
int main() { | |
Node *headA = Insert(NULL, 5); | |
headA = Insert(headA, 8); | |
headA = Insert(headA, 12); | |
Node *headB = Insert(NULL, 5); | |
headB = Insert(headB, 8); | |
//headB = Insert(headB, 12); | |
//head = Reverse(head); | |
//cout << "Hello, World!" << endl; | |
int result = CompareLists(headA, headB); | |
return 0; | |
} | |
Node* Delete(Node *head, int position) | |
{ | |
if (position == 0) { | |
return head->next; | |
} | |
head->next = Delete(head->next, position - 1); | |
return head; | |
} | |
Node* Insert(Node *head,int data) | |
{ | |
Node *newHead; | |
newHead = new Node; | |
newHead->data = data; | |
newHead->next = head; | |
head = newHead; | |
return head; | |
} | |
int CompareLists(Node *headA, Node* headB) | |
{ | |
int identical = 1; | |
if (headA == NULL || headB == NULL) { | |
return (headA == NULL && headB == NULL) ? 1 : 0; | |
} | |
while (headA->next != NULL && identical == 1) { | |
if (headA->data != headB->data) { | |
identical = 0; | |
continue; | |
} | |
headA = headA->next; | |
headB = headB->next; | |
} | |
return identical; | |
} | |
Node* Reverse(Node *head) | |
{ | |
if (head == NULL) { | |
return head; | |
} | |
Node *temp = new Node(); | |
while (head != NULL) { | |
temp->data = head->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment