diff --git a/List/List.c b/List/List.c deleted file mode 100644 index cfce5244562cba2864c96db9176342e658c814a9..0000000000000000000000000000000000000000 --- a/List/List.c +++ /dev/null @@ -1,114 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"List.h" -LTNode* LTBuyNode(LTDataType x) -{ - LTNode* node = (LTNode*)malloc(sizeof(LTNode)); - if (node == NULL) - { - perror("malloc fail!"); - exit(1); - } - node->Data=x; - node->prev = node->next = node; - return node; -} -LTNode* LTInit() -{ - LTNode* phead = LTBuyNode(-1); - return phead; -} - -void LTDestroy(LTNode* phead) -{ - LTNode* pcur = phead->next; - while (pcur != phead) - { - LTNode* next = pcur->next; - free(pcur); - pcur = next; - } - free(phead); - phead = NULL; -} -void LTPrint(LTNode* phead) -{ - LTNode* pcur = phead->next; - while (pcur != phead) - { - printf("%d->", pcur->Data); - pcur = pcur->next; - } - printf("\n"); -} -void LTPushBack(LTNode* phead, LTDataType x) -{ - assert(phead); - LTNode* ptail = phead->prev; - LTNode* node = LTBuyNode(x); - node->next = phead; - node->prev = ptail; - phead->prev = node; - ptail->next = node; -} -void LTPopBack(LTNode* phead) -{ - assert(phead && phead->next != phead); - LTNode* ptail = phead->prev; - ptail->prev->next = phead; - phead->prev = ptail->prev; - free(ptail); - ptail = NULL; -} -void LTPushFront(LTNode* phead, LTDataType x) -{ - assert(phead); - LTNode* node = LTBuyNode(x); - node->prev = phead; - node->next = phead->next; - phead->next = node; - node->next->prev = node; -} -void LTPopFront(LTNode* phead) -{ - assert(phead && phead->next != phead); - LTNode* del = phead->next; - del->next->prev = phead; - phead->next = del->next; - free(del); - del = NULL; -} -void LTInsert(LTNode* pos, LTDataType x) -{ - assert(pos); - - LTNode* newnode = LTBuyNode(x); - //pos newnode pos->next - newnode->next = pos->next; - newnode->prev = pos; - - pos->next->prev = newnode; - pos->next = newnode; -} -void LTErase(LTNode* pos) -{ - assert(pos); - //pos->prev pos pos->next - pos->next->prev = pos->prev; - pos->prev->next = pos->next; - - free(pos); - pos = NULL; -} -LTNode* LTFind(LTNode* phead, LTDataType x) -{ - LTNode* pcur = phead->next; - while (pcur != phead) - { - if (pcur->Data == x) - { - return pcur; - } - pcur = pcur->next; - } - return NULL; -} diff --git a/List/List.h b/List/List.h deleted file mode 100644 index 4a172912e9bfab5b5584b2160a6dc2e092524ec0..0000000000000000000000000000000000000000 --- a/List/List.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include -#include -#include - - -typedef int LTDataType; -typedef struct ListNode -{ - struct ListNode* prev; - struct ListNode* next; - LTDataType Data; -}LTNode; - -//void LTInit(LTNode** pphead); -LTNode* LTInit(); -void LTDestroy(LTNode* phead); -void LTPrint(LTNode* phead); -void LTPushBack(LTNode* phead, LTDataType x); -void LTPopBack(LTNode* phead); -void LTPushFront(LTNode* phead, LTDataType x); -void LTPopFront(LTNode* phead); -void LTInsert(LTNode* pos, LTDataType x); -void LTErase(LTNode* pos); -LTNode* LTFind(LTNode* phead, LTDataType x); \ No newline at end of file diff --git a/List/test.c b/List/test.c deleted file mode 100644 index 422170d2f06d6af32115225761b6ef91db0bdd5d..0000000000000000000000000000000000000000 --- a/List/test.c +++ /dev/null @@ -1,35 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"List.h" - -void ListTest01() -{ - LTNode* lt = LTInit(); - - LTPushBack(lt, 2); - LTPrint(lt); - LTPushBack(lt, 3); - LTPrint(lt); - - LTPushFront(lt, 1); - LTPrint(lt); - LTPushFront(lt, 0); - LTPrint(lt); - - LTPopBack(lt); - LTPrint(lt); - LTPopBack(lt); - LTPrint(lt); - - LTPopFront(lt); - LTPrint(lt); - - LTDestroy(lt); - lt = NULL; - -} - -int main() -{ - ListTest01(); - return 0; -} \ No newline at end of file diff --git a/OJ-BinaryTreeSimple b/OJ-BinaryTreeSimple new file mode 100644 index 0000000000000000000000000000000000000000..f6d46afa6002f7a34d304b7104b89b6d49c97f86 --- /dev/null +++ b/OJ-BinaryTreeSimple @@ -0,0 +1,54 @@ +bool isSameTree(struct TreeNode* p, struct TreeNode* q)//相同二叉树 +{ +if(p==NULL&&q==NULL) + return true; +if(p==NULL||q==NULL) +return false; + +if(p->val!=q->val) +return false; + +return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); +} + +bool isUnivalTree(struct TreeNode* root) //单值二叉树 +{ + if(root==NULL) + return true; + if((root->left&&root->left->val!=root->val) + ||(root->right&&root->right->val!=root->val)) + return false; + return isUnivalTree(root->left)&&isUnivalTree(root->right); +} + +bool _isSymmetric(struct TreeNode* q,struct TreeNode* p) //对称二叉树的判断 +{ + if(q==NULL&&p==NULL) + return true; + if(q==NULL||p==NULL) + return false; + if(q->val!=p->val) + return false; + +return _isSymmetric(q->left,p->right)&&_isSymmetric(q->right,p->left); +} + +bool isSymmetric(struct TreeNode* root) +{ + return _isSymmetric(root->left,root->right); +} + +bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot) +{ + +if(root==NULL) +return false; + +if(isSameTree(root,subRoot)) +return true; + +return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot); + +} + + diff --git a/Queue/Queue.c b/OJ-Queue1 similarity index 37% rename from Queue/Queue.c rename to OJ-Queue1 index 70331721f2f01b05c5fe8da329ef1c04f3a0352c..168ef25148d817ec3617024bf23f1fdc9d579b12 100644 --- a/Queue/Queue.c +++ b/OJ-Queue1 @@ -1,5 +1,17 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"Queue.h" +typedef int QDataType; +typedef struct QueueNode +{ + QDataType data; + struct QueueNode* next; +}QNode; + +typedef struct Queue +{ + QNode* phead; + QNode* ptail; + int size; +}Queue; + void QueueInit(Queue* q) { assert(q); @@ -31,12 +43,12 @@ void QueuePush(Queue* q, QDataType x) void QueuePop(Queue* q) { assert(q&&q->size>0); - if (q->phead->next == NULL)//һ¸ö½ڵã + if (q->phead->next == NULL)//一个节点 { free(q->phead); q->phead = q->ptail = NULL; } - else//¶à¸ö½ڵã + else//多个节点 { QNode* next = q->phead->next; free(q->phead); @@ -76,4 +88,81 @@ int QueueSize(Queue* q) { assert(q); return q->size; -} \ No newline at end of file +} + + +typedef struct +{ + //只是两个指针,需要为他们单独开辟内存,如果是直接创建queue的变量则不用,不过不方便使用 + Queue* q1; + Queue* q2; +} MyStack; + + +MyStack* myStackCreate() { + MyStack* ps=(MyStack*)malloc(sizeof(MyStack)); + ps->q1 = (Queue*)malloc(sizeof(Queue)); // 分配内存 + ps->q2 = (Queue*)malloc(sizeof(Queue)); + QueueInit(ps->q1); + QueueInit(ps->q2); + return ps; +} + +void myStackPush(MyStack* obj, int x) +{ + assert(obj); + if(QueueEmpty(obj->q1)&&QueueEmpty(obj->q2)) + { + QueuePush(obj->q1,x); + } + else + { + Queue* valid=QueueEmpty(obj->q1)?obj->q2:obj->q1; + QueuePush(valid,x); + } +} + +int myStackPop(MyStack* obj) +{ +assert(obj&&!(QueueEmpty(obj->q1)&&QueueEmpty(obj->q2))); +Queue* valid=obj->q1; +Queue* empty=obj->q2; +if(QueueEmpty(obj->q1)) +{ + valid=obj->q2; + empty=obj->q1; +} + +while(valid->size>1) +{ +QueuePush(empty,QueueFront(valid)); +QueuePop(valid); +} + +int ret=QueueFront(valid); +QueuePop(valid); + +return ret; +} + +int myStackTop(MyStack* obj) +{ + assert(obj); + Queue* valid=QueueEmpty(obj->q1)?obj->q2:obj->q1; + return QueueRear(valid); +} + +bool myStackEmpty(MyStack* obj) +{ + assert(obj); + return QueueEmpty(obj->q1)&&QueueEmpty(obj->q2); +} + +void myStackFree(MyStack* obj) +{ + assert(obj); + QueueDestroy(obj->q1); + QueueDestroy(obj->q2); + free(obj); +} + diff --git a/Stack/Stack.c b/OJ-Stack1 similarity index 57% rename from Stack/Stack.c rename to OJ-Stack1 index 422ed7be76bfa1eeb08f72fb90780bb814c24f77..1610d35aecc5379efc696d0499b1ed00166d7d47 100644 --- a/Stack/Stack.c +++ b/OJ-Stack1 @@ -1,5 +1,11 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"Stack.h" +typedef char STDataType; +typedef struct Stack +{ + STDataType* a; + int top; + int capacity; +}Stack; + void STInit(Stack* ps) { @@ -53,4 +59,38 @@ bool STEmpty(Stack* ps) assert(ps); return ps->top == 0; +} + + + + +bool isValid(char* s) +{ + Stack st; + STInit(&st); + while(*s) + { + if(*s=='('||*s=='['||*s=='{') + { + STPush(&st,*s); + } + else + { + if(STEmpty(&st)) + { + return false; + } + char top=STTop(&st); + STPop(&st); + +if((*s==')'&&top!='(')||(*s=='}'&&top!='{')||(*s==']'&&top!='[')) +{ +return false; +} + } + + s++; + } + + return STEmpty(&st); } \ No newline at end of file diff --git a/OJ-Stack2 b/OJ-Stack2 new file mode 100644 index 0000000000000000000000000000000000000000..206d5df16abd2fbcd698b43e42f40591abad272e --- /dev/null +++ b/OJ-Stack2 @@ -0,0 +1,150 @@ +typedef int STDataType; +typedef struct Stack +{ + STDataType* a; + int top; + int capacity; +}Stack; + +void STInit(Stack* ps) +{ + assert(ps); + ps->a = NULL; + ps->top = ps->capacity = 0; +} +void STPush(Stack* ps,STDataType x) +{ + assert(ps); + if (ps->capacity == ps->top) + { + int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; + STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType)); + if (tmp == NULL) + { + perror("realloc failed"); + return; + } + ps->a = tmp; + ps->capacity = newcapacity; + } + ps->a[ps->top++] = x; +} +void STPop(Stack* ps) +{ + assert(ps&&ps->top!=0); + ps->top--; +} +STDataType STTop(Stack* ps) +{ + assert(ps&&ps->top>0); + + return ps->a[ps->top - 1]; +} +int STSize(Stack* ps) +{ + assert(ps); + return ps->top; +} + +void STDestory(Stack* ps) +{ + assert(ps); + free(ps->a); + ps->a = NULL; + ps->top = ps->capacity = 0; +} +bool STEmpty(Stack* ps) +{ + assert(ps); + + return ps->top == 0; +} + + +typedef struct +{ + Stack* ps1; + Stack* ps2; +} MyQueue; + + +MyQueue* myQueueCreate() +{ + MyQueue* ret=(MyQueue*)malloc(sizeof(MyQueue)); + ret->ps1=(Stack*)malloc(sizeof(Stack)); + ret->ps2=(Stack*)malloc(sizeof(Stack)); + STInit(ret->ps1); + STInit(ret->ps2); + + return ret; +} + +void myQueuePush(MyQueue* obj, int x) +{ +assert(obj); +if(STEmpty(obj->ps1)&&STEmpty(obj->ps2)) +{ +STPush(obj->ps1,x); +} +else +{ + Stack* nonempty=STEmpty(obj->ps1)?obj->ps2:obj->ps1; + STPush(nonempty,x); +} + +} + +int myQueuePop(MyQueue* obj) +{ + assert(obj&&!(STEmpty(obj->ps1)&&STEmpty(obj->ps2))); +Stack* nonempty=obj->ps1; +Stack* empty=obj->ps2; +if(STEmpty(obj->ps1)) +{ + nonempty=obj->ps2; + empty=obj->ps1; +} + +while(STSize(nonempty)>1) +{ + STPush(empty,STTop(nonempty)); + STPop(nonempty); +} + +int ret=STTop(nonempty); +STPop(nonempty); + +//区别于队列实现栈,由于栈一端进一端出会颠倒方向,所以拿出队头后还得重新倒回去 +//所以两个栈有一个是固定空,一个是固定存的,之前的假设逻辑可以一开始就确认好 +while(STSize(empty)) +{ + STPush(nonempty,STTop(empty)); + STPop(empty); +} + +return ret; +} + +int myQueuePeek(MyQueue* obj) +{ + assert(obj); + Stack* nonempty=STEmpty(obj->ps1)?obj->ps2:obj->ps1; + + return nonempty->a[0]; +} + +bool myQueueEmpty(MyQueue* obj) +{ +assert(obj); + +return STEmpty(obj->ps1)&&STEmpty(obj->ps2); +} + +void myQueueFree(MyQueue* obj) +{ + assert(obj); + STDestory(obj->ps1); + STDestory(obj->ps2); + + free(obj); +} diff --git a/OJ-circlelist1 b/OJ-circlelist1 new file mode 100644 index 0000000000000000000000000000000000000000..e3a11a747ca258894c977060621b0f19304c9ef9 --- /dev/null +++ b/OJ-circlelist1 @@ -0,0 +1,13 @@ +bool hasCycle(struct ListNode *head) +{ +struct ListNode* fast=head,* slow=head; + +while(fast&&fast->next) +{ + fast=fast->next->next; + slow=slow->next; + if(slow==fast) + return true; +} +return false; +} \ No newline at end of file diff --git a/OJ-circlelist2 b/OJ-circlelist2 new file mode 100644 index 0000000000000000000000000000000000000000..2d2416b153baec4544a272c992cddedddfaff3f9 --- /dev/null +++ b/OJ-circlelist2 @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode *detectCycle(struct ListNode *head) +{ + struct ListNode *fast=head,*slow=head,*meet; + while(fast&&fast->next) + { + +fast=fast->next->next; +slow=slow->next; + +if(fast==slow) +{ +meet=slow; +while(head!=meet) +{ + head=head->next; + meet=meet->next; +} +return meet; +} + } + return NULL; +} \ No newline at end of file diff --git a/OJ-list1 b/OJ-list1 new file mode 100644 index 0000000000000000000000000000000000000000..cf9ceaf6a2e79c9b01ea7de4c0b9c9b7615b74d3 --- /dev/null +++ b/OJ-list1 @@ -0,0 +1,65 @@ +/** + * Definition for a Node. + * struct Node { + * int val; + * struct Node *next; + * struct Node *random; + * }; + */ + +struct Node* copyRandomList(struct Node* head) { + typedef struct Node Node; + +Node *cur=head,*copy; +//复制节点并插到节点后 +while(cur) +{ +//复制 +copy=(Node*)malloc(sizeof(Node)); +copy->val=cur->val; +//插入 +copy->next=cur->next; +cur->next=copy; +//跳转下个节点 +cur=copy->next; + +} + +cur=head; +//利用random->next是copy的节点复制原链表random +while(cur) +{ +copy=cur->next; +if(cur->random==NULL) +{ + copy->random=NULL; +} +else +{ + copy->random=cur->random->next; +} +cur=copy->next; +} + +// 完成复制,记得重置cur到head +cur=head; +//将copy的节点重新连接到一个新的头结点(可以不管原链表的复原) +Node* newhead=NULL,*tail=NULL; +while(cur) +{ +copy=cur->next; +//无哨兵位尾插节点常规操作 +if(tail==NULL) +{ + newhead=tail=copy; +} +else +{ + tail->next=copy; + tail=tail->next; +} +cur=copy->next; +} + +return newhead; +} \ No newline at end of file diff --git a/OJ-queue b/OJ-queue new file mode 100644 index 0000000000000000000000000000000000000000..a6fd6f57816f99a0f085c07e49002e70e704973c --- /dev/null +++ b/OJ-queue @@ -0,0 +1,81 @@ +typedef struct { + int* a; + int head; + int tail; + int k; +} MyCircularQueue; + + +MyCircularQueue* myCircularQueueCreate(int k) +{ +MyCircularQueue* ret=(MyCircularQueue*)malloc(sizeof(MyCircularQueue)); +ret->a=(int*)malloc((k+1)*sizeof(int)); +ret->k=k; +ret->head=ret->tail=0; +return ret; +} +bool myCircularQueueIsEmpty(MyCircularQueue* obj) +{ +return obj->head==obj->tail; +} + +bool myCircularQueueIsFull(MyCircularQueue* obj) +{ +return (obj->tail+1)%(obj->k+1)==obj->head; +} + + +bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) +{ +if(myCircularQueueIsFull(obj)) +{ + return false; +} +else{ + obj->a[obj->tail]=value; + ++obj->tail; + obj->tail%=(obj->k+1); + //tail指向队尾的下个位置 + return true; +} + + +} + +bool myCircularQueueDeQueue(MyCircularQueue* obj) +{ + if(myCircularQueueIsEmpty(obj)) + return false; + +++obj->head; +obj->head%=(obj->k+1); +return true; + +} + +int myCircularQueueFront(MyCircularQueue* obj) +{ + if(myCircularQueueIsEmpty(obj)) + return -1; + else + return obj->a[obj->head]; + +} + +int myCircularQueueRear(MyCircularQueue* obj) +{ + if(myCircularQueueIsEmpty(obj)) + return -1; + + else + return obj->a[(obj->tail-1+obj->k+1)%(obj->k+1)]; + //加k+1对正数模运算没影 响,但可以让负数变正数 + //也可以用三目运算符对tail等于0的情况单独赋值 +} + +void myCircularQueueFree(MyCircularQueue* obj) +{ + assert(obj); + free(obj->a); + free(obj); +} \ No newline at end of file diff --git a/Queue/Queue.h b/Queue/Queue.h deleted file mode 100644 index cc38ea5f573568f53c2c9d04f1afff9022588045..0000000000000000000000000000000000000000 --- a/Queue/Queue.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include -#include -#include -#include - -typedef int QDataType; -typedef struct QueueNode -{ - QDataType data; - struct QueueNode* next; -}QNode; - -typedef struct Queue -{ - QNode* phead; - QNode* ptail; - int size; -}Queue; - -void QueueInit(Queue* q); -void QueuePush(Queue* q,QDataType x); -void QueuePop(Queue* q); -void QueueDestroy(Queue* q); -QDataType QueueFront(Queue* q); -QDataType QueueRear(Queue* q); -bool QueueEmpty(Queue* q); -int QueueSize(Queue* q); diff --git a/Queue/test.c b/Queue/test.c deleted file mode 100644 index 91c54cf4d74c8e07969895d89a764416a7c037f0..0000000000000000000000000000000000000000 --- a/Queue/test.c +++ /dev/null @@ -1,48 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"Stack.h" -#include"Queue.h" -void StackTest() -{ - Stack st; - STInit(&st); - STPush(&st, 1); - STPush(&st, 2); - STPush(&st, 3); - - while (!STEmpty(&st)) - { - printf("%d ", STTop(&st)); - STPop(&st); - } - STDestory(&st); -} - -void QueueTest() -{ - Queue q; - QueueInit(&q); - QueuePush(&q, 1); - QueuePush(&q, 2); - QueuePush(&q, 3); - printf("size=%d\n", QueueSize(&q)); - - while (!QueueEmpty(&q)) - { - printf("%d ",QueueFront(&q)); - QueuePop(&q); - } - - QueueDestroy(&q); - -} - -int main() -{ - -#if 0 - StackTest(); -#endif - - QueueTest(); - return 0; -} \ No newline at end of file diff --git a/SLTNode/SLTNode/SLTNode.c b/SLTNode/SLTNode/SLTNode.c deleted file mode 100644 index 9116a6da09efba75003968da08f30723dc1b0734..0000000000000000000000000000000000000000 --- a/SLTNode/SLTNode/SLTNode.c +++ /dev/null @@ -1,170 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"SLTNode.h" - -void SLTPrint(SLTNode* phead) -{ - SLTNode* pcur = phead; - while (pcur) - { - printf("%d->", pcur->data); - pcur = pcur->next; - } - printf("NULL"); - printf("\n"); -} - -SLTNode* SLTBuyNode(SLTDataType x) -{ - SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode)); - if (newnode == NULL) - { - perror("malloc failed!"); - exit(1); - } - newnode->data = x; - newnode->next = NULL; - return newnode; -} - -void SLTPushBack(SLTNode** pphead, SLTDataType x) -{ - assert(pphead); - SLTNode*newnode = SLTBuyNode(x); - if (*pphead==NULL) - { - *pphead = newnode; - } - else - { - SLTNode* ptail = *pphead; - while (ptail->next) - { - ptail = ptail->next; - } - ptail->next = newnode; - } - -} - -void SLTPushFront(SLTNode** pphead, SLTDataType x) -{ - assert(pphead); - SLTNode* newnode = SLTBuyNode(x); - newnode->next = *pphead; - *pphead = newnode; -} - -void SLTPopBack(SLTNode** pphead) -{ - assert(pphead&&*pphead); - if ((*pphead)->next == NULL) - { - free(*pphead); - *pphead = NULL; - } - else - { - SLTNode* pcur = *pphead; - SLTNode* ptail = *pphead; - while (ptail->next) - { - pcur = ptail; - ptail = ptail->next; - } - free(ptail); - ptail = NULL; - pcur->next = NULL; - } -} - -void SLTPopFront(SLTNode** pphead) -{ - assert(pphead && *pphead); - SLTNode* next = (*pphead)->next; - free(*pphead); - *pphead = next; -} - -SLTNode* SLTFind(SLTNode* phead, SLTDataType x) -{ - SLTNode* pcur = phead; - while (pcur) - { - if (pcur->data == x) - { - return pcur; - } - pcur = pcur->next; - } - return NULL; -} - -void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x) -{ - assert(pphead && pos&&*pphead); - if ((*pphead)->next == NULL) - { - SLTPushFront(*pphead, x); - } - else - { - SLTNode* pcur = *pphead; - while (pcur->next != pos) - { - pcur = pcur->next; - } - SLTNode* newnode = SLTBuyNode(x); - newnode->next = pos; - pcur->next = newnode; - } -} - -void SLTErase(SLTNode** pphead, SLTNode* pos) -{ - assert(pphead && pos && *pphead); - if ((*pphead)->next == NULL) - { - SLTPopFront(*pphead); - } - else - { - SLTNode* pcur = *pphead; - while (pcur->next!= pos) - { - pcur = pcur->next; - } - pcur->next = pos->next; - free(pos); - pos = NULL; - } -} - -void SLTInsertAfter(SLTNode* pos, SLTDataType x) -{ - assert(pos); - SLTNode* newnode = SLTBuyNode(x); - newnode->next = pos->next; - pos->next = newnode; -} - -void SLTEraseAfter(SLTNode* pos) -{ - assert(pos); - SLTNode* del= pos->next; - pos->next=del->next; - free(del); - del = NULL; -} - -void SListDesTroy(SLTNode** pphead) -{ - assert(pphead&&*pphead); - SLTNode* pcur = *pphead; - while (pcur) - { - SLTNode* next = pcur->next; - free(pcur); - pcur = next; - } - *pphead = NULL; -} \ No newline at end of file diff --git a/SLTNode/SLTNode/SLTNode.h b/SLTNode/SLTNode/SLTNode.h deleted file mode 100644 index d8822cc254e7061b031922f37a6a7bbc3070bf0c..0000000000000000000000000000000000000000 --- a/SLTNode/SLTNode/SLTNode.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include -#include -#include - -typedef int SLTDataType; -typedef struct SLTNodelist -{ - SLTDataType data; - struct SLTNodelist* next; -}SLTNode; - -void SLTPrint(SLTNode* p); -void SLTPushBack(SLTNode** pphead, SLTDataType x); -void SLTPushFront(SLTNode** pphead, SLTDataType x); -void SLTPopBack(SLTNode** pphead); -void SLTPopFront(SLTNode** pphead); -SLTNode* SLTBuyNode(SLTDataType x); -SLTNode* SLTFind(SLTNode* phead, SLTDataType x); -void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);//½ڵã֮ǰ²åÈëÊý¾Ý -void SLTErase(SLTNode** pphead, SLTNode* pos);//ɾ³ý½ڵã -void SLTInsertAfter(SLTNode* pos, SLTDataType x);//½ڵã֮ºó²åÈëÊý¾Ý -void SLTEraseAfter(SLTNode* pos);//ɾ³ý½ڵã֮ºóÊý¾Ý -void SListDesTroy(SLTNode** pphead); \ No newline at end of file diff --git a/SLTNode/SLTNode/test.c b/SLTNode/SLTNode/test.c deleted file mode 100644 index ba875eacd1028d6a43aeb5e3d133992c8030ab16..0000000000000000000000000000000000000000 --- a/SLTNode/SLTNode/test.c +++ /dev/null @@ -1,40 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"SLTNode.h" - -void test01() -{ - SLTNode* SLT=NULL; - SLTPushBack(&SLT, 1); - SLTPrint(SLT); - SLTPushBack(&SLT, 2); - SLTPrint(SLT); - SLTPushBack(&SLT, 3); - SLTPrint(SLT); - - SLTPushFront(&SLT, 0); - SLTPrint(SLT);//0 1 2 3 - - SLTInsert(&SLT, SLTFind(SLT,2), 3); - SLTPrint(SLT);// 0 1 3 2 3 - - SLTErase(&SLT, SLTFind(SLT, 3)); - SLTPrint(SLT); - - SListDesTroy(&SLT); - SLTPrint(SLT); - /*SLTPopFront(&SLT); - SLTPrint(SLT); - SLTPopFront(&SLT); - SLTPrint(SLT); - SLTPopFront(&SLT); - SLTPrint(SLT); - SLTPopFront(&SLT); - SLTPrint(SLT);*/ - -} - -int main() -{ - test01(); - return 0; -} \ No newline at end of file diff --git a/Stack/Stack.h b/Stack/Stack.h deleted file mode 100644 index 50d9394dba041960b0d29be19168aec2ce8eb291..0000000000000000000000000000000000000000 --- a/Stack/Stack.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include -#include -#include -#include - -typedef int STDataType; -typedef struct Stack -{ - STDataType* a; - int top; - int capacity; -}Stack; - -void STInit(Stack* ps); -void STPush(Stack* ps, STDataType x); -void STPop(Stack* ps); -STDataType STTop(Stack* ps); -int STSize(Stack* ps); -void STDestory(Stack* ps); -bool STEmpty(Stack* ps); \ No newline at end of file diff --git a/Stack/test.c b/Stack/test.c deleted file mode 100644 index 91c54cf4d74c8e07969895d89a764416a7c037f0..0000000000000000000000000000000000000000 --- a/Stack/test.c +++ /dev/null @@ -1,48 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"Stack.h" -#include"Queue.h" -void StackTest() -{ - Stack st; - STInit(&st); - STPush(&st, 1); - STPush(&st, 2); - STPush(&st, 3); - - while (!STEmpty(&st)) - { - printf("%d ", STTop(&st)); - STPop(&st); - } - STDestory(&st); -} - -void QueueTest() -{ - Queue q; - QueueInit(&q); - QueuePush(&q, 1); - QueuePush(&q, 2); - QueuePush(&q, 3); - printf("size=%d\n", QueueSize(&q)); - - while (!QueueEmpty(&q)) - { - printf("%d ",QueueFront(&q)); - QueuePop(&q); - } - - QueueDestroy(&q); - -} - -int main() -{ - -#if 0 - StackTest(); -#endif - - QueueTest(); - return 0; -} \ No newline at end of file diff --git a/seqlist/seqlist.c b/seqlist/seqlist.c deleted file mode 100644 index 6eabb61c02d42205e9db585ee2e569895fe5f69f..0000000000000000000000000000000000000000 --- a/seqlist/seqlist.c +++ /dev/null @@ -1,112 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"seqlist.h" -void SeqListInit(SL* ps) -{ - ps->arr = NULL; - ps->size = ps->capacity = 0; -} -void SeqListDesTroy(SL* ps) -{ - if (ps->arr) - { - free(ps->arr); - } - ps->arr = NULL; - ps->size = ps->capacity = 0; -} -void SeqListPrint(SL sl) -{ - for (int i = 0; i < sl.size; i++) - { - printf("%d ", sl.arr[i]); - } - printf("\n"); -} - -void CheckCapacity(SL* ps) //2±¶À©ÈÝ -{ - assert(ps); - int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; - if (ps->size == ps->capacity) - { - SLDATATYPE* tmp = (SLDATATYPE*)realloc(ps->arr, newcapacity * sizeof(SLDATATYPE)); - if (tmp == NULL) - { - perror("realloc fail!"); - exit(1); - } - ps->arr = tmp; - ps->capacity = newcapacity; - } - -} -void SLPushBack(SL* ps, SLDATATYPE x) -{ - assert(ps); - CheckCapacity(ps); - ps->arr[ps->size++] = x; -} -void SLPopBack(SL* ps) -{ - assert(ps); - assert(ps->size); - ps->size--; -} - -void SLPushFront(SL* ps, SLDATATYPE x) -{ - assert(ps); - CheckCapacity(ps); - for (int i = ps->size; i > 0; i--) - { - ps->arr[i] = ps->arr[i-1]; - } - ps->arr[0] = x; - ps->size++; -} - -void SLPopFront(SL* ps) -{ - assert(ps); - assert(ps->size); - for (int i = 0; i size-1; i++) - { - ps->arr[i] = ps->arr[i+1];//arr[size-2]=arr[size-1] - } - ps->size--; -} - -void SLInsert(SL* ps, int pos, SLDATATYPE x) -{ - assert(ps); - assert(pos >= 0 && pos < ps->size); - CheckCapacity(ps); - for (int i = ps->size; i > pos; i--) - { - ps->arr[i] = ps->arr[i - 1];//arr[pos+1]=arr[pos] - } - ps->arr[pos] = x; - ps->size++; -} - -void SLErase(SL* ps, int pos) -{ - assert(ps); - assert(pos >= 0 && pos < ps->size); - for (int i = pos; i size-1; i++) - { - ps->arr[i] = ps->arr[i+1];//arr[pos]=arr[pos+1],arr[size-2]=arr[size-1] - } - ps->size--; -} - -int SLFind(SL* ps, SLDATATYPE x) -{ - assert(ps); - for (int i = 0; i < ps->size; i++) - { - if (ps->arr[i] == x) - return i; - } - return -1; -} \ No newline at end of file diff --git a/seqlist/seqlist.h b/seqlist/seqlist.h deleted file mode 100644 index b3428ecf1231eeff20985f7ab398afb6a21de0e0..0000000000000000000000000000000000000000 --- a/seqlist/seqlist.h +++ /dev/null @@ -1,20 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include -#include -#include -typedef int SLDATATYPE; -typedef struct SeqList -{ - SLDATATYPE* arr; - int size; - int capacity; -}SL; - -void SeqListInit(SL* ps); -void SeqListDesTroy(SL* ps); -void SeqListPrint(SL sl); -void CheckCapacity(SL* ps); -void SLPushBack(SL* ps, SLDATATYPE x); -void SLPopBack(SL* ps); -void SLPushFront(SL* ps, SLDATATYPE x); -void SLPopFront(SL* ps); \ No newline at end of file diff --git a/seqlist/test.c b/seqlist/test.c deleted file mode 100644 index d9276b1afa5ca3f297bda90a2a6991146cc51014..0000000000000000000000000000000000000000 --- a/seqlist/test.c +++ /dev/null @@ -1,51 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS -#include"seqlist.h" - -void test() -{ - SL sl; - SeqListInit(&sl); - - SLPushBack(&sl, 1); - SLPushBack(&sl, 2); - SLPushBack(&sl, 3); - SLPushFront(&sl, 0); - SLPushFront(&sl, -1); - SeqListPrint(sl);//-1 0 1 2 3 - - SLPopBack(&sl); - SeqListPrint(sl);//-1 0 1 2 - /*SLPopBack(&sl); - SeqListPrint(sl); - SLPopBack(&sl); - SeqListPrint(sl); - SLPopBack(&sl); - SeqListPrint(sl); - SLPopBack(&sl); - SeqListPrint(sl); - SLPopBack(&sl); - SeqListPrint(sl);*/ - - SLPopFront(&sl); - SeqListPrint(sl); - /*SLPopFront(&sl); - SeqListPrint(sl); - SLPopFront(&sl); - SeqListPrint(sl); - SLPopFront(&sl); - SeqListPrint(sl); - SLPopFront(&sl); - SeqListPrint(sl);*/ - - SeqListDesTroy(&sl); - - -} - - -int main() -{ - - test(); - return 0; -} \ No newline at end of file