注:把我大二的数据结构实验报告当中书写的代码做一个记录,此系列仅为代码分享,无解析
1.实现顺序表的各种基本运算:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{int* data;int length;int capacity;
}SqList;
//创建顺序表
void createList(int capacity, SqList **list) {*list = (SqList*)malloc(sizeof(SqList));if (*list == NULL) {printf("Failed to allocate memory for the sequence list.\n");return ;}(*list)->data = (int*)malloc(capacity * sizeof(int));if ((*list)->data == NULL) {printf("Failed to allocate memory for the data array.\n");free(*list);*list = NULL;return ;}(*list)->length = 0;(*list)->capacity = capacity;
}
//初始化顺序表
void initSqList(SqList* list)
{if(list != NULL)list->length = 0;
}
//销毁线性表
void destroyList(SqList* list)
{if(list != NULL){free(list->data);free(list);}
}
//判断线性表是否为空
//这里的话使用bool类型的函数也可以 如:bool ListEmpty(SqList* list)
int ListEmpty(SqList* list)
{return list->length == 0;
}
//求线性表长度
int Listlength(SqList* list)
{return list->length;
}
//输出线性表
void dispList(SqList* list)
{int i = 0;for(i = 0;i < list->length;i++)printf("%d ",list->data[i]);printf("\n");
}
//求出线性表当中的第n个元素
int getelem(SqList* list,int n)
{if(n < 1 || n > list->length){printf("Invalid index\n");return -1; }return list->data[n - 1];
}
//查找第一个值为n的元素位置
int locateElem(SqList* list,int n)
{int i = 0;if( i < list->length && list->data[i] != n)i++;if( i > list->length){printf("Invalid element");return 0;}elsereturn i+1;
}
//插入第i个元素
int listinsert(SqList* list,int i,int n)
{if( i < 1 || i > list->length + 1 || list->length >= list->capacity){ printf("Invalid position\n");return -1;}/*if(list->length == list->capacity){printf("SqList is full\n");return -1;}*/int j = 0;for (j = list->length; j >= i;j--){list->data[j] = list->data[j - 1];}list->data[i-1] = n;list->length++;
}
//删除第i个元素
int deletelist(SqList *list,int i)
{if(i < 1 || i > list->length || list->length >= list->capacity){printf("Invalid position");return -1;}int j;for (j = i - 1;j < list->length - 1; j++){list->data[j] = list->data[j+1];}list->data[list->length - 1] = 0;list->length--;
}
int main()
{SqList* list;createList(10,&list);if (list != NULL){initSqList(list);if(ListEmpty(list)){printf ("The sequence list is empty.\n");}else{printf ("The sequence list is not empty.\n");}int length = Listlength(list);printf("The length of the sequence list is :%d\n",length);list->data[0] = 7;list->data[1] = 4;list->data[2] = 3;list->length = 3;dispList(list);int elem = getelem(list,2);printf("The value of the n element is %d\n",elem);int locate = locateElem(list, 4);printf("The position of the first element with a value of n is %d\n",locate);listinsert(list,1,8);printf("The sequence list after adding the element is:\n");dispList(list);deletelist(list,2);printf("The sequence list after deleteing the element is:\n");dispList(list);destroyList(list);}return 0;
}
2.实现单链表的各种基本运算:
#include<stdio.h>
#include<stdlib.h>
//创建节点
typedef char ElemType;
typedef struct Node{ElemType data;struct Node* next;
}Node;
//头插法创建一个单链表
Node* createlistf(int data[],int n)
{Node* head = NULL;int i = 0;for(i = 0; i < n; i++){Node* newNode = (Node*)malloc(sizeof(Node));if (newNode == NULL){printf("位置不合法");return NULL;}newNode->data = data[i];newNode->next = head;head = newNode;}return head;
}
// 尾插法建立单链表
Node* createlistR(int data[],int n)
{Node *head = NULL;;Node *tail = NULL;;int i = 0;for (i = 0; i < n; i++){Node* newNode = (Node*)malloc(sizeof(Node));if (newNode == NULL){printf("位置不合法");return NULL;}newNode->data = data[i];newNode->next = NULL;if (head == NULL){head = newNode;tail = newNode;}else{tail->next = newNode;tail = newNode;}} return head;
}
//初始化线性表
void initlist(Node **head)
{*head = (Node*)malloc(sizeof(Node));(*head)->next = NULL;
}
//销毁线性表
void destroylist(Node **head)
{Node *current = *head;Node *next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
//判断线性表是否为空
void emptylist(Node *head)
{if (head == NULL)printf("链表是空的\n");elseprintf("链表为非空\n");
}
//求线性表的长度
int lengthlist(Node *head)
{int i = 0;Node *p =head;while (p != NULL){i++;p = p->next;}return i;
}
//打印线性表
void printflist(Node *head)
{Node *p = head;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");
}
//求线性表中第i个元素
int getelem(Node *head,int i)
{Node *p = head;if (i < 1 || p == NULL){printf("无效位置\n");return -1; }int j = 0;for( j = 1; j < i; j++){if (p->next == NULL){printf("索引越界\n");return -1;}p = p->next;}return p->data;
}
//求值为i的元素序号
int locatelist(Node *head,int i)
{int n = 1;Node *p = head;while ( p != NULL && p->data != i){p = p->next;n++;}if (p == NULL){printf ("%d不存在\n",i);return 0;}else{printf ("值为%d的位置为%d\n",i,n);return 0;}
}
//插入第i个元素
int insertlist(Node **head, int i, int n)
{Node *p = *head;Node *s = (Node*)malloc(sizeof(Node));s->data = n;if (p == NULL && i != 1){printf("线性表是空的\n");return -1;}if (i == 1){s->next = p;*head = s;return 0;}int j = 1;while (p != NULL && j < i - 1){p = p->next;j++;}if (p == NULL){printf("索引越界\n");return -1;}else{s->next = p->next;p->next = s;return 0;}
}
/*int insertlist(Node **head, int i, int n)
{if (*head == NULL){// 创建新的头节点*head = (Node*)malloc(sizeof(Node));(*head)->data = n;(*head)->next = NULL;return 0;}Node *p = *head;Node *prev = NULL;int j = 0;for (j = 0; j < i - 1; j++){if (p == NULL){printf("Index out of bounds\n");return -1;}prev = p;p = p->next;}if (p == NULL){printf("Index out of bounds\n");return -1;}Node *s = (Node*)malloc(sizeof(Node));s->data = n;s->next = p;if (prev == NULL){// 在头部插入元素*head = s;}else{prev->next = s;}return 0;
}
*/
//删除第i个元素
int deletelist(Node **head, int i)
{Node *p = *head;Node *q = NULL;// 删除头节点if (i == 1) {*head = p->next;free(p);return 0;}int j = 1;// 找到第i-1个节点while (p != NULL && j < i ){p = p->next;j++;}// 如果第i-1个节点不存在或者第i个节点不存在,返回错误if (p == NULL || p->next == NULL){printf("索引越界\n");return -1;}else{q = p->next;p->next = q->next;free(q);return 0;}
}
int main()
{int data1[] = {1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1) / sizeof(data1[0]);int data2[] = {2 , 4 , 6 , 8 , 10};int n2 = sizeof(data2) / sizeof(data2[0]);// 使用头插法创建链表Node* head1 = createlistf(data1, n1);// 使用尾插法创建链表Node* head2 = createlistR(data2, n2);//测试用例 //printflist(head1);//printflist(head2); //初始化线性表 Node *head = NULL;initlist(&head);head = head1;//判断线性表是否为空emptylist(head); //求线性表长度int length = lengthlist(head);printf ("表的长度是 %d\n",length); //打印线性表printf("表为:");printflist(head);//线性表中第i个元素的值int elem = getelem(head,3);printf("第3个位置的值是%d\n",elem);//求第一个值为i的元素序号locatelist(head,9); //插入第i个元素insertlist(&head,3,6);printflist(head);//删除第i个元素deletelist(&head,2);printflist(head);//删除线性表 destroylist(&head);return 0;
}
3.实现双向链表的各种基本运算:
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct DNode
{ElemType data;struct DNode* prior;struct DNode* next;
}DLinkNode;
//头插法
DLinkNode* createListF(int data[],int n) {DLinkNode* head = NULL;int i = 0;for (i = 0; i < n; i++){DLinkNode* newNode = (DLinkNode*)malloc(sizeof(DLinkNode));if (newNode == NULL) {printf("内存分配失败\n");return NULL;}newNode->data = data[i];newNode->prior = NULL;newNode->next = head;if (head != NULL) {head->prior = newNode;}head = newNode;}return head;
}
//尾插法
DLinkNode* createListR(int data[], int n)
{DLinkNode* head = NULL;DLinkNode* tail = NULL;int i;for (i = 0; i < n; i++){DLinkNode* newNode = (DLinkNode*)malloc(sizeof(DLinkNode));if (newNode == NULL){printf("内存分配失败\n");return NULL;}newNode->data = data[i];newNode->next = NULL;if (head == NULL){newNode->prior = NULL;head = newNode;tail = newNode;}else{tail->next = newNode;newNode->prior = tail;tail = newNode;}}return head;
}
//打印链表
void printflist(DLinkNode *head)
{DLinkNode *p = head;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");
}
//初始化双向链表
void initlist(DLinkNode** head)
{*head = (DLinkNode*)malloc(sizeof(DLinkNode));(*head)->prior = NULL;(*head)->next = NULL;
}
//销毁双向链表
void destroylist(DLinkNode** head)
{DLinkNode* current = *head;DLinkNode* next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
//判断双向链表是否为空
void emptylist(DLinkNode* head)
{if (head == NULL)printf("链表是空的\n");elseprintf("链表为非空\n");
}
//求线性表长度
int lengthlist(DLinkNode* head)
{int i = 0;DLinkNode* p = head;while (p != NULL){i++;p = p->next;}return i;
}
//求第i个元素的值
int getelem(DLinkNode* head, int i)
{DLinkNode* p = head;if (i < 1 || p == NULL){printf("无效位置");return -1;}int j = 0;for (j = 1; j < i; j++){if (p->next == NULL){printf("索引越界");return -1;}p = p->next;}return p->data;
}
//求值为i的元素序号
int locatelist(DLinkNode *head,int i)
{int n = 1;DLinkNode *p = head;while ( p != NULL && p->data != i){p = p->next;n++;}if (p == NULL){printf ("%d不存在\n",i);return 0;}else{printf ("值为%d的位置为%d\n",i,n);return 0;}
}
//插入第i个元素
int insertlist(DLinkNode** head, int i, int n)
{DLinkNode* p = *head;DLinkNode* s = (DLinkNode*)malloc(sizeof(DLinkNode));s->data = n;if (p == NULL && i != 1){printf("线性表是空的\n");return -1;}if (i == 1){s->next = p;*head = s;return 0;}int j = 1;while (p != NULL && j < i - 1){p = p->next;j++;}if (p == NULL){printf("索引越界\n");return -1;}else{s->prior = p;s->next = p->next;if (p->next != NULL) {p->next->prior = s; // 这里需要先判断 p->next 是否为空}p->next = s;return 0;}
}
//删除第i个元素
int deletelist(DLinkNode** head, int i)
{if (*head == NULL || i < 1) {printf("链表为空或索引无效\n");return -1;}DLinkNode* p = *head;int j = 1;// 移动到第i个节点while (p != NULL && j < i) {p = p->next;j++;}// 如果第i个节点不存在if (p == NULL) {printf("第%d个节点不存在\n", i);return -1;}// 如果是头节点if (p == *head) {*head = p->next; // 更新头节点if (*head != NULL) {(*head)->prior = NULL; // 更新新的头节点的前驱指针}} else {p->prior->next = p->next; // 更新前一个节点的next指针if (p->next != NULL) {p->next->prior = p->prior; // 更新后一个节点的prior指针}}free(p); // 释放当前节点return 0;
}
int main(){int data1[] = {1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1) / sizeof(data1[0]);int data2[] = {2 , 4 , 6 , 8 , 10};int n2 = sizeof(data2) / sizeof(data2[0]);// 使用头插法创建链表DLinkNode* head1 = createListF(data1, n1);// 使用尾插法创建链表DLinkNode* head2 = createListR(data2, n2);//测试用例 //printflist(head1);//printflist(head2);//初始化线性表 DLinkNode *head = NULL;initlist(&head);head = head1;//判断线性表是否为空emptylist(head); //求线性表长度int length = lengthlist(head);printf ("表的长度是 %d\n",length); //打印线性表printf("表为:");printflist(head);//线性表中第i个元素的值int elem = getelem(head,3);printf("第3个位置的值是%d\n",elem);//求第一个值为i的元素序号locatelist(head,9); //插入第i个元素insertlist(&head,3,6);printflist(head);//删除第i个元素deletelist(&head,2);printflist(head);//删除线性表 destroylist(&head);return 0;
}
4.实现单向循环链表的各种基本运算:
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node {ElemType data;struct LinkNode* next;
}LinkNode;
//头插法
LinkNode* createlistF(int data[], int n)
{LinkNode* head = NULL;LinkNode* tail = NULL;int i = 0;for (i = 0; i < n; i++){LinkNode* newNode = (LinkNode*)malloc(sizeof(LinkNode));if (newNode == NULL){printf("内存分配失败\n");return NULL;}newNode->data = data[i];if (head == NULL){head = newNode;tail = newNode;newNode->next = head;}else{newNode->next = head;head = newNode;tail->next = head;}}return head;
}
//尾插法
LinkNode* createlistR(int data[], int n)
{LinkNode* head = NULL;LinkNode* tail = NULL;int i = 0;for (i = 0; i < n; i++){LinkNode* newNode = (LinkNode*)malloc(sizeof(LinkNode));if (newNode == NULL){printf("内存分配失败\n");return NULL;}newNode->data = data[i];if (head == NULL){head = newNode;tail = newNode;newNode->next = head;}else{tail->next = newNode;tail = newNode;tail->next = head;}}return head;
}
//打印循环单链表
void printflist(LinkNode *head)
{LinkNode *p = head;do {printf("%d ", p->data);p = p->next;} while (p != head);printf("\n");
}
//初始化
void initlist(LinkNode** head)
{*head = (LinkNode*)malloc(sizeof(LinkNode));(*head)->next = *head;
}
//销毁
void destroylist(LinkNode** head)
{LinkNode* current = *head;LinkNode* next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
//判断是否为空
void emptylist(LinkNode* head)
{if (head == NULL){printf("线性表为空\n");}else{printf("线性表为非空\n");}
}
//求线性表长度
int lengthlist(LinkNode* head)
{int i = 0;LinkNode* p = head;do{i++;p = p->next;}while(p != head);return i;
}
//得到第i个元素
int getelem(LinkNode* head, int i)
{LinkNode* p = head;if (i < 1 || p == NULL){printf("无效位置\n");return -1;}int j = 0;for (j = 1; j < i; j++){if (p->next == NULL){printf("索引越界\n");return -1;}p = p->next;}return p->data;
}
//求值为i的元素序号
/*int locatelist(LinkNode* head, int i)
{int n = 1;int length = lengthlist(head);LinkNode* p = head;while (p != NULL && p->data != i){p = p->next;n++;if (n > length){break;}} if (p == NULL && n > length){printf("%d不存在\n", i);return 0;}else{printf("%d的位置是%d\n", i, n);return 0;}
}*/
int locatelist(LinkNode* head, int i) {if (head == NULL) {printf("链表为空\n");return -1;}int n = 1;LinkNode* p = head;do {if (p->data == i) {printf("%d的位置是%d\n", i, n);return n;}p = p->next;n++;} while (p != head); // 循环直到回到头节点// 如果没有找到printf("%d不存在\n", i);return -1;
}
//插入第i个元素
/*int insertlist(LinkNode** head, int i, int n)
{LinkNode* current = *head;LinkNode* newNode = (LinkNode*)malloc(sizeof(LinkNode));newNode->data = n;if (current == NULL && i != 1){printf("线性表是空的");return - 1;}if (i == 1){newNode->next = current;*head = newNode;return 0;}int j = 1;do{current = current->next;j++;} while (current != *head && j < i - 1);if (current == head){printf("索引越界");return -1;}else{newNode->next = current->next;current->next = newNode;return 0;}
}*/
int insertlist(LinkNode** head, int i, int n) {if (i < 1) {printf("插入位置不合法\n");return -1;}LinkNode* newNode = (LinkNode*)malloc(sizeof(LinkNode));if (!newNode) {printf("内存分配失败\n");return -1;}newNode->data = n;// 插入到链表为空或者插入到第一个位置时if (*head == NULL || i == 1) {newNode->next = newNode; // 新节点指向自己,形成循环if (*head != NULL) {// 找到链表的最后一个节点LinkNode* last = *head;while (last->next != *head) {last = last->next;}last->next = newNode; // 更新最后一个节点的next指针}*head = newNode; // 更新头指针return 0;}// 插入到链表中间或尾部的情况LinkNode* current = *head;int j = 1;while (current->next != *head && j < i - 1) {current = current->next;j++;}// 如果位置超过链表长度if (j != i - 1) {printf("索引越界\n");free(newNode); // 释放新节点的内存return -1;}newNode->next = current->next;current->next = newNode;return 0;
}
//删除第i个元素
int deletelist(LinkNode** head, int i) {if (*head == NULL || i < 1) {printf("链表为空或者位置不正确\n");return -1;}LinkNode* current = *head;LinkNode* toDelete = NULL;if (i == 1) {toDelete = *head;// 如果链表中只有一个节点if ((*head)->next == *head) {*head = NULL;} else {// 将头节点的前一个节点的next指向头节点的下一个节点while (current->next != *head) {current = current->next;}current->next = (*head)->next;*head = (*head)->next;}} else {int j = 1;// 找到第i个节点的前一个节点while (current->next != *head && j < i - 1) {current = current->next;j++;}// 如果没有找到第i个节点(即i大于链表长度)if (current->next == *head || j != i - 1) {printf("索引越界\n");return -1;}// 删除第i个节点toDelete = current->next;current->next = toDelete->next;}free(toDelete);return 0;
}
int main()
{int data1[] = {1 , 3 , 5, 7 , 9};int n1 = sizeof(data1)/ sizeof(data1[0]);int data2[] = {2, 4 , 6 , 8 , 10};int n2 = sizeof(data2)/ sizeof(data2[0]);LinkNode* head1 = createlistF(data1,n1);LinkNode* head2 = createlistR(data2,n2); //测试用例 //printflist(head1);//printflist(head2);//初始化LinkNode *head =NULL;initlist(&head);head = head1;//判断是否为空emptylist(head); //求长度int length = lengthlist(head);printf("线性表的长度为%d\n",length); //打印线性表printf("表为:");printflist(head);//线性表中第i个元素的值int elem = getelem(head,3);printf("第3个位置的值是%d\n",elem);//求第一个值为i的元素序号locatelist(head,9); //插入第i个元素insertlist(&head,3,6);printflist(head);//删除第i个元素deletelist(&head,2);printflist(head);//删除线性表 destroylist(&head);return 0;
}
5.实现双向循环链表的基本运算:
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct DNode
{ElemType data;struct DNode* prior;struct DNode* next;
}DLinkNode;
//头插法
DLinkNode* createlistF(int data[], int n)
{DLinkNode* head = NULL;DLinkNode* tail = NULL;int i = 0;for (i = 0; i < n; i++){DLinkNode* newNode = (DLinkNode*)malloc(sizeof(DLinkNode));if (newNode == NULL){printf("内存分配失败\n");return NULL;}newNode->data = data[i];if (head == NULL){head = newNode;tail = newNode;newNode->next = head;newNode->prior = head;}else{newNode->next = head;head->prior = newNode;newNode->prior = tail;tail->next = newNode;head = newNode;}}return head;
}
//尾插法实现
DLinkNode* createlistR(int data[], int n)
{DLinkNode* head = NULL;DLinkNode* tail = NULL;int i = 0;for (i = 0; i < n; i++){DLinkNode* newNode = (DLinkNode*)malloc(sizeof(DLinkNode));if (newNode == NULL){printf("内存分配失败\n");return NULL;}newNode->data = data[i];if (head == NULL){head = newNode;tail = newNode;newNode->next = head;newNode->prior = head;}else{newNode->prior = tail;tail->next = newNode;newNode->next = head;head->prior = newNode;tail = newNode;}}return head;
}
//初始化
void initlist(DLinkNode** head)
{*head = (DLinkNode*)malloc(sizeof(DLinkNode));(*head)->prior = (*head)->next = *head;
}
//打印链表
void printflist(DLinkNode* head)
{DLinkNode* p = head;do{printf("%d ", p->data);p = p->next;} while (p != head);printf("\n");
}
//销毁线性表
void destroylist(DLinkNode** head)
{DLinkNode* current = *head;DLinkNode* next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
//判断是否为空
void emptylist(DLinkNode* head)
{if (head == NULL)printf("链表是空的\n");elseprintf("链表为非空\n");
}
//求长度
int lengthlist(DLinkNode* head)
{DLinkNode* p = head;int i = 0;do{i++;p = p->next;} while (p->next != head);return i;
}
//求值为i的元素序号
int locatelist(DLinkNode* head, int i)
{if (head == NULL){printf("链表为空\n");return -1;}int n = 1;DLinkNode* p = head;do{if (p->data == i){printf("%d的位置是%d\n", i, n);return n;}n++;p = p->next;} while (p != head);printf("%d不存在\n", i);return -1;
}
//插入第i个元素
int insertlist(DLinkNode** head, int i, int n)
{if (i < 1){printf("插入位置不合法\n");return -1;}DLinkNode* newNode = (DLinkNode*)malloc(sizeof(DLinkNode));if (!newNode) {printf("内存分配失败\n");return -1;}newNode->data = n;if (*head == NULL || i == 1){newNode->next = newNode;newNode->prior = newNode;if (*head != NULL){DLinkNode* tail = *head;while (tail->next != *head){tail = tail->next;}tail->next = newNode;newNode->prior = tail;}*head = newNode;return 0;}DLinkNode* current = *head;int j = 1;while (current->next != *head && j < i -1){current = current->next;j++;}if (j != i - 1) {printf("索引越界\n");free(newNode); // 释放新节点的内存return -1;}newNode->prior = current;newNode->next = current->next;current->next = newNode;return 0;
}
//删除第i个元素
int deletelist(DLinkNode** head, int i)
{if (*head == NULL || i < 1) {printf("链表为空或者位置不正确\n");return -1;}DLinkNode* current = *head;DLinkNode* todelete = NULL;if (i == 1){todelete = *head;if (current->next == head){*head == NULL;}else{while (current->next != head){current = current->next;}current->next = (*head)->next;*head = (*head)->next;(*head)->prior = current;}}else{int j = 1;while (current->next != *head && j < i - 1){current = current->next;j++;}if (current->next == *head || j != i - 1) {printf("索引越界\n");return -1;}todelete = current->next;current->next = todelete->next;todelete->next->prior = current;}free(todelete);return 0;
}
int main()
{int data1[]={1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1)/ sizeof(data1[0]);int data2[]={2 , 4 , 6 , 8 , 10};int n2 = sizeof(data2)/ sizeof(data2[0]);DLinkNode* head1 = createlistF(data1,n1);DLinkNode* head2 = createlistR(data2,n2);//测试用例//printflist(head1); //printflist(head2);//初始化DLinkNode* head = NULL;initlist(&head);head = head1;//判断是否为空emptylist(head); //求长度int length = lengthlist(head);printf("线性表的长度为%d\n",length); //打印线性表printf("表为:");printflist(head);//求第一个值为i的元素序号locatelist(head,9); //插入第i个元素insertlist(&head,3,6);printflist(head);//删除第i个元素deletelist(&head,2);printflist(head);//删除线性表 destroylist(&head);return 0;
}
6.把单链表按基准划分:
#include<stdio.h>
#include<stdlib.h>
//创建节点
typedef char ElemType;
typedef struct Node {ElemType data;struct Node* next;
}Node;
//将单链表以x为基准划分
void Split(Node** head, ElemType x)
{Node* current = *head;Node* smallerHead = NULL;Node* smallerTail = NULL;Node* largerHead = NULL;Node* largerTail = NULL;while (current != NULL){if (current->data < x){if (smallerHead == NULL) {smallerHead = current;smallerTail = current;}else {smallerTail->next = current;smallerTail = current;}}else{if (largerHead == NULL) {largerHead = current;largerTail = current;}else {largerTail->next = current;largerTail = current;}}current = current->next;}if (smallerHead == NULL) {*head = largerHead;}else {smallerTail->next = largerHead;*head = smallerHead;}if (largerTail != NULL) {largerTail->next = NULL;}
}
//头插法创建一个单链表
Node* createlistf(int data[],int n)
{Node* head = NULL;int i = 0;for(i = 0; i < n; i++){Node* newNode = (Node*)malloc(sizeof(Node));if (newNode == NULL){printf("位置不合法");return NULL;}newNode->data = data[i];newNode->next = head;head = newNode;}return head;
}
void destroylist(Node **head)
{Node *current = *head;Node *next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
void printflist(Node *head)
{Node *p = head;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");
}
int main()
{int data1[] = {1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1) / sizeof(data1[0]);Node* head = createlistf(data1, n1);printflist(head);ElemType x = 7;printf("以7为基准划分:\n");Split(&head,x);printflist(head);return 0;
}
7.简单合并单链表(不涉及排序):
#include<stdio.h>
#include<stdlib.h>
//创建节点
typedef char ElemType;
typedef struct Node {ElemType data;struct Node* next;
}Node;
//合并单链表
void merge(Node** head1, Node** head2, Node** head3)
{Node* p = *head1;Node* q = *head2;Node dummy; // 创建一个哑节点作为合并链表的开始Node* r = &dummy; // r指向哑节点while (q != NULL && p != NULL){r->next = p; // 将p节点连接到rp = p->next; // 移动p到下一个节点r = r->next; // 移动r到下一个节点r->next = q; // 将q节点连接到rq = q->next; // 移动q到下一个节点r = r->next; // 移动r到下一个节点 }if (p != NULL){r->next = p;}else if (q != NULL){r->next = q;}*head3 = dummy.next; // 设置head3为合并后链表的头节点
}
//头插法创建一个单链表
Node* createlistf(int data[],int n)
{Node* head = NULL;int i = 0;for(i = 0; i < n; i++){Node* newNode = (Node*)malloc(sizeof(Node));if (newNode == NULL){printf("位置不合法");return NULL;}newNode->data = data[i];newNode->next = head;head = newNode;}return head;
}
void destroylist(Node **head)
{Node *current = *head;Node *next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
void printflist(Node *head)
{Node *p = head;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");
}
int main()
{int data1[] = {1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1) / sizeof(data1[0]);int data2[] = {2 , 4 , 6 , 8 , 10};int n2 = sizeof(data2) / sizeof(data2[0]);Node* head1 = createlistf(data1, n1);Node* head2 = createlistf(data2, n2);Node* head3 = NULL;printf("合并为:\n");merge(&head1,&head2,&head3);printflist(head3);return 0;
}
8.合并单链表(有序:小到大):
#include<stdio.h>
#include<stdlib.h>
//创建节点
typedef char ElemType;
typedef struct Node {ElemType data;struct Node* next;
}Node;
//合并单链表
void merge(Node** head1, Node** head2, Node** head3)
{Node* p = *head1;Node* q = *head2;Node dummy; // 创建一个哑节点作为合并链表的开始Node* r = &dummy; // r指向哑节点while (q != NULL && p != NULL){if(p->data < q->data){r->next = p;p = p->next;}else{r->next = q;q = q->next;}r = r->next;}
if (p != NULL){r->next = p;}else if (q != NULL){r->next = q;}
*head3 = dummy.next; // 设置head3为合并后链表的头节点
}
Node* createlistr(int data[],int n)
{Node *head = NULL;;Node *tail = NULL;;int i = 0;for (i = 0; i < n; i++){Node* newNode = (Node*)malloc(sizeof(Node));if (newNode == NULL){printf("位置不合法");return NULL;}newNode->data = data[i];newNode->next = NULL;
if (head == NULL){head = newNode;tail = newNode;}else{tail->next = newNode;tail = newNode;}} return head;
}
void destroylist(Node **head)
{Node *current = *head;Node *next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
void printflist(Node *head)
{Node *p = head;while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");
}
int main()
{int data1[] = {1 , 3 , 5 , 7 , 9};int n1 = sizeof(data1) / sizeof(data1[0]);int data2[] = {2 , 4 , 6 , 8 , 10};int n2 = sizeof(data2) / sizeof(data2[0]);
Node* head1 = createlistr(data1, n1);Node* head2 = createlistr(data2, n2);Node* head3 = NULL;printf("合并为:\n");merge(&head1,&head2,&head3);printflist(head3);return 0;
}
9.用单链表实现交、并和差:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 100;
typedef struct
{double coef; //系数int exp; //指数
}PolyArray;
typedef struct pnode
{double coef; //系数int exp; //指数struct pnode* next;
}PolyNode;
//尾插法建立单链表
PolyNode* createlistR(PolyArray a[], int n)
{PolyNode* head = NULL;PolyNode* tail = NULL;
int i = 0;for (i = 0; i < n; i++){PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));
if (newNode == NULL){printf("位置不合法");return NULL;}
newNode->coef = a[i].coef;newNode->exp = a[i].exp;
newNode->next = NULL;
if (head == NULL){head = newNode;tail = newNode;}else{tail->next = newNode;tail = newNode;}}return head;
}
//打印
void DispPoly(PolyNode* head)
{bool first = true;
PolyNode* p = head;
while (p != NULL){
if (first){first = false;}else if (p->coef > 0){printf("+");}
switch (p->exp){case 0:printf("%g", p->coef);break;case 1:printf("%gx", p->coef);break;default:printf("%gx^%d", p->coef, p->exp);break;}
p = p->next;}
printf("\n");
}
void Sort(PolyNode** head)
{if (*head == NULL || (*head)->next == NULL) {return; // 如果链表为空或只有一个节点,无需排序}
bool swapped;PolyNode** ptr;do {swapped = false;ptr = head;
while ((*ptr)->next != NULL) {if ((*ptr)->exp < (*ptr)->next->exp) {// 交换节点PolyNode* temp = *ptr;*ptr = (*ptr)->next;temp->next = (*ptr)->next;(*ptr)->next = temp;
swapped = true;}ptr = &((*ptr)->next);}} while (swapped);
}
//销毁线性表
void destroylist(PolyNode** head)
{PolyNode* current = *head;PolyNode* next = NULL;
while (current != NULL){next = current->next;free(current);current = next;}
*head = NULL;
}
// 辅助函数:向结果多项式中添加项
void AddTerm(PolyNode** hc, double coef, int exp) {if (coef == 0) return; // 如果系数为0,直接返回,不添加
PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));newNode->coef = coef;newNode->exp = exp;newNode->next = NULL;
if (*hc == NULL || (*hc)->exp < exp) { // 如果链表为空或当前项指数大于链表头指数newNode->next = *hc;*hc = newNode;} else {PolyNode* prev = NULL;PolyNode* curr = *hc;while (curr != NULL && curr->exp > exp) {prev = curr;curr = curr->next;}if (curr != NULL && curr->exp == exp) { // 找到相同指数的项curr->coef += coef; // 合并系数if (curr->coef == 0) { // 如果合并后系数为0if (prev != NULL) {prev->next = curr->next; // 从链表中删除当前项} else {*hc = curr->next; // 更新头指针}free(curr); // 释放内存}free(newNode); // 释放新创建的节点,因为已经合并} else { // 没有找到相同指数的项,插入新项newNode->next = curr;if (prev != NULL) {prev->next = newNode;} else {*hc = newNode; // 更新头指针}}}
}
// 多项式乘法函数
void Mult(PolyNode* ha, PolyNode* hb, PolyNode** hc) {*hc = NULL; // 初始化结果多项式为空PolyNode* pa = ha;PolyNode* pb = NULL;
while (pa != NULL) {pb = hb;while (pb != NULL) {// 计算乘积项的系数和指数double coef = pa->coef * pb->coef;int exp = pa->exp + pb->exp;// 将乘积项添加到结果多项式中AddTerm(hc, coef, exp);pb = pb->next;}pa = pa->next;}
}
int main()
{PolyArray a[] = { {2,1},{3,3} };PolyArray b[] = { {2,1},{3,3} };
PolyNode* ha = createlistR(a, 2);PolyNode* hb = createlistR(b, 2);
//打印printf("初始多项式:\n");DispPoly(ha);DispPoly(hb);
Sort(&ha);Sort(&hb);printf("排序后多项式:\n");DispPoly(ha);DispPoly(hb);
//多项式相乘 PolyNode* hc; Mult(ha,hb,&hc);printf("多项式相乘后:\n");DispPoly(hc);
destroylist(&ha);destroylist(&hb);destroylist(&hc);
return 0;
}
10.实现两个多项式相加:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 100;
typedef struct
{double coef; //系数int exp; //指数
}PolyArray;
typedef struct pnode
{double coef; //系数int exp; //指数struct pnode* next;
}PolyNode;
//尾插法建立单链表
PolyNode* createlistR(PolyArray a[], int n)
{PolyNode* head = NULL;PolyNode* tail = NULL;
int i = 0;for (i = 0; i < n; i++){PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));
if (newNode == NULL){printf("位置不合法");return NULL;}
newNode->coef = a[i].coef;newNode->exp = a[i].exp;
newNode->next = NULL;
if (head == NULL){head = newNode;tail = newNode;}else{tail->next = newNode;tail = newNode;}}return head;
}
//打印
void DispPoly(PolyNode *head)
{bool first = true;
PolyNode* p = head;
while (p != NULL){
if (first){first = false;}else if(p->coef > 0){printf("+");}
switch (p->exp){case 0:printf("%g", p->coef);break;case 1:printf("%gx", p->coef);break;default:printf("%gx^%d", p->coef, p->exp);break;}p = p->next;}
printf("\n");
}
void Sort(PolyNode **head)
{if (*head == NULL || (*head)->next == NULL) {return; // 如果链表为空或只有一个节点,无需排序}
bool swapped;PolyNode **ptr;do {swapped = false;ptr = head;
while ((*ptr)->next != NULL) {if ((*ptr)->exp < (*ptr)->next->exp) {// 交换节点PolyNode *temp = *ptr;*ptr = (*ptr)->next;temp->next = (*ptr)->next;(*ptr)->next = temp;
swapped = true;}ptr = &((*ptr)->next);}} while (swapped);
}
// 向多项式中添加项
void AddTerm(PolyNode** head, double coef, int exp) {PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));newNode->coef = coef;newNode->exp = exp;newNode->next = NULL;
if (*head == NULL) {*head = newNode;} else {PolyNode* temp = *head;while (temp->next != NULL) {temp = temp->next;}temp->next = newNode;}
}
// 多项式相加函数
void AddPolynomials(PolyNode* pa, PolyNode* pb, PolyNode** pc) {*pc = NULL; // 初始化结果多项式为空while (pa != NULL && pb != NULL) {if (pa->exp > pb->exp) {AddTerm(pc, pa->coef, pa->exp);pa = pa->next;} else if (pa->exp < pb->exp) {AddTerm(pc, pb->coef, pb->exp);pb = pb->next;} else {double sumCoef = pa->coef + pb->coef;if (sumCoef != 0) { // 如果系数和不为0,则添加到结果多项式中AddTerm(pc, sumCoef, pa->exp);}pa = pa->next;pb = pb->next;}}
// 处理剩余项while (pa != NULL) {AddTerm(pc, pa->coef, pa->exp);pa = pa->next;}
while (pb != NULL) {AddTerm(pc, pb->coef, pb->exp);pb = pb->next;}
}
//销毁线性表
void destroylist(PolyNode **head)
{PolyNode *current = *head;PolyNode *next = NULL;while (current != NULL){next = current->next;free(current);current = next;}*head = NULL;
}
int main()
{PolyArray a[] = { {1.2,0},{2.5,1},{3.2,3},{-2.5,5} };PolyArray b[] = { {-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10} };
PolyNode* ha = createlistR(a, 4);PolyNode* hb = createlistR(b, 5);
//打印printf("初始多项式:\n");DispPoly(ha);DispPoly(hb); Sort(&ha);Sort(&hb);printf("排序后多项式:\n");DispPoly(ha);DispPoly(hb);
//多项式相加PolyNode* hc; AddPolynomials(ha,hb,&hc);printf("多项式相加后:\n");DispPoly(hc);destroylist(&ha);destroylist(&hb);destroylist(&hc);return 0;
}
11.实现两个多项式相乘:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 100;
typedef struct
{double coef; //系数int exp; //指数
}PolyArray;
typedef struct pnode
{double coef; //系数int exp; //指数struct pnode* next;
}PolyNode;
//尾插法建立单链表
PolyNode* createlistR(PolyArray a[], int n)
{PolyNode* head = NULL;PolyNode* tail = NULL;
int i = 0;for (i = 0; i < n; i++){PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));
if (newNode == NULL){printf("位置不合法");return NULL;}
newNode->coef = a[i].coef;newNode->exp = a[i].exp;
newNode->next = NULL;
if (head == NULL){head = newNode;tail = newNode;}else{tail->next = newNode;tail = newNode;}}return head;
}
//打印
void DispPoly(PolyNode* head)
{bool first = true;
PolyNode* p = head;
while (p != NULL){
if (first){first = false;}else if (p->coef > 0){printf("+");}
switch (p->exp){case 0:printf("%g", p->coef);break;case 1:printf("%gx", p->coef);break;default:printf("%gx^%d", p->coef, p->exp);break;}
p = p->next;}
printf("\n");
}
void Sort(PolyNode** head)
{if (*head == NULL || (*head)->next == NULL) {return; // 如果链表为空或只有一个节点,无需排序}
bool swapped;PolyNode** ptr;do {swapped = false;ptr = head;
while ((*ptr)->next != NULL) {if ((*ptr)->exp < (*ptr)->next->exp) {// 交换节点PolyNode* temp = *ptr;*ptr = (*ptr)->next;temp->next = (*ptr)->next;(*ptr)->next = temp;
swapped = true;}ptr = &((*ptr)->next);}} while (swapped);
}
//销毁线性表
void destroylist(PolyNode** head)
{PolyNode* current = *head;PolyNode* next = NULL;
while (current != NULL){next = current->next;free(current);current = next;}
*head = NULL;
}
// 辅助函数:向结果多项式中添加项
void AddTerm(PolyNode** hc, double coef, int exp) {if (coef == 0) return; // 如果系数为0,直接返回,不添加
PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));newNode->coef = coef;newNode->exp = exp;newNode->next = NULL;
if (*hc == NULL || (*hc)->exp < exp) { // 如果链表为空或当前项指数大于链表头指数newNode->next = *hc;*hc = newNode;} else {PolyNode* prev = NULL;PolyNode* curr = *hc;while (curr != NULL && curr->exp > exp) {prev = curr;curr = curr->next;}if (curr != NULL && curr->exp == exp) { // 找到相同指数的项curr->coef += coef; // 合并系数if (curr->coef == 0) { // 如果合并后系数为0if (prev != NULL) {prev->next = curr->next; // 从链表中删除当前项} else {*hc = curr->next; // 更新头指针}free(curr); // 释放内存}free(newNode); // 释放新创建的节点,因为已经合并} else { // 没有找到相同指数的项,插入新项newNode->next = curr;if (prev != NULL) {prev->next = newNode;} else {*hc = newNode; // 更新头指针}}}
}
// 多项式乘法函数
void Mult(PolyNode* ha, PolyNode* hb, PolyNode** hc) {*hc = NULL; // 初始化结果多项式为空PolyNode* pa = ha;PolyNode* pb = NULL;
while (pa != NULL) {pb = hb;while (pb != NULL) {// 计算乘积项的系数和指数double coef = pa->coef * pb->coef;int exp = pa->exp + pb->exp;// 将乘积项添加到结果多项式中AddTerm(hc, coef, exp);pb = pb->next;}pa = pa->next;}
}
int main()
{PolyArray a[] = { {2,1},{3,3} };PolyArray b[] = { {2,1},{3,3} };
PolyNode* ha = createlistR(a, 2);PolyNode* hb = createlistR(b, 2);
//打印printf("初始多项式:\n");DispPoly(ha);DispPoly(hb);
Sort(&ha);Sort(&hb);printf("排序后多项式:\n");DispPoly(ha);DispPoly(hb);
//多项式相乘 PolyNode* hc; Mult(ha,hb,&hc);printf("多项式相乘后:\n");DispPoly(hc);
destroylist(&ha);destroylist(&hb);destroylist(&hc);
return 0;
}