程序代码:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct node {
int data;
struct node * next;
}Node;
/** 判等 */
int equals(Node *headA, Node *headB) {
Node *curNodeA=headA;
Node *curNodeB=headB;
while(curNodeA!=NULL&&curNodeB!=NULL){
if(curNodeA->data!=curNodeB->data){
return FALSE;
}
curNodeA=curNodeA->next;
curNodeB=curNodeB->next;
}
if(curNodeA!=NULL||curNodeB!=NULL){
return FALSE;
}
return TRUE;
}
/** 创建链表 */
Node* createLink(int arr[],int len){
Node *head=(Node*)malloc(sizeof(Node));
Node *curNode=head;
for(int i=0;i<len;i++){
curNode->data=arr[i];
if(i<len-1){
Node *tmpNode=(Node*)malloc(sizeof(Node));
curNode->next=tmpNode;
}else{
curNode->next=NULL;
}
curNode=curNode->next;
}
return head;
}
/** 遍历链表 */
void display(Node *head){
Node *curNode=head;
while(curNode!=NULL){
printf("%d\t",curNode->data);
curNode=curNode->next;
}
}
/** 释放 */
void freeLink(Node *node){
if(node!=NULL){
freeLink(node->next);
free(node);
}
}
int main(){
int arrA[]={1,2,3,4,5,6};
int arrB[]={1,2,3,4,5};
Node *headA=createLink(arrA,6);
Node *headB=createLink(arrB,5);
printf("\nheadA:\t");
display(headA);
printf("\nheadB:\t");
display(headB);
int result=equals(headA,headB);
printf("\nheadA equals headB?\t%s",result==0?"false":"true");
freeLink(headA);
freeLink(headB);
return 0;
}
[此贴子已经被作者于2019-11-3 10:10编辑过]