注册 登录
编程论坛 C语言论坛

c语言数据结构链表的题

呆萌动漫说 发布于 2019-10-30 17:51, 2453 次点击
要求比较两个链表是否相等,很迷,这个怎么比较
10 回复
#2
c小白23332019-10-30 21:54
按照正常的思维
依次比较链表中的数据 直到结束
关于结束 若一个结束 一个没结束一定不相等
定义一个bool型 为false
在for循环中 依次发现不相等则 赋值true 退出循环break;
按照这样的思维来
自己试试 有错解决不了
把程序发出来  给你改
我直接发出程序 作用不大 看后就忘了
#3
呆萌动漫说2019-10-31 20:24
回复 2楼 c小白2333
好的 ,我自己先写写。✧*。٩(^㉨^*)و✧*。
#4
呆萌动漫说2019-11-03 08:40
123456789101112131415161718192021222324252627282930313233343536373839404142434445
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef
struct node  {      
ElemType data;   
 struct node * next;  }
Node;
public ListNode 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){            lenB=lenA;1        }      
  else{           return FALSE;        }     
   void *data;        
while(pL->data!=pS->data){            pL->data=pL.next->data;            pS->data=pS.next->data;            printf(TRUE);        }      
 return FALSE;}
#5
呆萌动漫说2019-11-03 08:40
回复 2楼 c小白2333
我下面写的那个可以吗?
#6
林月儿2019-11-03 09:10
好像不太对
#7
林月儿2019-11-03 10:08
程序代码:
#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编辑过]

#8
呆萌动漫说2019-11-03 10:38
回复 7楼 林月儿
这个比我的那个好看多了,谢谢
#9
bcbbcclbbc2019-11-04 12:05
回复 8楼 呆萌动漫说
不只是程序,头像也挺好看的
#10
c小白23332019-11-04 21:07
你的程序有很多问题
新手能写出来就行
试着理解7楼的判等  
知道你程序的错误 并把7楼的记住
根据你原程序改完变成这样
下次发程序注意规范!!!
程序代码:

#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编辑过]

#11
呆萌动漫说2019-11-09 20:35
回复 10楼 c小白2333
看见了,谢谢
1