Skip to content

Commit

Permalink
合并原始代码主干
Browse files Browse the repository at this point in the history
合并原始代码主干
  • Loading branch information
bkcarlos committed Oct 15, 2018
1 parent 751da89 commit bc3b4e3
Show file tree
Hide file tree
Showing 68 changed files with 3,807 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ hs_err_pid*

# editor files
.vscode
.*.swp
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 数据结构和算法之美
# 请点击查看:[https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)
# [https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)

# Java rate limiting library/framework
# https://github.com/wangzheng0822/ratelimiter4j
220 changes: 220 additions & 0 deletions c-cpp/06_linkedlist/Dlist/Dlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*************************************************************************
> File Name: Dlist.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-10-07
> Desc:
************************************************************************/
#include<stdio.h>
#include <stdbool.h>
#include "./Dlist.h"



void dlist_init(stDlistHead *dlist)
{
dlist->size = 0;
dlist->head = NULL;
dlist->tail = NULL;
return;
}

void dlist_destory(stDlistHead *dlist)
{
stDlistNode *pNode = NULL;

while(dlist->size > 0)
{
pNode = dlist->head;
dlist->head = dlist->head->next;
free(pNode);
dlist->size--;
}

memset(dlist,0,sizeof(stDlistHead));

return;
}

int dlist_insert_head(stDlistHead *dlist,stDlistNode *pNode,int data)
{
if(pNode == NULL)
{
pNode = (stDlistNode *)malloc(sizeof(stDlistNode));
if (pNode == NULL)
{
return -1;
}
}

pNode->data = data;
pNode->prev = NULL;
pNode->next = NULL;

if (dlist->size == 0)
{
dlist->head = pNode;
dlist->tail = pNode;
}
else
{
pNode->next = dlist->head;
dlist->head->prev = pNode;
dlist->head = pNode;
}

dlist->size++;
return 0;
}

stDlistNode * dlist_remove_tail(stDlistHead *dlist)
{
stDlistNode *pNode = NULL;

if(dlist->size == 0)
{
return NULL;
}

pNode = dlist->tail;
if(dlist->size > 1)
{
dlist->tail = dlist->tail->prev;
dlist->tail->next = NULL;
}
else
{
dlist->head = NULL;
dlist->tail = NULL;
}
dlist->size--;
return pNode;
}

void dlist_remove_node(stDlistHead * dlist,stDlistNode *pNode)
{
if ((dlist == NULL)||(pNode == NULL))
{
return;
}

if (dlist->head == pNode)
{
dlist->head = dlist->head->next;
}
else if (dlist->tail == pNode)
{
dlist->tail = pNode->prev;

dlist->tail->next = NULL;
}
else
{
pNode->prev->next = pNode->next;
pNode->next->prev = pNode->prev;
}
dlist->size--;
pNode->prev = NULL;
pNode->next = NULL;

if (dlist->size == 0)
{
memset(dlist,0,sizeof(stDlistHead));
}

return;
}
stDlistNode * dlist_search(stDlistHead * dlist,int data)
{
stDlistNode *pNode = dlist->head;
while(pNode != NULL)
{
if (pNode->data == data)
{
return pNode;
}
pNode = pNode->next;

}
return NULL;
}

void dlist_dump(stDlistHead *dlist)
{
int no = 0;
stDlistNode *pNode = dlist->head;
while(pNode != NULL)
{
printf("\r\n [%d] = %d",no++,pNode->data);
pNode = pNode->next;
}

return;
}


void Lru_dlist(stDlistHead *dlist,int data)
{
stDlistNode *pNode = NULL;

pNode = dlist_search(dlist,data);
if (pNode != NULL)
{
dlist_remove_node(dlist,pNode);
}
else if(dlist->size >= 4)
{
pNode = dlist_remove_tail(dlist);

}

dlist_insert_head(dlist ,pNode,data);

return;
}

