兩個鏈表的交集 - 免費下載

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
  • 如有密碼請查看說明
  • 解壓后即可使用

?? 積分獲取

  • 上傳資源獲得積分
  • 每日簽到免費領取
  • 邀請好友注冊獎勵
  • 查看詳情 →

相關標簽

點擊標簽查看更多相關資源:

相關資源推薦