亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

蟲蟲首頁| 資源下載| 資源專輯| 精品軟件
登錄| 注冊

您現在的位置是:蟲蟲下載站 > 資源下載 > Linux/uClinux/Unix編程 > 兩個鏈表的交集

兩個鏈表的交集

  • 資源大?。?/b>25 K
  • 上傳時間: 2015-04-27
  • 上傳用戶:舞玥
  • 資源積分:2 下載積分
  • 標      簽: c語言編程

資 源 簡 介

兩個鏈表的交集

#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);
}

相 關 資 源

主站蜘蛛池模板: 通州区| 屏山县| 辽源市| 新巴尔虎左旗| 阆中市| 山东| 新沂市| 安乡县| 平阳县| 乌鲁木齐市| 永清县| 无棣县| 定边县| 佛山市| 壤塘县| 莒南县| 兴义市| 南华县| 宁武县| 泰安市| 崇文区| 宁安市| 潮州市| 乐业县| 玛纳斯县| 安福县| 临夏县| 黔西县| 潞西市| 胶南市| 海林市| 晋宁县| 广水市| 西城区| 西华县| 南部县| 措勤县| 台东市| 镇宁| 邵武市| 麟游县|