int main()
{
stDlistHead dlist = {0};
stDlistNode * pNode = NULL;

dlist_init(&dlist);

printf("\r\n inset 1,2,3");
dlist_insert_head(&dlist,NULL,1);
dlist_insert_head(&dlist,NULL,2);
dlist_insert_head(&dlist,NULL,3);

dlist_dump(&dlist);

pNode = dlist_remove_tail(&dlist);
if(pNode != NULL)
{
printf("\r\n remove %d",pNode->data);
}
dlist_insert_head(&dlist,pNode,4);
dlist_dump(&dlist);

Lru_dlist(&dlist,5);
dlist_dump(&dlist);
Lru_dlist(&dlist,6);
dlist_dump(&dlist);
Lru_dlist(&dlist,7);
dlist_dump(&dlist);
Lru_dlist(&dlist,5);
dlist_dump(&dlist);



while(dlist.size > 0)
{
pNode = dlist_remove_tail(&dlist);
if(pNode != NULL)
{
printf("\r\n remove %d",pNode->data);
free (pNode);
}
}

return 0;
}
23 changes: 23 additions & 0 deletions c-cpp/06_linkedlist/Dlist/Dlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*************************************************************************
> File Name: Dlist.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-10-07
> Desc:
************************************************************************/
#include<stdio.h>

typedef struct DlistNode
{
struct DlistNode *prev;
struct DlistNode *next;
int data;
}stDlistNode;

typedef struct Dlisthead
{
int size;
stDlistNode *head;
stDlistNode *tail;
}stDlistHead;

122 changes: 122 additions & 0 deletions c-cpp/08_stack/StackBasedOnArray/StackBasedOnArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* 1)顺序栈的操作:入栈和出栈;
* 2)采用模板的方法实现存储任意类型的数据
* 3)采用数组的栈,支持动态扩容,每次扩容1.5 倍,初始栈的大小是 10 。
*
* Author:caozx
* time ;2018年10月11日
*/

#include <iostream>
#include "StackBasedOnArray.h"
using namespace std;

//构造函数,创建栈
//类模板成员函数的写法 template<class T> 返回值类型 类名<T>::成员函数名(参数列表){}
template<class T> ArrayStack<T>::ArrayStack()
{
this -> count = 10;
this -> flag = 0;
this -> array = new T[this -> count];
if (! this -> array){
cout << "array malloc memory failure" << endl;
}
}


//有参构造函数,创建栈
template<class T> ArrayStack<T>::ArrayStack(int count)
{
this -> count = count;
this -> flag = 0;
this -> array = new T[this -> count];
if (! this -> array){
cout << "array malloc memory failure" << endl;
}
}

//析构函数,销毁栈
template <class T> ArrayStack<T>::~ArrayStack(){
this -> count = 0;
this -> flag = 0;
if(this -> array){
delete [] this -> array;
this -> array = NULL;
}

}

// 入栈
template<class T> void ArrayStack<T>::push(T data){
if(this -> flag == this -> count){
cout << "The stack is full , so need to enlarge 1.5x! "<< endl;
this -> count = int (1.5 * this -> count);
T * temp = new T [this -> count];
for(int i = 0; i < this -> flag ; i++){
temp[i] = this -> array[i];
//cout << temp[i] <<endl;
}
delete [] this -> array; //释放原来的空间
temp[this -> flag] = data;
this -> flag ++;
this -> array = temp;
}
else{
this -> array [this -> flag] = data;
this -> flag ++ ;
}
}

//出栈,并删除栈顶元素
template<class T> T ArrayStack<T>::pop(){
this -> flag --;
T temp = this -> array[this -> flag];
return temp;
}

//出栈,不删除栈顶元素
template<class T> T ArrayStack<T>::peek(){
T temp = this -> array[this -> flag - 1];
return temp;
}

template<class T> int ArrayStack<T>::stackSize(){
return this -> flag;
}

template<class T> int ArrayStack<T>::stackMaxSize(){
return this -> count;
}

int main(int argc, char const *argv[])
{
cout << " === test begin ===" << endl;
ArrayStack <int> arrstack(12);
arrstack.push(10);
arrstack.push(20);
arrstack.push(30);
arrstack.push(40);
arrstack.push(50);
arrstack.push(60);
arrstack.push(70);
arrstack.push(80);
arrstack.push(90);
arrstack.push(100);
arrstack.push(110);
arrstack.push(120);
arrstack.push(130);
arrstack.push(140);
arrstack.push(150);

cout << "peek , not delete " << arrstack.peek() << endl;
cout << "pop , delete " << arrstack.pop()<<endl;

arrstack.push(210);
arrstack.push(220);

cout << "peek , not delete " << arrstack.peek() << endl;
cout << "pop , delete " << arrstack.pop()<<endl;

system("pause");
return 0;
}
Loading

0 comments on commit bc3b4e3

Please sign in to comment.