随笔分类 -  数据结构与算法

摘要:问题:字符串结尾记得加'\0';发现自己代码写的真烂,逻辑太简单了。。。代码:#include <iostream>using namespace std;int main(){ char *s1="hello"; char *s2="world"; char *c; char *c1; int len1,len2; len1=len2=0; c=s1; while(c[len1]!='\0') len1++; c=s2; while(c[len2]!='\0') len2++; c1=(cha 阅读全文
posted @ 2013-04-26 14:52 xshang 阅读(236) 评论(0) 推荐(0) 编辑
摘要:问题:一定要记得保存指针的地址。代码:#include <iostream>using namespace std;int main(){ char s1[]="helloworld"; char s2[]="hellochina"; char *c1=s1; char *c2=s2; char *c3; int length1=0,length2=0; while(s1[length1]!='\0') length1++; while(c2[length2]!='\0') length2++; if(leng 阅读全文
posted @ 2013-04-26 14:26 xshang 阅读(182) 评论(0) 推荐(0) 编辑
摘要:问题:STL学得真不过关啊 ,用到时就像不起来,while(cin.get(c))在结束时,先换行,即按下回车键,在按ctl+z键,代码是看了别人之后,自己重写的。STL还是要好好学,真的很有用,可以吧复杂的问题变得简单。代码:#include <iostream>#include <vector>using namespace std;int main(){ vector<char> s; char c; int beg,len; while(cin.get(c)) s.push_back(c); cout<<"please inpu 阅读全文
posted @ 2013-04-26 10:53 xshang 阅读(195) 评论(0) 推荐(0) 编辑
摘要:问题:主要是队列为空和满的条件. 为空:cqueue->front==cqueue->rear 为满(cqueue->rear+1)==cqueue->front代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXSIZE 20typedef struct CQueue{ int front; int rear; int data[MAXSIZE];}*CirQueue;void initCQueue(CirQueue &cqueue){ cqueu 阅读全文
posted @ 2013-04-21 15:38 xshang 阅读(226) 评论(0) 推荐(0) 编辑
摘要:问题:今天累了,没心情写,但我知道路漫漫,还是要坚持,要进步。顺序队列比较简单,明白原理就行。注:判断队列为空的条件:squeue->front==squeue->rear;代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXSIZE 20typedef struct SQueue{ int data[MAXSIZE]; int front; int rear;}*SeqQueue;void initSeqQueue(SeqQueue &squeue) //初始 阅读全文
posted @ 2013-04-20 20:30 xshang 阅读(323) 评论(0) 推荐(0) 编辑
摘要:问题:建立顺序栈还是比较简单的。主要是一开始在入栈操作中每次调用初始化栈函数,结构出错。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXSIZE 20typedef struct SeqStack{ int stack [MAXSIZE]; int top;}*stackNode;void initStack(stackNode &stack){ stack=(stackNode)malloc(sizeof(struct SeqStack)); if(!stack) { 阅读全文
posted @ 2013-04-19 15:36 xshang 阅读(763) 评论(0) 推荐(0) 编辑
摘要:问题:我用的是尾插入法,主要是在输出的时候循环条件不要用错了^^,昨天查资料时遇见了一批“外星人”,今天看循环链表时又遇见“外星人”了**!贴代码:#include <iostream>#include <cstdlib>using namespace std;typedef struct CircleList{ int data; struct CircleList *next;}*CList;void createCList(CList &clist){ CList head,node; int c; clist=(CList)malloc(sizeof(s 阅读全文
posted @ 2013-04-18 16:40 xshang 阅读(164) 评论(0) 推荐(0) 编辑
摘要:问题:在分配空间时,遇到问题定义一个结构体:typedef struct dLinkListNode{int data;struct dLinkListNode *prior;struct dLinkListNode *next;}*dLinkList,dListNode;dList=(dLinkList)malloc(sizeof(dListNode));与dList=(dLinkList)malloc(sizeof(dLinkList));是不一样的,即指针和结构体的大小不一样,不要想当然。指针的问题真的小心,一不小心就出错。代码:#include <iostream>#inc 阅读全文
posted @ 2013-04-17 16:10 xshang 阅读(177) 评论(0) 推荐(0) 编辑
摘要:遇到的问题:主要还是指针问题,还有算法思想要清晰。在删除时对于相同的数只能删除一次。不知什么原因。在GCC上运行却可以删除相同的数多次。 代码:#include <iostream>#include <cstdlib>using namespace std;typedef struct ListNode{ int data; struct ListNode *next;}*List;void createList(List &list,int arr[],int n){ List head,node; list=(List)malloc(sizeof(List) 阅读全文
posted @ 2013-04-15 17:38 xshang 阅读(161) 评论(0) 推荐(0) 编辑
摘要:遇到的问题 malloc、realloc的用法realloc:(类型 *)realloc(原来的内存地址,新的大小(*类型)); 指针的问题:要深刻理解指针,指针也是一个变量,在函数传递参数的过程中,作为参数来讲,传递的也是值。这个值就是指针本身的内容,即指针指向的地址。而不是传的指针。所以指针作为函数形参是一定要注意!代码:#include <iostream>#include <cstdlib>using namespace std;int Maxsize=10;typedef struct SeqListNode{ int data; int len;}*SeqL 阅读全文
posted @ 2013-04-13 16:54 xshang 阅读(406) 评论(0) 推荐(0) 编辑
摘要:顺序表的概念:顺序表就是用连续的存储空间来存储数据。顺序表的优点:方便查询,当查询时,直接用下标就可以,时间复杂度O(1);遇到的问题:srand函数中time()方法要加上头文件#include <time.h> ,使用srand()时,rand()函数生成的随机数会不同,srand()在for循环的外面。代码:#include <iostream>#include <cstdlib>#include <time.h>using namespace std;struct Snode{ int i; int data;}Slist[50];int 阅读全文
posted @ 2013-04-12 10:15 xshang 阅读(676) 评论(0) 推荐(1) 编辑
摘要:from: http://www.cnblogs.com/nokiaguy/archive/2013/01/29/2881476.html有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。 原文用java写的,没看太懂,但很感谢博主的算法思路,即找正负数的临界值。于是我用C++实现了一遍,代码如下: 1 #include <iostream> 2 #include <cmath> 阅读全文
posted @ 2013-01-29 16:07 xshang 阅读(257) 评论(0) 推荐(0) 编辑
摘要:从C++奋斗乐园上看到的题目,也想试着编程来学习。网址:http://www.cppleyuan.com/viewthread.php?tid=8055&extra=page%3D1 首先,说一下自己的体会,这题有不同的解决方法,让我更好的对C++字符串的理解,以及指针的了解和内存分配。 下面是源码,已通过经过测试: #include <iostream> #include <string> #include <cstdlib> using namespace std; char * convert(char *strDest,char *strSt 阅读全文
posted @ 2013-01-20 17:36 xshang 阅读(1182) 评论(0) 推荐(0) 编辑
摘要:之前看了许多关于递归的理解,还是是懂非懂的,这个问题一直纠结在心里。 今天又碰到这个递归问题了,我认为一定要把问题分析清楚了,以后再遇到这样的问题或者类似问题才能轻车熟路,不然又要头疼或者成为问题的瓶颈了。1)讲到递归,我觉得先从函数说起,递归首先是一个函数,具有函数的一切功能,即写一个递归要有函数的形式。比如 void function()。2) 递归的定义,即递推和回归,即把一个大问题分成有限的小问题,并且通过这些小问题的解决,最后把大问题可以解决。3)递归函数的格式,重要的是有个出口,即一个递归结束的条件,比如 if(btree->data=='#'),最后有个re 阅读全文
posted @ 2013-01-17 15:37 xshang 阅读(268) 评论(0) 推荐(0) 编辑