數據結構演算法源碼
① 數據結構與演算法演示系統完整簡單的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
⑦ 數據結構各種演算法或源代碼從哪找
可以去書店找一下,會有裡面是數據機構整個程序實現的參考書的