diff --git a/2224020123/chapter1/chapter2/.keep b/2224020123/chapter1/chapter2/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2224020123/chapter1/\346\225\264\346\225\260\345\217\215\350\275\254.cpp" "b/2224020123/chapter1/\346\225\264\346\225\260\345\217\215\350\275\254.cpp" index d2ad64e5c145fb78efa1e153a72518b04c76369b..d7a11b5c9f8628455ad0578aed5600f670031fa0 100644 --- "a/2224020123/chapter1/\346\225\264\346\225\260\345\217\215\350\275\254.cpp" +++ "b/2224020123/chapter1/\346\225\264\346\225\260\345\217\215\350\275\254.cpp" @@ -1,3 +1,24 @@ +<<<<<<< HEAD +#include +int main() +{ + int x; + int reverse(int x); + printf("请输入一串数字:"); + scanf("%d",&x); + printf("反转后为:%d",reverse(x)); +} + +int reverse(int x){ + long long int result=0; + while(x!=0) + { + result=result*10+x%10; + x=x/10; + } + return (int)result==result?(int)result:0; +} +======= #include int main() { @@ -19,3 +40,4 @@ int reverse(int x){ } +>>>>>>> master diff --git "a/2224020123/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020123/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..9730f2f650d4b75118c809b6841cd10b18c8350b --- /dev/null +++ "b/2224020123/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" @@ -0,0 +1,146 @@ +#include +using namespace std; + +//单链表存储结构 +struct LinkedList { + int data; + LinkedList* next; +}; + +//初始化操作(创建头节点) +void InitList(LinkedList*& L) { + L = new LinkedList; + L->next = NULL; +} + +//前插法(在头节点后插入 n 个结点) +void CreateList_H(LinkedList*& L, int n = 1) { + if (n < 1){ + cout << "ERROR" << endl; + exit(0); + } + //逆位序插入结点 + for (int i = 0; i < n; i++) { + LinkedList* p = new LinkedList; + cin >> p->data; + p->next = L->next; + L->next = p; + } +} + +//后插法 +void CreateList_R(LinkedList*& L, int n = 1) { + LinkedList* r = L->next; + while (r->next) { + r = r->next; + } + if (n < 1) { + cout << "ERROR" << endl; + exit(0); + } + for (int i = 0; i < n; i++) { + LinkedList* p = new LinkedList; + cin >> p->data; + p->next =r->next; + r->next = p; + r = p; + } +} + +//取值 +void GetElem(LinkedList*& L, int i, int& e) { + LinkedList* p = L->next; + int j = 1; + while (p && j < i) { + p = p->next; + j++; + } + if (!p || j > i) { + cout << "ERROR" << endl; + exit(0); + } + e = p->data; +} + +//查找 +LinkedList* LocateElem(LinkedList*& L, int e) { + LinkedList* p = L; + while (p && p->data != e) { + p = p->next; + } + return p; +} + +//插入 +void ListInsert(LinkedList*& L, int i, int e) { + LinkedList* p = L; + int j = 1; + while (p && j < i) { + p = p->next; + j++; + } + if (!p || j > i) { + cout << "ERROR" << endl; + exit(0); + } + LinkedList* s = new LinkedList; + s->data = e; + s->next = p->next; + p->next = s; +} + +//删除 +void ListDelete(LinkedList*& L, int i) { + LinkedList* p = L; + int j = 1; + while (p->next && j < i) { + p = p->next; + j++; + } + if (!p->next || j > i) { + cout << "ERROR" << endl; + exit(0); + } + LinkedList* q = p->next; + p->next = q->next; + delete q; +} + +//显示 +void ShowList(LinkedList*& L) { + LinkedList* p = L->next; + while (p){ + cout << p->data << " "; + p = p->next; + } + cout << endl; +} + + +int main() { + //创建头指针 + LinkedList* L; + + //初始化(创建头节点,让头指针和头节点关联) + InitList(L); + + cout << "前插:" << endl; + CreateList_H(L, 2); + ShowList(L); + + cout << "后插:" << endl; + CreateList_R(L, 3); + ShowList(L); + + cout << "插入元素:" << endl; + ListInsert(L, 3, 9); + ShowList(L); + + cout << "删除元素:" << endl; + ListDelete(L, 2); + ShowList(L); + + + system("pause"); + return 0; +} diff --git "a/2224020123/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020123/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d035e8014328ad12308e01ab2b1c526eb226d595 --- /dev/null +++ "b/2224020123/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" @@ -0,0 +1,102 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +typedef char ElemType; + +typedef struct Node { + ElemType data; + struct Node *next; +}LinkNode; + +void CreateListR(LinkNode *&L, ElemType a[], int n) +{ + LinkNode *node = NULL; + int i = 0; + L = (LinkNode *)malloc(sizeof(LinkNode)); + L->next = NULL;//创建头结点,其next域设置为NULL + + LinkNode *p = L; + for(i=0; idata = a[i]; + node->next = NULL; + p->next = node; + p = node; + } +} + +void DispList(LinkNode *L) +{ + LinkNode *p = L->next; + while(p) + { + printf("%c ",p->data); + p = p->next; + } + printf("\n"); +} + +void Split(LinkNode *L, ElemType x) +{ + //两个链表的头指针,用两个小链表储存小于和大于部分 + LinkNode* pLeftHead = (LinkNode *)malloc(sizeof(LinkNode));//小的 + LinkNode* pRightHead = (LinkNode *)malloc(sizeof(LinkNode));//大的 + //两个链表的当前最后一个元素 + LinkNode* left = pLeftHead; + LinkNode* right = pRightHead; + LinkNode* p = L->next; + while(p != NULL) + { + if (p->data < x) + { + left->next = p; + left = p; + } + else + { + right->next = p; + right = p; + } + p = p->next; + } + + //将right连接到left尾部,将两个链表合成一个 分割好的 + left->next = pRightHead->next; + right->next = NULL; + + L->next = pLeftHead->next; + + free(pLeftHead); + free(pRightHead); +} + +void DestroyList(LinkNode* L) +{ + LinkNode* node = L->next; + while(L != NULL) + { + node = L->next; + free(L); + L = node; + } +} + +int main() +{ + LinkNode *L = NULL; + ElemType a[] = "fgbedhca"; + int n = strlen(a); + CreateListR(L, a, n); + printf("划分前 L: "); + DispList(L); + ElemType x = 'd'; + printf("以%c进行划分\n", x); + Split(L, x); + printf("划分后 L: "); + DispList(L); + DestroyList(L); + + return 0; +} \ No newline at end of file diff --git "a/2224020123/chapter2/\345\260\206\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020123/chapter2/\345\260\206\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..480da52e5e00bc39aaaa1fc073438151418d54fa --- /dev/null +++ "b/2224020123/chapter2/\345\260\206\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" @@ -0,0 +1,102 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +typedef char ElemType; + +typedef struct Node { + ElemType data; + struct Node *next; +}LinkNode; + +void CreateListR(LinkNode *&L, ElemType a[], int n) +{ + LinkNode *node = NULL; + int i = 0; + L = (LinkNode *)malloc(sizeof(LinkNode)); + L->next = NULL;//ͷ㣬nextΪNULL + + LinkNode *p = L; + for(i=0; idata = a[i]; + node->next = NULL; + p->next = node; + p = node; + } +} + +void DispList(LinkNode *L) +{ + LinkNode *p = L->next; + while(p) + { + printf("%c ",p->data); + p = p->next; + } + printf("\n"); +} + +void Split(LinkNode *L, ElemType x) +{ + //ͷָ룬ССںʹڲ + LinkNode* pLeftHead = (LinkNode *)malloc(sizeof(LinkNode));//С + LinkNode* pRightHead = (LinkNode *)malloc(sizeof(LinkNode));// + //ĵǰһԪ + LinkNode* left = pLeftHead; + LinkNode* right = pRightHead; + LinkNode* p = L->next; + while(p != NULL) + { + if (p->data < x) + { + left->next = p; + left = p; + } + else + { + right->next = p; + right = p; + } + p = p->next; + } + + //rightӵleftβϳһ ָõ + left->next = pRightHead->next; + right->next = NULL; + + L->next = pLeftHead->next; + + free(pLeftHead); + free(pRightHead); +} + +void DestroyList(LinkNode* L) +{ + LinkNode* node = L->next; + while(L != NULL) + { + node = L->next; + free(L); + L = node; + } +} + +int main() +{ + LinkNode *L = NULL; + ElemType a[] = "fgbedhca"; + int n = strlen(a); + CreateListR(L, a, n); + printf("ǰ L: "); + DispList(L); + ElemType x = 'd'; + printf("%cл\n", x); + Split(L, x); + printf("ֺ L: "); + DispList(L); + DestroyList(L); + + return 0; +} diff --git "a/2224020123/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020123/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e3f469c99423ca9df86f0d5b7673bbb1bd8e7e15 --- /dev/null +++ "b/2224020123/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" @@ -0,0 +1,102 @@ +#include +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct +{ + ElemType data[MaxSize]; + int top; // 栈指针 +}SqStack; + +/*------------------------初始化栈s-------------------------*/ +void InitStack(SqStack *&s) +{ + s = (SqStack *)malloc(sizeof(SqStack)); + s->top = -1; +} +/*------------------------释放栈s-------------------------*/ +void ClearStack(SqStack *&s) +{ + free(s); +} + +/*------------------------求栈s的长度-------------------------*/ +int StackLength(SqStack *s) +{ + return (s->top + 1); +} + +/*------------------------判断栈s是否为空栈-------------------------*/ +int StackEmpty(SqStack *s) +{ + return (s->top == -1); +} + +/*------------------------进栈元素e-------------------------*/ +int Push(SqStack *&s, ElemType e) +{ + if(s->top == MaxSize - 1) + return 0; + s->top++; + s->data[s->top] = e; + return 1; +} + +/*------------------------出栈一个元素e-------------------------*/ +int Pop(SqStack *&s, ElemType &e) +{ + if(s->top == -1) + return 0; + e = s->data[s->top]; + s->top--; + return 1; +} + +/*------------------------取栈顶元素-------------------------*/ +int GetTop(SqStack *s, ElemType &e) +{ + if(s->top == -1) // 空栈 + return 0; + e = s->data[s->top]; + return 1; +} + +/*------------------------从栈顶到栈底输出元素-------------------------*/ +void DispStack(SqStack *s) +{ + int i; + + for(i = s->top; i >= 0; i--) + printf("%c ", s->data[i]); + printf("\n"); +} + +int main(void) +{ + ElemType e; + SqStack *s; + + printf("(1)初始化栈s\n"); + InitStack(s); + printf("(2)栈为%s\n", (StackEmpty(s) ? "空" : "非空")); + printf("(3)依次进栈元素a,b,c,d,e\n"); + Push(s, 'a'); + Push(s, 'b'); + Push(s, 'c'); + Push(s, 'd'); + Push(s, 'e'); + printf("(4)栈为%s\n", (StackEmpty(s) ? "空" : "非空")); + printf("(5)栈长度:%d\n", StackLength(s)); + printf("(6)从栈顶到栈底元素:"); + DispStack(s); + printf("(7)出栈序列:"); + while(!StackEmpty(s)) + { + Pop(s, e); + printf("%c ", e); + } + printf("\n"); + printf("(8)栈为%s\n", (StackEmpty(s) ? "空" : "非空")); + printf("(9)释放栈\n"); + return 0; +} \ No newline at end of file