数据结构算法源码
① 数据结构与算法演示系统完整简单的c++源代码,包哈内容有:线性表、二叉树、图、排序,急用!谢谢!
我有表和二叉树的,图和排序没有!那玩意不能简单的,一个就得100多行!而且只是。h文件。先给你2个吧!
#ifndef sqlIST_H
#define SQLIST_H
using namespace std;
template<class elemtype>
class sqlist
{
public:
sqlist(){head=NULL; n=0;}
virtual ~sqlist(){head=NULL;}
bool insert(const elemtype& ,int);
elemtype find(int );
void clear();
int length()const {return n;}
bool Delete(int );
protected:
int n;
struct node{
node *next;
elemtype data;
};
node *head;
};
template<class elemtype>
void sqlist<elemtype>::clear()
{
node *p,*q;
int i=1;
p=head;
while(i!=n)
{
q=p;
delete q;
p=p->next;
i++;
}
}
template<class elemtype>
bool sqlist<elemtype>::insert(const elemtype &e,int i)
{
node *p,*q;
int j=1;
if(head==NULL)
head=new node;
p=head;
if(i==1&&i>n)//表头赋值
{head->data=e;
n++;
return true;}
else if(i==1&&i<=n)//表头插入
{
head=new node;
head->data=e;
head->next=p;
n++;
return true;
}
else if(i<=n) //表中插入
{
q=new node;
q->data=e;
while(j<i-1){
p=p->next;
j++;
}
q->next=p->next;
p->next=q;
n++;
return true;}
else if(i==n+1&&i!=1)//表尾插入
{
q=new node;
q->data=e;
while (j<n)
{
p=p->next;
j++;
}
p->next=q;
q->next=NULL;
n++;
return true;}
else
return false;
}
template<class elemtype>
elemtype sqlist<elemtype>::find(int i)
{
int j=1;
node *p;
p=head;
while(j<i){
p=p->next;
j++;
}
return p->data;
}
template<class elemtype>
bool sqlist<elemtype>::Delete(int i)
{
int j=1;
node *p,*q;
p=head;
if(i!=1)//表中及表尾删除
{
while(j<i)
{
q=p;
p=p->next;
j++;
}
q->next=p->next;
delete p;
n--;
return true;}
else//表头删除
head=p->next;
delete p;
n--;
return true;
}
#endif // SQLIST_H
#ifndef binarytree_H
#define binarytree_H
#include"stack.h"
#include"queue.h"
using namespace std;
template<class elemtype>
class binarytree{
public:
binarytree(){root=NULL;}
~binarytree(){}
int height(){return height(root);}
int size(){return size(root);}
void createtree(elemtype flag);
void preorder() const ;
void midorder() const ;
void postorder() const ;
bool isempty(){return root==NULL;}
void clear(){if (root!=NULL) clear(root);}
void delleft(){ clear(root->left);}
int height()const{return height(root );}
void delright(){return clear(root->right);}
private:
struct node{
node *left,*right;
elemtype data;
node():left(NULL),right(NULL){}
node(elemtype it,node*l=NULL,node*r=NULL):data(it),left(l),right(r){}
};
node *root;
struct stnode{
node *t;
int step;
stnode(node *tc=NULL):t(tc),step(0){}
};
void clear(node *);
int size(node* );
int height(node*);
};
template<class elemtype>
void binarytree<elemtype>:: clear(node *t)
{
queue<node> la;
node *tem,*q,*p;
la.append(t);
while(!la.empty()){
tem=la.putelem();
if(tem->left!=NULL)
{ p=t->left;
la.append(p);}
if(tem->right!=NULL)
{ q=tem->right;
la.append(q);}
delete tem;
}
}
template<class elemtype>
void binarytree<elemtype>::createtree(elemtype flag)
{
queue<node*> la;
node *tem;
elemtype x,ldata,rdata;
cout<<"根节点:";
cin>>x;
root=new node(x);
la.append(root);
while(!la.isempty()){
tem=la.putelem();
cout<<"输入"<<tem->data<<"的两节点值:";
cin>>ldata>>rdata;
if(ldata!=flag) la.append(tem->left=new node(ldata));
if(rdata!=flag) la.append(tem->right=new node(rdata));
}
}
template<class elemtype>
void binarytree<elemtype>::preorder( )const
{
stack<node*> la;
node *tem;
cout<<"前序遍历为:";
la.push(root);
while(!la.empty()){
tem=la.pop();
cout<<tem->data<<" ";
if(tem->right!=NULL)
la.push(tem->right);
if(tem->left!=NULL)
la.push(tem->left);
}
}
template<class elemtype>
void binarytree<elemtype>::midorder() const
{
stack<stnode> la;
stnode tem(root);
cout<<"中序遍历:";
la.push(tem);
while(!la.empty()){
tem=la.pop();
if(++tem.step==2){
cout<<tem.t->data<<" ";
if(tem.t->right!=NULL)
la.push(stnode(tem.t->right));}
else{
la.push(tem);
if(tem.t->left!=NULL)
la.push(stnode(tem.t->left));
}
}
}
template<class elemtype>
void binarytree<elemtype>::postorder() const
{
stack<stnode> la;
stnode tem(root);
cout<<"后序遍历:";
la.push(tem);
while(!la.empty()){
tem=la.pop();
if(++tem.step==3)
{cout<<tem.t->data<<" ";continue;}
la.push(tem);
if(tem.step==1)
{
if(tem.t->left!=NULL)
la.push(stnode(tem.t->left));
}
else{
if(tem.t->right!=NULL)
la.push(stnode(tem.t->right));
}
}
}
template<class elemtype>
int binarytree<elemtype>::size(node *t)
{
queue<node*> la;
node *tem;
int a=0;
la.append(t);
while(!la.isempty()){
tem=la.putelem();
a++;
if(tem->left!=NULL)
la.append(tem->left);
if(tem->right!=NULL)
la.append(tem->right);
}
return a;
}
template<class elemtype>
int binarytree<elemtype>::height(node* t)const
{
queue<node*> la;
atack<node*> lb;
node *tmp;
la.append(t);
while(!la.isempty())
{
tmp=la.putelem();
if(tmp->left)
}
/*if(t==NULL) return 0;
else
{
int l=height(t->left),r=height(t->right);
return 1+((l>r)?l:r);
}*/
}
}
你还不如用stl!而且你要这玩意干什么?c++书里都有的,实在不行自己抄书不也行吗?c++的表分单链表,循环单链表,循环双链表(我给的只是单链表)。二叉树我自己用的非递归实现,除了求高度。但要用到栈和队列,你自己写吧,太多了!发着麻烦。
② 《数据结构》算法实现与分析高一凡中的源代码要怎么用
这个代码可以直接用。用的时候必须把include中的文件也保存好。
③ c语言数据结构(考题,测试你的能力)--编写源代码
考考自己吧
人不能总能依靠别人.
④ 数据结构问题,希望有完整源代码
先看题目,顺序表L的数据元素是整数且非递增有序,也就是有序的。
//1,2,3,3,4,5,8,8,.:非递减排列
//9,8,7,7,6,5,5,2,1,.:非递增排列
我们只需要使用两个标记,一个i,一个len,
然后线性扫描一遍线性表L,判断相邻两个数是否相等,相等len就不变,不想等len就+1.并且更新数组的值。最后注意边界条件即可。
代码实现如下,如果喜欢cout可以自行修改。线性表直接使用的数组表示。
#include<iostream>
#include<cstdio>
usingnamespacestd;
constintMAXN=256;
intmain(){
intL[MAXN],n,i,j,len,t;
scanf("%d",&n);
//根据键盘输入数据建立顺序表L
for(i=0;i<n;++i){
scanf("%d",&L[i]);
}
//输出顺序表
for(i=0;i<n;++i){
printf("%d",L[i]);
}
printf(" ");
//以O(n)的时间复杂度完成对值相同多余元素的删除
for(i=1,len=0;i<n;++i){
if(L[i]!=L[i-1]){
L[len]=L[i-1];
++len;
if(i==n-1){//注意边界条件
L[len]=L[i];
++len;
}
}
if(i==n-1&&L[i]==L[i-1]){//注意边界条件
L[len]=L[i];
++len;
}
}
//输出删除值相同多余元素后的顺序L
for(i=0;i<len;++i){
printf("%d",L[i]);
}
printf(" ");
//逆置顺序表L
for(i=0,j=len-1;i<=j;++i,--j){
t=L[i];
L[i]=L[j];
L[j]=t;
}
//输出逆置后的顺序表L
for(i=0;i<len;++i){
printf("%d",L[i]);
}
printf(" ");
return0;
}
//justdoit
⑤ 怎么学习数据结构算法效果比较好,需要每个程序都编写吗
1. 程序 = 数据结构 + 算法
2. 学习:刚开始看时肯定会有些不清楚,因为你是刚学完 C 语言,对 C 语言还不太熟练。你学习数据结构时找一本经典的数据结构书,看完一个数据结构后用 C 语言将其实现。开始时的实现肯定会有困难,那么请 google 下会有很多优秀的数据结构源码的。你可以模仿这些优秀的源码写。请记住一定要开始时自己实现,当被卡住了就看一下源码,看看自己被卡在了什么地方,引起注意以便下次自己会写。当你把书上的数据结构源码写了一遍之后,你已经超过了你绝大部分的同学。
3. 运用: 这时你就需要对这些数据结构加以运用,你可以在 google 上搜索“某个数据结构 + ACM”,你就会看到一些题目,这些题目都是数据结构的运用,甚至有这些数据结构的变形。每种数据结构做5题左右。期间你还会遇到程序另一重要的方面算法,有不会的就 google。 期间可以学到的数据结构和算法做小软件玩儿,例如压缩软件,五子棋之类的。
4. 深入: 当你完成了第三步你已经是你们学校的小高手了。这时看你的方向如果这时发现自己喜欢 ACM 的话就去搞 ACM,如果不感兴趣,就找自己感兴趣的技术学习一下,做几个完整的项目,例如写个编译器,或者实现一个简单的编程语言。
总结:无论选择哪条道路只要按照这些做了,你以后肯定会成为抢手货。
⑥ 求数据结构与算法实验 张铭 的完整源代码
不知道你要的是不是这个。
http://book.knowsky.com/book_6125.htm
⑦ 数据结构各种算法或源代码从哪找
可以去书店找一下,会有里面是数据机构整个程序实现的参考书的