两个链表的交集 - 免费下载

Linux/uClinux/Unix编程资源 文件大小:25 K

📋 资源详细信息

文件格式
未知
上传用户
上传时间
文件大小
25 K
所需积分
2 积分
推荐指数
⭐⭐⭐ (3/5)

💡 温馨提示:本资源由用户 舞玥 上传分享,仅供学习交流使用。如有侵权,请联系我们删除。

资源简介

两个链表的交集

#include<stdio.h>

#include<stdlib.h>
typedef struct Node{
  int data;
  struct  Node *next;
}Node;
void initpointer(struct Node *p){
  p=NULL;
}
int  printlist(struct Node* head){
  int flag=1;
  head=head->next;
  /*
  因为标记1的地方你用了头结点,所以第一个数据域无效,应该从下一个头元结点开始
  */
  if(head==NULL)
    printf("NULL\n");
  else
  {
    while(head!=NULL)
    {
      if(flag==1)
      {
      printf("%d",head->data);
      flag=0;
      }
      else
      {
        printf(" %d",head->data);
      }
      head=head->next;
    }
    printf("\n");
  }
  return 0;
}
struct Node *creatlist(struct Node *head)
{
     int n;
   struct  Node *p1=(struct Node *)malloc(sizeof(struct Node));
  p1->next=NULL;
while(scanf("%d",&n),n!=-1)
{
  struct Node *pnode=(struct Node *)malloc(sizeof(struct Node));
  pnode->next=NULL;
     pnode->data=n;
  if(head==NULL)
    head=pnode;
  p1->next=pnode;
  p1=pnode;
}
return head;
}
struct Node *Intersect(struct Node *head1, struct Node *head2)
{
struct Node *p1=head1,*p2=head2;/*我这里没有用头指针和头结点,这里是首元结点head1里面就是第一个数据,一定要理解什么事头指针,
头结点,和首元结点
具体你一定要看这个博客:http://blog.sina.com.cn/s/blog_71e7e6fb0101lipz.html*/
struct Node *head,*p,*q;
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
p = head;
while( (p1!=NULL)&&(p2!=NULL) )
{
if (p1->data == p2->data)
{
q = (struct Node *)malloc(sizeof(struct Node));
q->data = p1->data;
q->next = NULL;
p->next = q;//我可以认为你这里用了头结点,也就是说第一个数据域无效     **标记1**
p = q;
p1 = p1->next;
p2 = p2->next;
}
else if (p1->data < p2->data)
{
p1 = p1->next;
}
else
{
p2 = p2->next;
}
}
return head;
}
int main()
{
  struct Node *head=NULL,*headt=NULL,*t;
  //initpointer(head);//这里的函数相当于head=NULL;
 // initpointer(headt);//上面已经写了headt=NULL那么这里可以不用调用这个函数
  head=creatlist(head);
  headt=creatlist(headt);
  t=Intersect(head,headt);
  printlist(t);
}

立即下载此资源

提示:下载后请用压缩软件解压,推荐使用 WinRAR 或 7-Zip

资源说明

📥 下载说明

  • 下载需消耗 2积分
  • 24小时内重复下载不扣分
  • 支持断点续传
  • 资源永久有效

📦 使用说明

  • 下载后用解压软件解压
  • 推荐 WinRAR 或 7-Zip
  • 如有密码请查看说明
  • 解压后即可使用

🎁 积分获取

  • 上传资源获得积分
  • 每日签到免费领取
  • 邀请好友注册奖励
  • 查看详情 →

相关标签

点击标签查看更多相关资源:

相关资源推荐