您的位置:首页 > 其它

DS(栈、栈实现算术操作、队列、循环队列、二叉树实现、4种排序)

2011-09-12 16:02 531 查看

1.栈实现

Static成员函数不能实现多态

头文件中:

#ifndef THREE_H_H

#define THREE_H_H

#include <iostream>

#include <stdexcept>

using namespace std;

#define N 10

template <typename T>

class Stack{

int top;//表示栈的最上面

T data
;

public:

Stack():top(0) {}

void pop(){if(top==0) throw "stack is empty"; top--;}

T tp(){if(top==0) throw "stack is empty"; return data[top-1];}

void push(T t);

bool empty() {return (top==0);}

bool full() {return (top==N);}

};

#endif

实现文件中:

#include "33.h"

template <typename T>

void Stack<T>::push(T t)

{if(top==N) throw "stack is full"; data[top++]=t;}

主文件中:

#include "12.cpp" //注意这句:是包含实现文件而不是头文件

int main()

{

Stack<int> s;

for (int i=0;i<N;i++)

{

s.push(i);

}

for (i=0;i<N;i++)

{

cout<<s.tp()<<' ';

s.pop();

}

cout<<endl;

}

2.栈实现算术操作

#include <iostream>

#include <string>

#include <stdexcept>

using namespace std;

class Stack{

int sz;

char data[100];

public:

Stack():sz(0){memset(data,0,100);}

void push(char t){if(full()) throw "stack full";data[sz++]=t;}

void pop(){if(empty()) throw "empty";sz--;}

char top(){ if(empty()) throw "empty";return data[sz-1];}

bool empty(){return sz==0;}

bool full(){return sz==100;}

int size(){return sz;}

};

bool prior(char c1,char c2)

{if( (c1=='*' || c1=='/' || c1=='%') && ( c2=='+' || c2=='-' ) ) return true;

return false;}

int main()

{

//string s="a+b*c-(d+e)";

string s="1+2*3/2-(4+5)*6/9";

char c=0;

Stack res;//结果栈

Stack exp;//运算符栈

for(int i=0;i<s.length();i++)

{

c=s[i];

if(c>='0' && c<='9' || c>='a'&&c<='z' || c>='A'&&c<='Z') //如果是是数字或字符的话

{res.push(c);}

if(c=='+' || c=='-' || c=='*' || c=='/' || c=='%')//如果是运算符

{

if(exp.empty()) {exp.push(c);}

else if(exp.top()=='(') {exp.push(c);}

else

{

while ( !exp.empty() && !prior(c,exp.top()) )

{

res.push(exp.top());exp.pop();

}

exp.push(c);

}

}

if(c=='(') //如果是 (

exp.push(c);

if(c==')' ) //如果是)

{

while ( exp.top()!='(' && !exp.empty())

{

res.push(exp.top());exp.pop();

}

exp.pop();

}

}

while(!res.empty()) {exp.push(res.top());res.pop();}

while(!exp.empty()) {cout<<exp.top()<<' ';exp.pop();}cout<<endl;

cout<<endl;

}

//此时输出的值就是后序表达式。a+b*c-(d+e)可以这样来求后序表达式: ( (a+(b*c))-(d+e) )先用括号

//将要求的表达式括起来,然后 将符号拉到每个相应表达式的后面。变为:( (a(bc)*)+(de)+ )-

//为 abc*+de+- ,程序总体思路:用两个栈来实现:若不是运算符直接入res栈,若是运算符,如果

//此时exp中的不比此运算符优先级高,则移到res栈中。将此运算符入exp栈,左括号和括号中的运算符直接入exp栈,

//当右 ) 时,将 ( 之前的运算符入 res 栈,丢弃 (). 最后将res中的全部移到exp中,输出exp中的内容。就是中

//序表达式的值。

此程序只是实现了简单的功能,可以在此程序基础上进行扩展。

3.队列(queue)实现

队列是一种先进先出的实现,一般左边为出的方向,右边为进的方向。循环队列中有指向头的指针来表示头位置。

#include <iostream>

#include <stdexcept>

using namespace std;

#define N 50

class Queue{

int sz;//队列长度

int data
;

int beg;//头指针

public:

Queue():sz(0),beg(0) {}

void push(int t)throw (const char *)

{if(full()) throw "full"; data[(beg+sz)%N]=t;sz++;}

void pop(){sz--;beg=(beg+1)%N;}

int top(){return data[beg];}

bool empty(){return sz==0;}

bool full(){return sz==N;}

};

int main()

{

Queue q;

for (int i=0;i<10;i++)

{

q.push(i);

}

for (i=0;i<10;i++)

{

cout<<q.top()<<' ';q.pop();

}

}

4.二叉树实现

//树实现的原则是比树小的放到根结点的左边,比根结点大的放到根结点的右边。

#include <iostream>

using namespace std;

