c语言数据结构链表的题
											要求比较两个链表是否相等,很迷,这个怎么比较
										
					
	
程序代码:#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编辑过]

程序代码:
#include <stdio.h>
#include <string.h>
#define BOOL int
#define ElemType int
#define TRUE 1
#define FALSE 0
typedef struct node  
{      
    ElemType data;   
    struct node * next;  
}node, *ListNode; 
BOOL getIntersectionNode(ListNode headA, ListNode headB) 
{   
    ListNode pL = headA;      
    ListNode pS = headB;      
    int lenA = 0;   
    while(pL != NULL)
    {           
         lenA++;         
         pL = pL->next;        
    }     
    int lenB = 0;   
    while(pS != NULL)
    {         
        lenB++;            
        pS = pS->next;        
    }      
    pL = headA;        
    pS = headB;     
//    int len = lenA - lenB;     
//    if(len == 0)
//    {           
//        return FALSE;        
//    } 
    //直接这样 不是更好 
    if(lenA != lenB)
    {           
        return FALSE;        
    } 
                  
//    while(pL->data != pS->data) 这两个数不相等时 可以直接break;了 
//    {            
//        pL->data = pL->next->data;            
//        pS->data = pS->next->data;            
//        printf(TRUE);        //printf 不是这样用的吧? 
//    }      
//     return FALSE;没理解你的思维 
    //我改成了这样
    while(pL) //由于前面可以确定两链表一样长了  所以只用pL就好 
    {
        if (pL->data != pS->data)
        {
            return FALSE;
        }
        pL = pL->next;
        pS = pS->next;    
    }
    return TRUE;
}[此贴子已经被作者于2019-11-4 21:08编辑过]
