谁会用C语言编写线性表啊?
答案:5 悬赏:40 手机版
解决时间 2021-04-05 16:31
- 提问者网友:書生途
- 2021-04-05 10:11
谁会用C语言编写线性表啊?
最佳答案
- 五星知识达人网友:低音帝王
- 2021-04-05 11:13
算法思想:两个链表中数据的交集也就是把他们相同的数据提取出来;
//创建递增排序的链表
struct Link *CreateLink()
{
struct Link *head=NULL,*p=NULL,*q=NULL;
head = (struct Link *)malloc(sizeof(struct Link));
head->next = NULL;
p = head;
int n=1;
printf("Please input a number(0 exit):\n");
scanf("%d",&n);
while( n != 0)
{
q = (struct Link *)malloc(sizeof(struct Link));
q->data = n;
p = Insert(p,q);
scanf("%d", &n);
}
return head;
}
struct Link *Insert(struct Link *head, struct Link *p)
{
struct Link *h=head,*q=NULL, *pTemp=p;
q = h;
while(q->next != NULL)
{
if ( (q->next->data) >= (pTemp->data) )
{
break;
}
q = q->next;
}
if (q->next != NULL)
{
pTemp->next = q->next;
q->next = pTemp;
}
else
{
q->next = pTemp;
pTemp->next = NULL;
}
return h;
}
//核心功能函数,找出交集
struct Link *Intersect(struct Link *head1, struct Link *head2)
{
struct Link *p1=head1->next,*p2=head2->next;
struct Link *head,*p,*q;
head = (struct Link *)malloc(sizeof(struct Link));
head->next = NULL;
p = head;
while( (p1!=NULL)&&(p2!=NULL) )
{
if (p1->data == p2->data)
{
q = (struct Link *)malloc(sizeof(struct Link));
q->data = p1->data;
q->next = NULL;
p->next = q;
p = q;
p1 = p1->next;
p2 = p2->next;
}
else if (p1->data < p2->data)
{
p1 = p1->next;
}
else
{
p2 = p2->next;
}
}
return head;
}
//输出链表内容
void PrintLink(struct Link *head)
{
struct Link *p = head->next;
printf("\nThe data in link:");
while (p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main(int argc, char* argv[])
{
struct Link *h1=NULL,*h2=NULL,*head=NULL;
h1 = CreateLink();
PrintLink(h1);
h2 = CreateLink();
PrintLink(h2);
head = Intersect(h1,h2);
PrintLink(head);
return 0;
}
//创建递增排序的链表
struct Link *CreateLink()
{
struct Link *head=NULL,*p=NULL,*q=NULL;
head = (struct Link *)malloc(sizeof(struct Link));
head->next = NULL;
p = head;
int n=1;
printf("Please input a number(0 exit):\n");
scanf("%d",&n);
while( n != 0)
{
q = (struct Link *)malloc(sizeof(struct Link));
q->data = n;
p = Insert(p,q);
scanf("%d", &n);
}
return head;
}
struct Link *Insert(struct Link *head, struct Link *p)
{
struct Link *h=head,*q=NULL, *pTemp=p;
q = h;
while(q->next != NULL)
{
if ( (q->next->data) >= (pTemp->data) )
{
break;
}
q = q->next;
}
if (q->next != NULL)
{
pTemp->next = q->next;
q->next = pTemp;
}
else
{
q->next = pTemp;
pTemp->next = NULL;
}
return h;
}
//核心功能函数,找出交集
struct Link *Intersect(struct Link *head1, struct Link *head2)
{
struct Link *p1=head1->next,*p2=head2->next;
struct Link *head,*p,*q;
head = (struct Link *)malloc(sizeof(struct Link));
head->next = NULL;
p = head;
while( (p1!=NULL)&&(p2!=NULL) )
{
if (p1->data == p2->data)
{
q = (struct Link *)malloc(sizeof(struct Link));
q->data = p1->data;
q->next = NULL;
p->next = q;
p = q;
p1 = p1->next;
p2 = p2->next;
}
else if (p1->data < p2->data)
{
p1 = p1->next;
}
else
{
p2 = p2->next;
}
}
return head;
}
//输出链表内容
void PrintLink(struct Link *head)
{
struct Link *p = head->next;
printf("\nThe data in link:");
while (p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int main(int argc, char* argv[])
{
struct Link *h1=NULL,*h2=NULL,*head=NULL;
h1 = CreateLink();
PrintLink(h1);
h2 = CreateLink();
PrintLink(h2);
head = Intersect(h1,h2);
PrintLink(head);
return 0;
}
全部回答
- 1楼网友:天凉才是好个秋
- 2021-04-05 14:50
int i=0, j=0, m=0;
while(i {
if (A[i] < B[j])
i++;
else if (A[i] > B[j])
j++;
else
{
C[m++] = A[i];
i++;
j++;
}
}
while(i
if (A[i] < B[j])
i++;
else if (A[i] > B[j])
j++;
else
{
C[m++] = A[i];
i++;
j++;
}
}
- 2楼网友:山河有幸埋战骨
- 2021-04-05 14:28
这个貌似要用到数据结构的追问对啊,就是那上面的题追答viod Mergelist(List La,List Lb,List &Lc){
InitList(Lc);
i=j=1;k=0;
La_len=ListLength(La);Lb_len=ListLrngth(Lb);
while((i<=La_len)&&(j<=Lb_len)){
GetElem(La,i,ai);GetElem(Lb,j,bj);
if(ai<=bj){ListInsert(Lc,++k,aj);++i;}
else{ListInsert(Lc,++k,bj);++j;}
}
while(i<=La_len){
GetElem(La,i++,ai);ListInsert(Lc,++k,ai);
}
while(j<=Lb_len){
GetElem(Lb,j++,bi);ListInsert(Lc,++k,bj);
}
}追问麻烦再完整一点,我这基础真的驾驭不了,光主函数我不会用,另外操作环境是VC6.0
InitList(Lc);
i=j=1;k=0;
La_len=ListLength(La);Lb_len=ListLrngth(Lb);
while((i<=La_len)&&(j<=Lb_len)){
GetElem(La,i,ai);GetElem(Lb,j,bj);
if(ai<=bj){ListInsert(Lc,++k,aj);++i;}
else{ListInsert(Lc,++k,bj);++j;}
}
while(i<=La_len){
GetElem(La,i++,ai);ListInsert(Lc,++k,ai);
}
while(j<=Lb_len){
GetElem(Lb,j++,bi);ListInsert(Lc,++k,bj);
}
}追问麻烦再完整一点,我这基础真的驾驭不了,光主函数我不会用,另外操作环境是VC6.0
- 3楼网友:痴妹与他
- 2021-04-05 12:52
数据结构书上都有的。追问你能给编一下吗追答数据结构上的是思想,需要自己改成c或c++什么的。
- 4楼网友:迷人又混蛋
- 2021-04-05 12:30
单链表一个节点指向下一个节点,A每个元素和B比较,有相同的插入C表追问没基础啊,能说得简单点吗追答网上找下C单链表的信息吧,会指针的话很快能理解单链表,不会的话就用一般的线性表,你这里递增我不明白什么意思....无视这个递增的话,用数组也可以做...
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