/* compare two string represented as LL similar to strcmp */
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
char c;
struct Node *next;
}Node;
Node* newNode(char c)
{
Node *temp = (Node*) malloc (sizeof(Node));
temp->c = c;
temp->next = NULL;
return temp;
};
int compare( Node *list1, Node *list2)
{
while (list1 && list2 && (list1->c == list2->c))
{
list1 = list1->next;
list2 = list2->next;
}
if (list1 && list2)
return (list1->c > list2->c) ? 1 : -1;
if (list1 && !list2)
return 1;
if (!list1 && list2)
return -1;
return 0;
}
int main()
{
Node *list1 = newNode('p');
list1->next = newNode('e');
list1->next->next = newNode('a');
list1->next->next->next = newNode('c');
Node *list2 = newNode('p');
list2->next = newNode('e');
list2->next->next = newNode('a');
list2->next->next->next = newNode('c');
list2->next->next->next->next = newNode('e');
printf("compare result %d\n", compare(list1, list2));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
char c;
struct Node *next;
}Node;
Node* newNode(char c)
{
Node *temp = (Node*) malloc (sizeof(Node));
temp->c = c;
temp->next = NULL;
return temp;
};
int compare( Node *list1, Node *list2)
{
while (list1 && list2 && (list1->c == list2->c))
{
list1 = list1->next;
list2 = list2->next;
}
if (list1 && list2)
return (list1->c > list2->c) ? 1 : -1;
if (list1 && !list2)
return 1;
if (!list1 && list2)
return -1;
return 0;
}
int main()
{
Node *list1 = newNode('p');
list1->next = newNode('e');
list1->next->next = newNode('a');
list1->next->next->next = newNode('c');
Node *list2 = newNode('p');
list2->next = newNode('e');
list2->next->next = newNode('a');
list2->next->next->next = newNode('c');
list2->next->next->next->next = newNode('e');
printf("compare result %d\n", compare(list1, list2));
return 0;
}
No comments:
Post a Comment