写一个程序求链表的异或!!要源代码

bing_shan 2005-05-21 02:30:18
写一个程序求链表的异或!!要源代码
...全文
45 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
llf_hust 2005-05-21
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}*Node;

void Create(struct Node **head)
{
int a;
struct Node *tail, *p;
printf("please input data end of 0.\n");
scanf("%d",&a);
p = (struct Node *) malloc (sizeof(struct Node));
p->data = a;
p->next = *head;
*head = p;
tail = p;
scanf("%d", &a);
while(a)
{

p = (struct Node *) malloc (sizeof(struct Node));
p->data = a;
p->next = NULL;
tail->next = p;
tail = p;
scanf("%d",&a);
}
}

int Length(Node p)
{
int len = 0;
while ( p != NULL)
{
len++;
p = p->next;
}
return len;
}

void Display(Node p)
{
while(p != NULL )
{
printf(" %d",p->data);
p = p->next;
}
printf("\n");
}

struct Node *Fun(struct Node *a, struct Node *b)
{
int len1 = 0, len2 = 0;
struct Node *p,*p1= NULL,*p2;
len1 = Length(a);
len2 = Length(b);
if ( len1 != len2)
{
printf("Error\n");
return NULL;
}
else
{
p = (struct Node *) malloc(sizeof(struct Node));
p->data = a->data ^ b->data;
p->next = p1;
p1 = p;
p2 = p;
a = a->next;
b = b->next;
while ( a != NULL && b != NULL)
{
p = (struct Node *) malloc (sizeof(struct Node));
p->data = a->data ^ b->data;
p->next = NULL;
p2->next = p;
p2 = p;
a = a->next;
b = b->next;
}
}
return p1;
}

void Del(struct Node **p)
{
struct Node *a;
while(*p)
{
a = (*p)->next;
free(*p);
*p = a;
}
}

main()
{
int a;
struct Node *p = NULL ,*p1 = NULL, *p2;
clrscr();
Create(&p);
Create(&p1);
p2 = Fun(p,p1);
Display(p2);
Del(&p);
Del(&p1);
Del(&p2);
printf("\n");
system("pause");
}




69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