class Tree{

struct Node

{

int data;

Node *left;

Node *right;

Node(int data):left(0),right(0),data(data) {/*cout<<"node construct"<<endl;*/}

~Node()

{

//cout<<"node deconstruct"<<endl;

}

};

Node *root;

int sz;//记录树有多少个结点

void insert(Node *p,Node * &rt)//将结点p插入到根为rt的树中 >=的数插入到右边

{

if(rt==0) {rt=p;return;}

if(p->data>=rt->data) insert(p,rt->right);

if(p->data<rt->data) insert(p,rt->left);

}

void clear(Node *&rt)//清除根为rt的树

{

if(rt==0) return;

clear(rt->left);

clear(rt->right);

delete rt;

}

Node * find(int data,Node * &rt)//在根结点是rt的树中找到返回数据data的指针,没找到返回bool

{

if(rt==0) return NULL;

if(data==rt->data) return rt;

if(data>=rt->data) return find(data,rt->right);

return find(data,rt->left);

}

bool remove(int data,Node *&rt)//在根结点是rt的树中删除data结点

{

Node *pp=find(data,rt);

if(pp==NULL) return false;

if(rt==pp) {rt=rt->right;}

insert(pp->left,pp->right);

Node *q=pp;

pp=pp->right;

delete q;

return true;

}

void travel(Node *rt)//遍历树

{

if(rt==0) return;

travel(rt->left);

cout<<rt->data<<' ';

travel(rt->right);

}

public:

Tree():sz(0),root(0) {}

~Tree(){clear();}

int getsize(){return sz;}

void insert(const int &data)//向树中插入数据

{

sz++;

Node *pnew=new Node(data);

//cout<<"in insert"<<pnew<<endl;

insert(pnew,root);

}

void clear()//清除树

{

clear(root);

sz=0;

}

bool remove(int data)//删除数据是data的结点,如果成功删除,返回true,失败返回false;

{

sz--;

//Node *p=new Node(data);

return remove(data,root);

}

void travel()//遍历树

{

travel(root);cout<<endl;

}

};

int main()

{

Tree t;

t.insert(89);

t.insert(49);

t.insert(119);

cout<<t.getsize()<<endl;

t.remove(89);

t.travel();

cout<<t.getsize()<<endl;

return 0;

}

//在对树进行插入,删除等操作时,要以结点为基本单位,这样写程序时好写,代码可读性高。

5.四种排序算法

#include <iostream>

#include <ctime>

using namespace std;

//冒泡排序(升序排序),两两比较,大数移向后

void bubblesort(int a[],int n)

{

for (int i=0;i<n-1;i++)

{

for (int j=0;j<n-1-i;j++)

{

if(a[j]>a[j+1]) swap(a[j],a[j+1]);//升序排序用> 降序排序用<

}

}

}

//选择排序 每次记最小的数下标,然后交换

void selectsort(int a[],int n)

{

for (int i=0;i<n-1;i++)

{

int k=i;

for (int j=i+1;j<n;j++)

{

if(a[k]>a[j]) k=j; //升序排序用> 降序排序用<

}

if(k!=i) swap(a[i],a[k]);

}

}

//插入法排序 形式如打牌时的插入

void insertsort(int a[],int n)

{

for (int i=1;i<n;i++)

{

int j=i-1;

int k=a[i];

while (j>=0 && a[j]>k) //升序排序用> 降序排序用<

{

a[j+1]=a[j];

j--;

}

a[j+1]=k;

}

}

//快速排序:

void quicksort(int a[],int n)

{

if(n<=1) return;

int L=0;

int R=n-1;

while (L<R)

{

while(L<R && a[L]<=a[R]) L++; //升序排序用> 降序排序用<

swap(a[L],a[R]);

while(L<R && a[L]<=a[R]) R--; //升序排序用> 降序排序用<

swap(a[L],a[R]);

}

quicksort(a,L);

quicksort(a+L+1,n-L-1);

}

void pt(int a[],int n)

{

for (int i=0;i<n;i++)

{

cout<<a[i]<<' ';

}

cout<<endl;

}

int main()

{

#define N 25000

srand(time(0));

int a[13]={33,45,11,6,89,67,56,49,87,55,3,12,11};

pt(a,13);

int b
;

for (int i=0;i<N;i++)

{

b[i]=rand()%1000;

}

clock_t start=clock();

//bubblesort(b,N); // 用时10 秒

//selectsort(b,N);//用时1 秒

//insertsort(b,N);//用时1 秒

quicksort(a,13);//用时非常短,以至于都无法计算

clock_t end=clock();

cout<<(end-start)/CLOCKS_PER_SEC<<endl;

// bubblesort(a,13);

// pt(a,13);

// selectsort(a,13);

// pt(a,13);

// insertsort(a,13);

// pt(a,13);

// quicksort(a,13);

// pt(a,13);

return 0;

}

查找字符串中某个字符:

#include <iostream>

#include <ctime>

using namespace std;

int search(char *p,int start,int end,char c)//在字符串p中找字符c,并返回它的下标,如果没有找到,返回-1,下标范围为start---end.

{

if(start>end) return -1;

int m=(end+start)/2;

if(p[m]==c) return m;

if(c>p[m])

{return search(p,m+1,end,c);}

else

{return search(p,start,m-1,c);}

}

int main()

{

char s[]="abcdefghijk";

cout<<search(s,0,13,'j');

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: