刚刚学习了数据结构~然后老师给了一个线性表的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;
}