Huoche27
Huoche27
采纳率0%
2016-04-27 11:44

C语言成语求代码标注~刚刚学习了线性表~~新手小白~~求带~~~谢谢各位大神

  • c
  • 线性表
  • 数据结构

刚刚学习了数据结构~然后老师给了一个线性表的C程序,,然后好多地方看不懂·~~~希望哪位大神帮我把每条代码的意思标注一下~~~谢谢~~~

#include
#include

typedef struct Node
{
struct Node *next;
int value;
}Node, *List;

List merge(List a, List b)
{
Node *p, *pa, *pb, *prea;
if(a == NULL || b == NULL) 0;

p = a;
prea = a;
pa = a->next;
pb = b->next;
while(pa && pb)
{
    if(pa == pb)
    {
        pb = NULL;
        break;
    }
    else if(pa->value > pb->value) 

    {
        prea->next = pb;
        pb = pb->next;
        prea = prea->next;
        prea->next = pa;
    }
    else
    {
        if(pa->next == pb->next)
        {
            pb->next = NULL;
        }
        prea = pa;
        pa = pa->next;
    }
}

prea->next = pa ? pa : pb;
return p;

}

void print(List a)
{

Node *p = a->next;
if(!a) return;
while(p)
{
    printf("%d ", p->value);
    p = p->next;
}
putchar('\n');

}

List getList(int a[], int len)
{
int i;
Node head = (Node)malloc(sizeof(Node));
Node p = head;
head->next = NULL;
for(i = 0; i < len; i++)
{
Node *n = (Node
)malloc(sizeof(Node));
n->value = a[i];
p->next = n;
p = n;
}
p->next = NULL;
return head;
}

int main(int argc, char *argv[])
{
{

int a[] = {1, 2, 3, 4};
int b[] = {0, 1, 5};
List la = getList(a, 4);
List lb = getList(b, 3);
print(la);
print(lb);
print(merge(la, lb));

}
printf("---------------\n");
{

int a[] = {0, 0, 0, 0};
int b[] = {0};
List la = getList(a, 4);
List lb = getList(b, 1);
lb->next->next = la->next->next;
print(la);
print(lb);
print(merge(la, lb));

}
{

int a[] = {0, 1, 3, 40};
int b[] = {0};
List la = getList(a, 4);
List lb = getList(b, 1);
lb->next->next = la->next->next->next;
print(la);
print(lb);
print(merge(la, lb));

}

return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq423399099 小灸舞 5年前

    这种链表的注释不一定能给楼主讲清楚,如果楼主指针学的不错倒是可以
    第一次学链表的话肯定要结合图来看
    举两句的例子:
    prea->next = pb; //prea的next指针指向了pb
    pb = pb->next; //pb指向了它的next指针指向的节点,即pb指向了下一个节点
    如果楼主听不懂的话,建议找个人面对面问问

    点赞 2 评论 复制链接分享

为你推荐