使用前须知:
此代码直接可以运行,但老师要求写纸上也就是意味着你不用全抄只需参考核心代码就行,变量名函数名可以自己换掉
第一题
List* mergeIncreasingList() {
Pair* p = creatIncreaseList();
List* list1 = p->first;
List* list2 = p->second;
List* list = (List*)malloc(sizeof(List));
Node* res = (Node*)malloc(sizeof(Node));
list->head = res;
list->head->data = -1;
Node* current1 = list1->head;
Node* current2 = list2->head;
while (current1 != NULL && current2 != NULL) {
if (current1->data > current2->data) { // 链表1中的元素更大
res->next = current2;
res = current2;
current2 = current2->next;
} else if (current1->data < current2->data) { //链表2中的元素更大
res->next = current1;
res = current1;
current1 = current1->next;
} else {
res->next = current1;
res = current1;
current1 = current1->next;
current2 = current2->next;
}
}
res->next = current1 ? current1 : current2;
return list;
}
第二题
List* mergeDecreasingList() {
Pair* p = creatIncreaseList();
List* list1 = p->first;
List* list2 = p->second;
List* list = (List*)malloc(sizeof(List));
Node* workNode = (Node*)malloc(sizeof(Node));
list->head = workNode;
Node* current1 = list1->head;
Node* current2 = list2->head;
while (current1 != NULL && current2 != NULL) {
if (current1->data >= current2->data) { //链表1中的元素更大
workNode->next = current2;
workNode = workNode->next;
current2 = current2->next;
} else {
workNode->next = current1;
workNode = workNode->next;
current1 = current1->next;
}
}
workNode->next = current1 ? current1 : current2;
return list;
}
第三题
List* getIntersection() {
Pair* p = creatIncreaseList();
List* list1 = p->first;
List* list2 = p->second;
List* list = (List*)malloc(sizeof(List));
Node* res = (Node*)malloc(sizeof(Node));
res->data = -1;
res->next = NULL;
list->head = res;
Node* current1 = list1->head;
Node* current2 = list2->head;
while (current1 != NULL && current2 != NULL) {
if (current1->data > current2->data) {
current2 = current2->next;
} else if (current1->data == current2->data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = current1->data;
newNode->next = NULL;
current1 = current1->next;
current2 = current2->next;
res->next = newNode;
res = newNode;
} else {
current1 = current1->next;
}
}
return list;
}
第四题
List* diffSet() { // 差集就是两个集合中独有的元素
Pair* p = creatIncreaseList();
List* list1 = p->first;
List* list2 = p->second;
Node* current1 = list1->head;
Node* current2 = list2->head;
List* list = initList();
Node* workNode = (Node*)malloc(sizeof(Node));
list->head = workNode;
workNode->data = -1;
workNode->next = NULL;
while (current1 != NULL && current2 != NULL) {
if (current1->data < current2->data) { // 链表1中的数据较小
workNode->next = current1;
workNode = workNode->next;
current1 = current1->next;
} else if (current1->data > current2->data) { // 链表2中的元素较小
workNode->next = current2;
workNode = workNode->next;
current2 = current2->next;
} else { //链表1中的数据等于链表2
current1 = current1->next;
current2 = current2->next;
}
}
return list;
}
第五题
Pair* separateList(List* A) {
Node* curNode = A->head;
List* B = initList();
List* C = initList();
append(B, -1);
append(C, -1);
Node* curB = B->head;
Node* curC = C->head;
while (curNode) {
if (curNode->data > 0) {
curB->next = curNode;
curB = curB->next;
} else if (curNode->data < 0) {
curC->next = curNode;
curC = curC->next;
}
curNode = curNode->next;
}
curC->next = NULL;
curB->next = NULL;
Pair* p = (Pair*)malloc(sizeof(Pair));
p->first = B;
p->second = C;
return p;
}
第六题
Node* getMaxNode(List* list) {
Node* current = list->head;
Node* maxNode = (Node*)malloc(sizeof(Node));
maxNode->data = list->head->data;
while (current != NULL) {
if (current->data > maxNode->data) {
maxNode = current;
}
current = current->next;
}
return maxNode;
}
第七题
void reverseList(List* list) {
Node* current = list->head;
Node* prev = NULL;
Node* temp;
while (current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
list->head = prev;
}
第八题
void getSubset(List* list, ElemType mink, ElemType maxk) {
Node* current = list->head;
Node* prev = NULL;
Node* firstMinNodePrev;
Node* firstMaxNodeNext;
while (current != NULL) { // 查找要删除的最小节点
if (current->data >= mink) {
firstMinNodePrev = prev;
break;
}
prev = current;
current = current->next;
}
while (current != NULL) {
if (current->data >= maxk) {
firstMaxNodeNext = current;
break;
}
current = current->next;
}
if (current == NULL) {
firstMinNodePrev->next = NULL;
} else {
firstMinNodePrev->next = firstMaxNodeNext;
}
backwardShowList(list);
}
第九题
void change(Node* node) {
Node* prev = node->prev;
Node* prevP = prev->prev; //当前节点的上一个节点的上一个节点
Node* next = node->next;
Node* nextN = next->next; //当前节点的后一个节点的后一个节点
prevP->next = next;
next->prev = prevP;
next->next = node;
node->prev = next;
node->next = prev;
prev->prev = node;
prev->next = nextN;
nextN->prev = prev;
}
第十题
void Delete ( ElemType A[ ] , int n )
{
i=1 ; j=n ;∥设置数组低、高端指针(下标) 。
while ( i<j ){
while ( i<j && A[i]!=item )
i++ ;∥若值不为 item ,左移指针。
if ( i<j )
while ( i<j && A[j]==item )
j-- ;∥若右端元素为 item ,指针左移
if ( i<j ) A[i++]=A[j--] ;
}
求赞~
版权声明:本文为forljnlearning原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。