本文共 8742 字,大约阅读时间需要 29 分钟。
链表头文件List.h
#pragma once#define _CRT_SECURE_NO_WARNINGS#include#include typedef struct node { int number; struct node* pnext;}listnode, * plistnode;void listHeadInsert(plistnode* head, plistnode* tail, int num); //头插法建立单向链表void listTailInsert(plistnode* head, plistnode* tail, int num); //尾插法建立单向链表void listSortInsert(plistnode* head, plistnode* tail, int num); //按顺序插入建立单向链表void DeleteListNode(plistnode* head, plistnode* tail, int num); //根据索引值num删除链表中该节点void listNodePrint(plistnode head); //输出链表中节点的值(函数格式1)void listNodePrint_T(plistnode head); //输出链表中节点的值(函数格式2)void MergeTwoOrderdList(plistnode* head1, plistnode* head2); //合并两个有序链表void DeleteRepetitiveNum(plistnode* head); //删除有序链表中重复的元素void DividedListByPosition(plistnode* head); //根据数值的奇偶性分割一个链表为两个int GetListLength(plistnode head); //返回链表的长度void FindListGivenNode(plistnode head); //输出链表倒数第四个位置的数值plistnode GetIntersectionNode(plistnode* headA, plistnode* headB); //返回两个链表相交的节点指针void ReverseList(plistnode* head); //反转链表(函数格式1)plistnode ReverseList_T(plistnode* head); //反转链表(函数格式2)void BestBigIntAdd(plistnode* head1, plistnode* head2); //链表的大整数相加函数接口
链表头文件函数实现List.c文件
#include "List.h"void listHeadInsert(plistnode* head, plistnode* tail, int num){ plistnode pnew = (plistnode)calloc(1, sizeof(listnode)); pnew->number = num; if (NULL == *head) { *head = pnew; *tail = pnew; } else { pnew->pnext = *head; *head = pnew; } return;}void listTailInsert(plistnode* head, plistnode* tail, int num){ plistnode pnew = (plistnode)calloc(1, sizeof(listnode)); pnew->number = num; if (NULL == *head) { *head = pnew; *tail = pnew; } else { (*tail)->pnext = pnew; *tail = pnew; } return;}void listSortInsert(plistnode* head, plistnode* tail, int num){ plistnode prehead = *head; plistnode curhead = *head; plistnode pnew = (plistnode)calloc(1, sizeof(listnode)); pnew->number = num; if (NULL == curhead) { *head = pnew; *tail = pnew; } else if (curhead->number >= num) { pnew->pnext = *head; *head = pnew; } else { while (curhead) { if (curhead->number >= num) { prehead->pnext = pnew; pnew->pnext = curhead; break; } prehead = curhead; curhead = curhead->pnext; } if (NULL == curhead) { prehead->pnext = pnew; prehead = pnew; } } return;}void DeleteListNode(plistnode* head, plistnode* tail, int num) { if (NULL == *head) { printf("the list is empty\n"); return; } plistnode preptr = *head; plistnode curptr = preptr->pnext; plistnode ptr; if (preptr->number == num) { free(preptr); *head = curptr; return; } while (curptr) { if (curptr->number == num) { ptr = curptr; preptr->pnext = curptr->pnext; free(ptr); return; } preptr = curptr; curptr = curptr->pnext; } if (NULL == curptr) { printf("not find this element in the List\n"); return; }}void listNodePrint(plistnode head){ while (head) { printf("%d -> ", head->number); head = head->pnext; } printf("NULL\n"); return;}void listNodePrint_T(plistnode head){ while (head) { printf("%d", head->number); head = head->pnext; } return;}void MergeTwoOrderdList(plistnode* head1, plistnode* head2) { plistnode ptr = (plistnode)calloc(1, sizeof(listnode)); plistnode head1ptr; plistnode head2ptr; plistnode initi_ptr = ptr; head1ptr = *head1; head2ptr = *head2; while (head1ptr && head2ptr) { if (head1ptr->number <= head2ptr->number) { ptr->pnext = head1ptr; ptr = head1ptr; head1ptr = head1ptr->pnext; } else { ptr->pnext = head2ptr; ptr = head2ptr; head2ptr = head2ptr->pnext; } } if (head1ptr) { ptr->pnext = head1ptr; } if (head2ptr) { ptr->pnext = head2ptr; } printf("The result of two List merge:\n"); listNodePrint(initi_ptr->pnext);}void DeleteRepetitiveNum(plistnode* head){ plistnode preptr; plistnode curptr; plistnode ptr; preptr = (*head); curptr = (*head)->pnext; while (curptr) { if (preptr->number != curptr->number) { preptr = preptr->pnext; curptr = curptr->pnext; } else { ptr = curptr->pnext; preptr->pnext = curptr->pnext; free(curptr); curptr = ptr; } } return;}void DividedListByPosition(plistnode* head){ plistnode odd = (plistnode)calloc(1, sizeof(listnode)); plistnode uneven = (plistnode)calloc(1, sizeof(listnode)); plistnode ptr = *head; plistnode oddptr = odd; plistnode unevenptr = uneven; while (ptr) { if (ptr->number % 2 == 0) { unevenptr->pnext = ptr; unevenptr = ptr; } else{ oddptr->pnext = ptr; oddptr = ptr; } ptr = ptr->pnext; } unevenptr->pnext = ptr; oddptr->pnext = ptr; listNodePrint(odd->pnext); listNodePrint(uneven->pnext); return;}int GetListLength(plistnode head){ int len = 0; while (head) { //遍历链表,计算链表长度 ++len; head = head->pnext; } return len;}void FindListGivenNode(plistnode head){ int len = GetListLength(head); if (len < 4) { printf("It is wrong position.\n"); return; } int delta = len - 4; while (head && delta) { head = head->pnext; --delta; } printf("倒数第四个节点的值为:%d \n", head->number);}plistnode GetIntersectionNode(plistnode* headA, plistnode* headB) { int List_A_len = GetListLength(*headA); int List_B_len = GetListLength(*headB); if (List_A_len > List_B_len) { //如果链表A长,移动headA到对应位置 int delta = List_A_len - List_B_len; while ((*headA) && delta--) { (*headA) = (*headA)->pnext; } } else { //如果链表B长,移动headB到对应位置 int delta = List_B_len - List_A_len; while ((*headB) && delta--) { (*headB) = (*headB)->pnext; } } while (*headA && *headB) { if (*headA == *headB) { return *headA; } *headA = (*headA)->pnext; *headB = (*headB)->pnext; } return NULL;}void ReverseList(plistnode* head){ plistnode new_head = NULL; //指向新链表头节点的指针 while (*head) { plistnode next = (*head)->pnext; //备份head->next (*head)->pnext = new_head; //更新head->next new_head = *head; //移动new_head *head = next; //遍历链表 } listNodePrint(new_head); //返回新链表节点}plistnode ReverseList_T(plistnode* head) { //plistnode new_head = (plistnode)calloc(1, sizeof(listnode)); plistnode new_head = NULL; while (*head) { plistnode next = (*head)->pnext; //备份head->next (*head)->pnext = new_head; //更新head->next new_head = *head; //移动new_head *head = next; //遍历链表 } return new_head; //返回新链表节点}void BestBigIntAdd(plistnode* head1, plistnode* head2){ int len1 = GetListLength(*head1); int len2 = GetListLength(*head2); plistnode ptrhead1 = (*head1); plistnode ptrhead2 = (*head2); int k = 0, t = 0; int count; if (len1 >= len2) { while (t <= len1 - 1 && ptrhead2) { k = ptrhead1->number + ptrhead2->number; if (k < 10) { ptrhead1->number = ptrhead1->number + ptrhead2->number; } else { ptrhead1->number = k % 10; ptrhead1->pnext->number = ptrhead1->pnext->number + 1; } ptrhead1 = ptrhead1->pnext; ptrhead2 = ptrhead2->pnext; t++; } } if (len2 > len1) { while (t <= len2 - 1 && ptrhead1) { k = ptrhead1->number + ptrhead2->number; if (k < 10) { ptrhead2->number = ptrhead2->number + ptrhead2->number; } else { ptrhead2->number = k % 10; ptrhead2->pnext->number = ptrhead2->pnext->number + 1; } ptrhead1 = ptrhead1->pnext; ptrhead2 = ptrhead2->pnext; t++; } } printf("超大数字A和B的和为:\n"); plistnode ptr = ReverseList_T(head1); listNodePrint_T(ptr); printf("\n");}
主函数测试程序main.c文件
#include "List.h"int main(){ int num; int num_t; plistnode phead = NULL; plistnode ptail = NULL; plistnode phead_t = NULL; plistnode ptail_t = NULL; while (scanf("%d", &num) != EOF) { //listHeadInsert(&phead, &ptail, num); listTailInsert(&phead,&ptail,num); //listSortInsert(&phead, &ptail, num); } listNodePrint(phead); while (scanf("%d", &num_t) != EOF) { //listHeadInsert(&phead_t, &ptail_t, num_t); listTailInsert(&phead_t, &ptail_t, num_t); } listNodePrint(phead_t); //MergeTwoOrderdList(&phead, &phead_t); //FindlistMiddleNode(phead); //JudgeListExistCircle(phead); //printf("input need to deleted element:\n"); //scanf("%d", &num); //DeleteListNode(&phead, &ptail, num); //listNodePrint(phead); //DeleteRepetitiveNum(&phead); //printf("The result of delete repetitve element in ordered list:\n"); //listNodePrint(phead); //DividedListByPosition(&phead); //ReverseList(&phead); //printf("%d ", GetListLength(phead)); //FindListGivenNode(phead); plistnode ptr1 = ReverseList_T(&phead); plistnode ptr2 = ReverseList_T(&phead_t); BestBigIntAdd(&ptr1, &ptr2); return 0;}
转载地址:http://bxxmb.baihongyu.com/