*默认有header*/
/*FOR singly Linked Lists*/
/*swap two adjcent elements*/
/*错误全是某某变量 在malloc时可能会变成NULL*/
/*或者在写函数时某某变量可能没有initialize*/
#include <stdio.h>
#include <stdlib.h>
typedef struct lnode* list;
typedef struct lnode* position;
struct lnode {
int data;
struct lnode* next;
};
void finish(position p)
{
p->next = NULL;
}
position attatch(position l, int x)
{
position new = malloc(sizeof(struct lnode));
if (new == NULL)
printf("no space!");
new->data = x;
l->next = new;
return new;
}
void the_intersect(list l1, list l2)
{
list l = malloc(sizeof(struct lnode));
position p = l;
/*没有必要求长度*/
position p1 = l1->next;
position p2 = l2->next;
while (p1 != NULL)
{
while (p2 != NULL)
{
if (p2->data == p1->data)
printf("%d ", p1->data);/*如果找到了其实可以直接跳过这一次循环*/
p2 = p2->next;/*p2在L的最末尾,一直不动了回不来了,*/
}
p2 = l2->next;/*所以在这里要回来,不然本例就一直是1*/
p1 = p1->next;
}
}
void prin(list l)
{
position p = l->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
/*solution from the author*/
/* This code can be made more abstract by using operations such as */
/* Retrieve and IsPastEnd to replace L1Pos->Element and L1Pos != NULL. */
/* We have avoided this because these operations were not rigorously defined. */
/*原题是排好序的两个链表*/
list make_empty(list l)
{
while (l->next != NULL)
{
list temp = l->next;
l->next = l->next->next;
free(temp);
}
return l->next;
}
void insert(int x, list l, position p)
{
position temp = malloc(sizeof(struct lnode));
if (temp == NULL)
Error("no space!");
temp->data = x;
temp->next = p->next;
p->next = temp;
}
void move(position p)
{
p = p->next;
}
list intersect(list l1, list l2)
{
list result;
position l1_pos, l2_pos, result_pos;
l1_pos = l1->next;
l2_pos = l2->next;
result = make_empty(NULL);/*这是返回空表的插入位置*/
result_pos = result->next;
while (l1_pos != NULL && l2_pos != NULL)/*只要不同时等于0*/
{
if (l1_pos->data < l2_pos->data)
l1_pos = l1_pos->next;
else if (l1_pos->data > l2_pos->data)
l2_pos = l2_pos->next;
else
{
insert(l1_pos->data, result, result_pos);/*把数据插在result的result_pos*/
l1_pos = l1_pos->next;
l2_pos = l2_pos->next;
result_pos = result_pos->next;
}
}
return result;
}
/*计算union*/
/*我这个是错的,因为l2比l1多了几个尾巴但是结果又在l1里卖弄,就有可能造成重复*/
list the_union(list l1, list l2)
{
position pos1, pos2;
pos1 = l1->next;
pos2 = l2->next;
list result = malloc(sizeof(struct lnode));
list result_pos = make_empty(result);
while (pos1 != NULL || pos2 != NULL)
{
if (pos1 == NULL)/*while已经保证了不会同时等于0*/
{
insert(pos2->data, result, result_pos);
pos2 = pos2->next;
result_pos = result_pos->next;
}
else if (pos2 == NULL)
{
insert(pos1->data, result, result_pos);
pos1 = pos1->next;
result_pos = result_pos->next;
}
else if (pos1->data == pos2->data)
{
insert(pos1->data, result, result_pos);
pos1 = pos1->next;
result_pos = result_pos->next;
}
else
{
insert(pos1->data, result, result_pos);
insert(pos2->data, result, result_pos);
result_pos = result_pos->next;
}
}
return result->next;
}
/*solution from the author*/
/*考虑1,2,3,6,10,100*/
/*和 1,2,3,4,5,6,7,8,9,10,11*/
/*还有1,2,3,11,18,20,29,30*/
/*和 1,3,4,5,7,8,9*/
/* 1,2,3,4,5,6*/
/* 10,20,30,40,50*/
list get_union(list l1, list l2)
{
list result;
int insert_data;
position pos1, pos2, result_pos;
pos1 = l1->next;
pos2 = l2->next;
result = make_empty(NULL);
result_pos = result->next;
while (pos1 != NULL && pos2 != NULL)
{
/*小的先记下来,而且后移*/
if (pos1->data < l2->data)
{
insert_data = pos1->data;
move(pos1);/* p = p->next */
}
else if (pos1->data > pos2->data)
{
insert_data = pos2->data;
move(pos2);
}
else
{
insert_data = pos1->data;
move(pos1);
move(pos2);
}
/*照这个方法查出来,即使末尾多几个数字,也比较好解决*/
insert(insert_data, result, result_pos);
move(result_pos);
}
if(pos1==NULL)
while(pos2!=NULL)
{
insert(pos2->data, result, result_pos);
move(result_pos);
move(pos2);
}
else if(pos2==NULL)
while (pos1 != NULL)
{
insert(pos1->data, result, result_pos);
move(result_pos);
move(pos1);
}
return result;
}
int main(void)
{
list l = malloc(sizeof(struct lnode));
position p;
p = attatch(l, 1);
p = attatch(p, 2);
p = attatch(p, 4);
p = attatch(p, 9);
p = attatch(p, 10);
list L = malloc(sizeof(struct lnode));
position x1, x2, x3, x4, x5, x6, x7, x8, x9;
x1 = attatch(L, 1);
x2 = attatch(x1, 3);
x3 = attatch(x2, 4);
x4 = attatch(x3, 7);
x5 = attatch(x4, 8);
x6 = attatch(x5, 9);
x7 = attatch(x6, 2);
x8 = attatch(x7, -2);
x9 = attatch(x8, 11);
finish(p);
finish(x9);
prin(l);
prin(L);
//the_intersect(l, L);
/*list read = the_union(l, L);
while (read != NULL)
{
printf("%d ", read->data);
read = read->next;
}*/
}