目录
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.5
/*
要比较两个单词在字典中的顺序,可以逐个比较它们的字符。首先比较两个单词的第一个字符,如果相同,则继续比较下一个字符,直到找到不同的字符或者某个单词比较完毕。比较时,可以利用ASCII码进行比较,因为字母在ASCII码中是按顺序排列的。
*/
#include<stdio.h>
#include<string.h>int compareWords(char* word1, char* word2) {int len1 = strlen(word1);int len2 = strlen(word2);int minLen = len1 < len2 ? len1 : len2;//三目运算符,如果?前面的式子成立,返回:前面的值,否则返回后面的 int i;for (i = 0; i < minLen; i++) {if (word1[i] < word2[i]) {return -1; // word1在字典中排在前面} else if (word1[i] > word2[i]) {return 1; // word2在字典中排在前面}}// 如果前面的字符都相同,则长度较短的单词在字典中排在前面if (len1 < len2) {return -1;} else if (len1 > len2) {return 1;}// 两个单词相等return 0;
}int main() {char word1[] = "apple";char word2[] = "banana";int result = compareWords(word1, word2);if (result < 0) {printf("%s 在字典中排在 %s 前面\n", word1, word2);} else if (result > 0) {printf("%s 在字典中排在 %s 前面\n", word2, word1);} else {printf("%s 和 %s 相等\n", word1, word2);}return 0;
}
2.6
/*
定义两个指针,分别指向顺序表的头部和尾部。
依次交换头部和尾部指针指向的元素,直到两个指针相遇或者交叉。
当两个指针相遇或者交叉时,表就地逆置完成。
*/
#include<stdio.h>
#define MaxSize 50typedef int ElemType;
typedef struct
{ElemType data[MaxSize]; // 存放顺序表元素int length; // 存放顺序表的长度
} SqList; // 顺序表的类型void InitList(SqList *L)
{L->length = 0;
}void CreateList(SqList *L, ElemType a[], int n)
{int i;for (i = 0; i < n; i++){L->data[i] = a[i];}L->length = n;
}void ReverseList(SqList *L)
{int i, temp;for (i = 0; i < L->length / 2; i++){// 交换位置i和length-i-1的元素temp = L->data[i];L->data[i] = L->data[L->length - i - 1];L->data[L->length - i - 1] = temp;}
}void DisplayList(SqList *L)
{int i; for (i = 0; i < L->length; i++){printf("%d ", L->data[i]);}printf("\n");
}int main()
{SqList L;ElemType a[] = {1, 2, 3, 4, 5};CreateList(&L, a, 5); // 创建顺序表printf("原顺序表元素:");DisplayList(&L);ReverseList(&L); // 就地逆置顺序表printf("逆置后顺序表元素:");DisplayList(&L);return 0;
}
2.7
/*根据m和n的大小选择将谁接在谁的后面,设置指针分别指向两个链表开头,从端链表的开头开始向后移动,直到移动到最后,然后末尾接在下一个链表的第一个节点,两个链表连接在一起*/
#include<stdio.h>
#include<stdlib.h>typedef struct Node {int value;struct Node* next;
} LinkList;LinkList* connect(LinkList* ha, LinkList* hb, int m, int n) {LinkList* hc, *rear;if (m >= n) {hc = hb;rear = ha;}else {hc = ha;rear = hb;}LinkList* head = hc;while (hc->next != NULL) {hc = hc->next;}hc->next = rear->next;ha = hb = NULL;return head;
}LinkList* createList(int arr[], int n) {LinkList* rear;LinkList* l = (LinkList*)malloc(sizeof(LinkList));rear = l;int i;for (i = 0; i < n; i++) {LinkList* tmp = (LinkList*)malloc(sizeof(LinkList));tmp->value = arr[i];rear->next = tmp;rear = rear->next;}rear->next = NULL;return l;
}int main() {int a[] = { 1,5,6,2,1,7 };int b[] = { 7,3,12,2,9,8,11 };LinkList* l1 = createList(a, sizeof(a) / sizeof(a[0]));LinkList* l2 = createList(b, sizeof(b) / sizeof(b[0]));LinkList* l = connect(l1, l2, sizeof(a) / sizeof(a[0]), sizeof(b) / sizeof(b[0]));l = l->next;while (l != NULL) {printf("%d ", l->value);l = l->next;}return 0;
}
2.8
/*定义指针分别指向两个链表的开头,以及开头的下一个元素,然后使用一个指针在两个链表之间交叉移动,将两个链表串起来*/
#include<stdio.h>
#include<stdlib.h>
#define error -1;
#define OK 1;typedef int ElemType;//typedef表示重命名
typedef int status;typedef struct LNode
{ElemType data;struct LNode *next;
}LNode, *LinkList;LinkList creat()
{//使用尾插法建立带头结点的单链表;LinkList L;L = (LinkList)malloc(sizeof(LNode)); //开辟头结点空间if(!L){printf("memory malloc error!\n");}LinkList p = L;ElemType a;int len;printf("请输入待建表的表长:");scanf("%d", &len);if(len>0){int i;for(i=0; i<len; i++){ printf("请输入第%d个元素的值:", i+1);scanf("%d", &a);p->next = (LinkList)malloc(sizeof(LNode));p->next->data = a;p = p->next;}}p->next = NULL;return L;
}void print(LinkList L)
{LinkList p = L ->next;while(p != NULL){printf("%d\n", p->data);p = p->next;}
}
LinkList mix_connect(LinkList A, LinkList B)
{//A,B均为带头结点的单链表,长度均未显式存储if(A->next == NULL && B->next == NULL)return NULL;else{LinkList p = A->next;LinkList q = B->next;LinkList C = A; C->next = NULL;LinkList s = C; free(B);while(p != NULL && q != NULL){s->next = p;p = p->next;s = s->next;s->next = q;q = q->next;s = s->next;}if(p != NULL)s->next = p;if(q != NULL)s->next = q; //这两个if只会执行其中一个return C;}
}
int main()
{LinkList A = creat();LinkList B = creat();LinkList C=mix_connect(A, B);print(C);return 0;
}
2.9
/*
创建三个循环链表,分别用于存放字母字符、数字字符和其它键盘字符。
遍历原链表,将不同类别的字符按照类别链接到对应的循环链表中。
遍历完成后,将每个循环链表的尾节点的 next 指针指向头节点,形成循环链表。
*/
#include<stdio.h>
#include<stdlib.h>typedef struct Node {char data;struct Node* next;
} Node, *LinkList;void splitList(LinkList L, LinkList* alphabetList, LinkList* digitList, LinkList* otherList) {//遍历原链表,将不同类别的字符按照类别链接到对应的循环链表中。Node *p = L->next, *q, *r, *s, *t, *u;*alphabetList = (LinkList)malloc(sizeof(Node));*digitList = (LinkList)malloc(sizeof(Node));*otherList = (LinkList)malloc(sizeof(Node));(*alphabetList)->next = (*digitList)->next = (*otherList)->next = NULL;q = *alphabetList;r = *digitList;s = *otherList;while (p != NULL) {if ((p->data >= 'a' && p->data <= 'z') || (p->data >= 'A' && p->data <= 'Z')) {q->next = p;q = p;} else if (p->data >= '0' && p->data <= '9') {r->next = p;r = p;} else {s->next = p;s = p;}p = p->next;}q->next = (*alphabetList)->next;r->next = (*digitList)->next;s->next = (*otherList)->next;// 移除哑结点,并连接头指针到第一个有效节点上*alphabetList = (*alphabetList)->next;*digitList = (*digitList)->next;*otherList = (*otherList)->next;// 将最后一个节点的next指针置为NULL,避免环形链表q->next = NULL;r->next = NULL;s->next = NULL;
}void printList(LinkList L) {Node* p = L;while (p != NULL) {printf("%c ", p->data);p = p->next;}printf("\n");
}int main() {char chars[] = "a1b2c3!@#d4";LinkList L = (LinkList)malloc(sizeof(Node));Node* p = L;int i;for (i = 0; i < sizeof(chars) / sizeof(chars[0]); i++) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->data = chars[i];newNode->next = NULL;p->next = newNode;p = newNode;}LinkList alphabetList, digitList, otherList;//分别表示字母,数字和其它 splitList(L, &alphabetList, &digitList, &otherList);printf("Alphabet List: ");printList(alphabetList);printf("Digit List: ");printList(digitList);printf("Other List: ");printList(otherList);return 0;
}
2.10
#include<stdio.h>typedef int ElemType;
typedef struct DuLNode{ElemType data;struct DuLNode *prior;struct DuLNode *next;
} DuLNode, *DuLinkList;//初始化双向循环链表(头节点)
DuLinkList InitDuLinkList()
{DuLNode *dnode = (DuLNode *)malloc(sizeof(DuLNode));dnode->data = 0;dnode->prior = dnode;dnode->next = dnode;return dnode;
}
//创建链表元素
void CreateDuLinkList(DuLinkList L, ElemType *arr, int n)
{DuLNode *dnode;DuLNode *p = L;int i;for (i = 0; i < n; i++){dnode = (DuLNode *)malloc(sizeof(DuLNode));dnode->data = arr[i];dnode->next = L;dnode->prior = p;p->next = dnode;p = p->next;}
}
//输出链表
void ShowList(DuLinkList L)
{int i;DuLNode *r = L->next;while (r->next != L){printf("%d ", r->data);r = r->next;}printf("%d ", r->data);printf("\n");
}/*
将L中的元素,按如下规则插入新表,并返回新表。
(1,2)->(1,3,2)->(1,3,4,2)->(1,3,5,4,2)->(1,3,5,6,4,2)->...
*/
DuLinkList Transform(DuLinkList L)
{DuLinkList Lnew = InitDuLinkList();DuLNode *p, *q, *pa, *pb;q = p = L->next;pa = pb = Lnew;while (p != L){if (p != L){/*L中寄数个数据插入Lnew*/q = p->next;//保留 L 链表//pa之后插入pp->prior = pa;pa->next = p;p->next = pb;pb->prior = p;pa = pa->next;p = q;//p指向 待操作 L}if (p != L){/*L中偶数个数据插入Lnew*/q = p->next;//保留 L 链表//pb之前插入pp->next = pb;pb->prior = p;p->prior = pa;pa->next = p;pb = pb->prior;p = q;//p指向 待操作 L}}return Lnew;
}int main()
{ElemType data[7] = { 1, 2, 3, 4, 5, 6, 7 };//测试数据1(奇数个)//ElemType data[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };//测试数据2(偶数个)int length = sizeof(data) / sizeof(ElemType);DuLinkList L = InitDuLinkList(), Lnew;CreateDuLinkList(L, data, length);printf("原链表:");ShowList(L);Lnew = Transform(L);printf("改造表:");ShowList(Lnew);getchar();return 0;
}
2.11
/*从最小的位置开始,将后面的数据依次往前赋值,然后指针向后移动*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>typedef struct node
{int data;//数据int length;//长度struct node *next;
}Node,*LinkList;
//删除超过范围的内容
void deLinkList(LinkList L,int mink,int maxk)
{LinkList p,q;p=L;while(p!=NULL){if(p->data>mink&&p->data<maxk){printf("找到错误目标\n");q=p->next;p->data=q->data;p->next=q->next;free(q);}else p=p->next;}
}
//得到要输入的数据数量
int getnumber()
{int n;printf("请输入你要录入的数据数量:");scanf("%d",&n);return n;
}/*建立链表
后插入法创造链表
算法时间复杂度为O(n)
*/
LinkList init(int n)
{int i;LinkList L;Node *r;Node *p;L=(LinkList)malloc(sizeof(Node));//建立一个带头指针的空链表L->next=NULL;r=L;//尾指针r指向头结点for(i=1;i<=n;i++){p=(Node *)malloc(sizeof(Node));//生成新结点printf("输入第%d位的数据:",i);//输入元素值赋给新结点*p的数据域scanf("%d",&p->data);p->next=NULL;//将新结点*p插入尾结点*r之后r->next=p;//r指向新的尾结点*pr=p;}L->length=n;return L;
}
//展示信息
//算法的时间复杂度未O(n),n为单链表中的数据节点的个数
void show(LinkList L)
{int i=0;Node *p;p=L->next;while(p){i++;printf("第%d位的数据:%d\n",i,p->data);p=p->next;}printf("\n信息已全部输出\n");}
//主函数
int main()
{int n,mink,maxk;LinkList L;n=getnumber();L=init(n);show(L);//删除mink~maxk之间的元素printf("请输入mink、maxk的值\n");scanf("%d%d",&mink,&maxk);deLinkList(L,mink,maxk);show(L);free(L);
}
2.12
#include<stdio.h>
#include<stdlib.h>typedef struct LNode
{int data;struct LNode *next;
}LNode, *LinkList;int InitList(LinkList *L)
{*L = (LinkList)malloc(sizeof(LNode));(*L)->next = NULL;return 0;
}int CreateList(LinkList *L, int e)
{LinkList p = *L;while(p->next)p = p->next;LinkList temp = (LinkList)malloc(sizeof(LNode));temp->data = e;temp->next = NULL;p->next = temp;return 0;
}int DispList(LinkList *L)
{LinkList p = (*L)->next;while(p){printf("%d\t", p->data);p = p->next;}return 0;
}int ListOppose(LinkList *L)
{LinkList q, p, s;p = *L;p = p->next;(*L)->next = NULL;while(p){q = p;p = p->next;q->next = (*L)->next;(*L)->next = q;}return 0;
}int ListMerge(LinkList *A, LinkList *B, LinkList *C)
{LinkList qa, pa, pb, qb;pa = *A; qa = pa; pa = pa->next;pb = (*B)->next;*C = *A;while(pa && pb){if(pa->data <= pb->data){qb = pb;pb = pb->next;qb->next = qa->next;qa->next = qb;qa = qa->next;}else{qb = pb;pb = pb->next;qb->next = pa->next;pa->next = qb;pa = pa->next;}}free(*B);return 0;
}int main()
{LinkList A, B, C;InitList(&A); InitList(&B);int i;for(i = 1; i< 6; i++)CreateList(&A, i);int j;for(j = 4; j < 11; j++)CreateList(&B, j);printf("\nA 链表为:\n");DispList(&A);printf("\nB 链表为:\n");DispList(&B);printf("\nC 链表为:\n");ListOppose(&A);ListOppose(&B);printf("\n逆置后的 A 链表为:\n");DispList(&A);printf("\n逆置后的 B 链表为:\n");DispList(&B);ListMerge(&A, &B, &C);printf("\n合并后的 C 链表为:\n");DispList(&C);return 0;
}
2.13
#include<stdio.h>
#include<stdlib.h>typedef struct {int* data; // 存放元素的数组int length; // 当前长度int maxSize; // 最大长度
} SqList;// 初始化顺序表
void initList(SqList* list, int maxSize) {list->data = (int*)malloc(maxSize * sizeof(int));list->length = 0;list->maxSize = maxSize;
}// 在顺序表中查找元素的位置,返回位置下标
int locateElem(SqList* list, int elem) {int i;for (i = 0; i < list->length; i++) {if (list->data[i] == elem) {return i;}}return -1; // 找不到返回-1
}// 添加元素到顺序表的末尾
void addElem(SqList* list, int elem) {if (list->length < list->maxSize) {list->data[list->length++] = elem;}
}// 求A和B的交集,结果保存在C中
void intersection(SqList* A, SqList* B, SqList* C) {int i = 0, j = 0;while (i < A->length && j < B->length) {if (A->data[i] == B->data[j]) {addElem(C, A->data[i]);i++;j++;} else if (A->data[i] < B->data[j]) {i++;} else {j++;}}
}// 打印顺序表的元素
void printList(SqList* list) {int i;for (i = 0; i < list->length; i++) {printf("%d ", list->data[i]);}printf("\n");
}int main() {SqList A, B, C;int maxSize = 10; // 假设最大长度为10initList(&A, maxSize);initList(&B, maxSize);initList(&C, maxSize);// 假设A和B的元素是有序且不重复的int a[] = {1, 3, 5, 7, 9};int b[] = {3, 4, 5, 6, 7};int i;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) {addElem(&A, a[i]);}int j;for (j = 0; j < sizeof(b) / sizeof(b[0]); j++) {addElem(&B, b[j]);}printf("集合A:");printList(&A);printf("集合B:");printList(&B);intersection(&A, &B, &C);printf("集合C(A和B的交集):");printList(&C);free(A.data);free(B.data);free(C.data);return 0;
}