導航:首頁 > 源碼編譯 > 數據結構與演算法實現代碼

數據結構與演算法實現代碼

發布時間:2022-06-19 14:45:34

㈠ 求數據結構演算法平衡二叉樹實現代碼

抄的,你能看懂就行。平衡二叉樹實現代碼
#include <stdio.h>
typedef struct bitreetype
{
int item;
int bdegree;/*平衡因子,左子樹深度-右子樹深度*/
struct bitreetype *lchild;
struct bitreetype *rchild;
}bitree;
typedef struct treequeuetype
{
int head;
int tail;
bitree *items[1000];
}treequeue;/*定義一個隊列,後面的平衡調整要用層序遍歷,於是要用這個隊列*/
void resetqueue(treequeue *queue)
{
queue->head=-1;
queue->tail=-1;
return;
}/*把隊列清空*/
void inqueue(treequeue *queue,bitree *element)
{
queue->tail++;
queue->items[queue->tail]=element;
}/*入隊列*/
bitree *outqueue(treequeue *queue)
{
queue->head++;
return queue->items[queue->head];
}/*出隊列*/
int isqueueempty(treequeue *queue)
{
if(queue->head==queue->tail)
return 1;
else
return 0;
}/*判斷隊列是否為空*/
void fillmemory(char *source,int len,char content)
{
while(len)
{
source=source+len;
*source=content;
source=source-len;
len--;
}
*source=0;
}/*用CONTENT的內容去FILL以SOURCE為首,LEN長度的一塊空間,初始化內存方便*/
int getnums(int *dst)/*輸入字元串並把字元串轉化為一串數存入DST指向的內存中去,我們用它採集原始數據*/
{
char *temp,*num,*p,t;
int len=0;
temp=(char *)malloc(1000*sizeof(char));
num=(char *)malloc(20*sizeof(char));
p=num;
fillmemory(temp,1000,0);
fillmemory(num,20,0);
scanf(\"%s\",temp);
t=*temp;
temp++;
while(t)
{
if(t!=\』,\』)
{
*num=t;
num++;
t=*temp;
temp++;
}/*抽出一個數放入NUM臨時空間中*/
else
{
num=p;
*dst=atoi(num);
len++;
fillmemory(num,20,0);
dst++;
t=*temp;
temp++;
}/*將NUM中的數字轉化出來存入DST中*/
}
num=p;
*dst=atoi(num);
len++;
fillmemory(num,20,0);
dst++;
t=*temp;
temp++;
return len;
}/*處理最後一個數字*/

㈡ 浙大遠程教育數據結構與演算法13秋實驗代碼

我用python和c++寫的,如果你不想要python的,請告知。

#表達式轉換
defmain():
text=raw_input()
root=build(text)
litx=[]
travel(root,litx)
rslt=litx[0]
foriinrange(1,len(litx)):
rslt=rslt+''+litx[i]
printrslt

defbuild(text):
left=readnode(text);
ifleft['text']=='':
returnleft['node']
text=left['text']
right=readnode(text[1:])
root={}
root['value']=text[0]
root['left']=left['node']
root['right']=right['node']
text=right['text']

whiletext!='':
left=root
right=readnode(text[1:])
root={}
root['value']=text[0]
root['right']=right['node']
ifsignlevel(left['value'])>=signlevel(text[0]):
root['left']=left
else:
root['left']=left['right']
left['right']=root
root=left
text=right['text']

returnroot

deftravel(root,litx):
ifroot['left']!={}:
travel(root['left'],litx)
ifroot['right']!={}:
travel(root['right'],litx)
litx.append(root['value'])

defreadnode(text):
#print'---------------------------readnode'
#printtext
node={}
iftext[0]=='(':
index=readbrace(text)
node=build(text[1:index])
else:
index=readnumber(text)
iftext[0]=='+':
node['value']=text[1:index+1]
else:
node['value']=text[0:index+1]
node['left']={}
node['right']={}
#printnode
#print'++++++++++++++++++++++++++readnode'
return{'node':node,'text':text[index+1:]}

defreadnumber(text):
index=0
foriinrange(1,len(text)):
iftext[i]in['+','-','*','/']:
break
index=i
returnindex

defreadbrace(text):
count=1
i=0
whilecount>0:
i=i+1
iftext[i]=='(':
count=count+1;
iftext[i]==')':
count=count-1;
returni

defsignlevel(sign):
ifsignin['+','-']:
return1
else:
return2

if__name__=="__main__":
main()

#家譜處理
defmain():
text=raw_input()
n=int(text[0:text.find('')])
m=int(text[text.find('')+1:])

root=build(n)
check(root,m)

defbuild(n):
raw_name=raw_input()
name=raw_name.strip()
root={}
root['name']=name
root['layer']=0
root['child']={}
root['sibling']={}

pre_node=root
foriinrange(1,n):
raw_name=raw_input()
name=raw_name.strip()
cur_layer=raw_name.index(name)/2
cur_node={}
cur_node['name']=name
cur_node['layer']=cur_layer
cur_node['child']={}
cur_node['sibling']={}
ifcur_layer==pre_node['layer']:
pre_node['sibling']=cur_node
elifcur_layer==pre_node['layer']+1:
pre_node['child']=cur_node
elifcur_layer==pre_node['layer']+1:
print'error! '
break
elifcur_layer<pre_node['layer']:
path=getpath(root,pre_node['name'])
pre_subling={}
forpinpath:
ifp['layer']>cur_layer:
break
else:
pre_subling=p
pre_subling['sibling']=cur_node
pre_node=cur_node

returnroot

defcheck(root,m):
#XisachildofY
#XistheparentofY
#XisasiblingofY
#XisadescendantofY
#XisanancestorofY
foriinrange(0,m):
text=raw_input()
X=text[0:text.find('')]
Y=text[text.rfind('')+1:]
if'child'intext:
path=getpath(root,X)
iflen(path)>=2andpath[-2]['name']==Y:
printTrue
else:
printFalse
if'parent'intext:
path=getpath(root,Y)
iflen(path)>=2andpath[-2]['name']==X:
printTrue
else:
printFalse
if'sibling'intext:
pathX=getpath(root,X)
pathY=getpath(root,Y)
iflen(pathX)>=2andlen(pathY)>=2andpathX[-2]['name']==pathY[-2]['name']:
printTrue
else:
printFalse
if'descendant'intext:
path=getpath(root,X)
forpinpath:
ifp['name']==Y:
break
ifp['name']==Y:
printTrue
else:
printFalse
if'ancestor'intext:
path=getpath(root,Y)
forpinpath:
ifp['name']==X:
break
ifp['name']==X:
printTrue
else:
printFalse

defgetpath(root,node_name):
path=[root]
next_node=root['child']
whileTrue:
whilenext_node!={}:
path.append(next_node)
ifnext_node['name']==node_name:
returnpath
next_node=next_node['child']

next_node=path.pop()
ifpath==[]:
returnpath
else:
next_node=next_node['sibling']

if__name__=="__main__":
main()
//奧運排行榜
#include<iostream>
#include<algorithm>

usingnamespacestd;

constintMAX=224;
typedefstruct
{
intindex;
doubleno;
intrange;
}ELEM;
intcmp_des(constELEM&a,constELEM&b){
returna.no>b.no;//降序
}
intcmp_aes(constELEM&a,constELEM&b){
returna.index<b.index;//升序
}
intmain()
{
intn,m;
cin>>n>>m;

doubledata[5][MAX];
intpaiming[5][MAX];
ELEMtmp[MAX];
intcountry[MAX];
inti,j;
for(i=0;i<n;i++)
{
cin>>data[1][i]>>data[2][i]>>data[0][i];
data[3][i]=data[1][i]/data[0][i];
data[4][i]=data[2][i]/data[0][i];
}
for(i=0;i<m;i++)
cin>>country[i];

for(i=1;i<=4;i++)
{
for(j=0;j<n;j++)
{
tmp[j].index=j;
tmp[j].no=data[i][j];
}
sort(tmp,tmp+n,cmp_des);
for(j=0;j<n;j++)
{
if(j>0&&tmp[j].no==tmp[j-1].no)
tmp[j].range=tmp[j-1].range;
else
tmp[j].range=j+1;
}
sort(tmp,tmp+n,cmp_aes);
for(j=0;j<n;j++)
{
paiming[i][j]=tmp[j].range;
}
}

inttop_12,top_34,top_1234;
for(i=0;i<m;i++)
{
top_12=paiming[1][country[i]]<=paiming[2][country[i]]?1:2;
top_34=paiming[3][country[i]]<=paiming[4][country[i]]?3:4;
top_1234=paiming[top_12][country[i]]<=paiming[top_34][country[i]]?top_12:top_34;
printf("%d:%d",(int)paiming[top_1234][country[i]],(int)top_1234);
if(i<m-1)
printf("");
}

return0;
}

㈢ (數據結構與演算法)已經並查集的Find函數部分代碼如下,請補充剩餘代碼

如果你的tree數組一開始初始為-1
int Find(int x)
{
int s;
s=x;
while(tree[s]!=-1)
s=tree[s];
return s;
}

如果tree[i]初始化為i
int Find(int x)
{
int s;
s=x;
while(tree[s]!=s) //tree數組是並查集數組,-1假設為根節點的父節點。。
s=tree[s];
return s;
}

㈣ 《數據結構與演算法分析》(C++版第二版)中的「可利用空間表」的實現代碼的疑問

不是樓主說的意思吧。
它的意思是你可以初始化一段內存,存放在
Link<Elem>* Link<Elem>::freelist中
當我們需要申請內存的時候,只需要從freelist中快速的取出一段即可
然而,當freelist已經使用完畢後,就調用C++默認的::new來申請
內存;
只不過這段代碼的freelist的初始化部分並沒有給出。

㈤ 數據結構演算法代碼

我感覺你很有必要把這段代碼好好的整理、規范下。。。

typedef
char
ListItem;
typedef
struct
node
*link;

typedef
struct
node
{

ListItem
element;

link
next;
}Node;

typedef
struct
llist
*List;

typedef
struct
llist
{

link
first;
}Llist;

估計到後面,你都弄不清楚,這裡面的哪個是哪個了吧。

把這段整理清楚之後,出了問題也就很容易找了。。

java演算法與數據結構代碼

第1題:我給你搭建演算法框架,具體需求,你只需往裡面寫Code即可:

publicclassProgram{

privatestaticfinalintN=6;
publicstaticvoidmain(String[]args){
Nodehead=newNode(-1,null);//定義頭指針,帶頭結點的單鏈表
for(inti=0;i<N;i++){
Nodee=newNode(i+1,null);
tailInsert(head,e);
}

//Test
Nodep=head;
while(p.getNext()!=null){
p=p.getNext();
}
}

/**
*@paramhead實施尾插法演算法的單鏈表頭指針
*@parame所需的元素
*/
privatestaticvoidtailInsert(Nodehead,Nodee){
Nodep=head;
while(p.getNext()!=null){
p=p.getNext();//尋訪單鏈表,直至到達單鏈表末尾
}
//實施尾插法
p.setNext(e);
}
}

classNode{
privateintid;//編號
privateNodenext;//單鏈表後繼指針
privateStringvote;//選票

publicNode(){}
publicNode(intid,Nodenext){
super();
this.id=id;
this.next=next;
}
publicNode(intid,Nodenext,Stringvote){
super();
this.id=id;
this.next=next;
this.vote=vote;
}
@Override
publicStringtoString(){
return"Node[id="+id+",next="+next+"]";
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicNodegetNext(){
returnnext;
}
publicvoidsetNext(Nodenext){
this.next=next;
}
}

第2題:參看我以前的回答:https://..com/question/431512924412893084

演算法思想已經寫的清楚得不能在清楚了。轉成Java就是小菜一碟。

㈦ 數據結構與演算法實驗代碼

#include <cstdio>

int gold[225],medal[225],population[225];
int list[10];
int fir = 0;//用來判斷是否是第一個元素用的
void printresult(int check_number,int n);
int main()
{
int n,m;

scanf("%d%d",&n,&m);
for(int i = 0;i < n;i++)
scanf("%d%d%d",&gold[i],&medal[i],&population[i]);
for(int i = 0;i < m;i++)
{
int check_number;
scanf("%d",&check_number);
printresult(check_number,n);
}
printf("\n");
return 0;
}
void printresult(int check_number,int n)
{
for(int i = 0;i < n;i++)
{
if(gold[i]>gold[check_number])
list[1]++;
}
int result = ++list[1];//這里還要多加一個1,因為數組原來是0
int num = 1;
for(int i = 0;i < n;i++)
{
if(medal[i]>medal[check_number])
list[2]++;
}
list[2]++;
double gold_per = gold[check_number]*1.0/population[check_number];
for(int i = 0;i < n;i++)
{
if((gold[i]*1.0/population[i])>gold_per)
list[3]++;
}
list[3]++;
double medal_per = medal[check_number]*1.0/population[check_number];
for(int i = 0;i < n;i++)
{
if(medal[i]*1.0/population[i]>medal_per)
list[4]++;
}
list[4]++;
for(int i = 1;i < 5;i++)
{
if(list[i]<result)
{
result = list[i];
num = i;
}
}
for(int i = 1;i < 5;i++)
list[i] = 0;//對排名數組清零,因為之後要多次調用
if(!fir)
{
printf("%d:%d",result,num);//首個輸入前面不用空格
fir = 1;
}
else
printf(" %d:%d",result,num);
}
//如果超時的話還可以再優化,不過國家的數量比較少,應該不會
//優化就是把排名是1的話直接輸出,不進行之後運算

㈧ 剛學數據結構,題目不會,給個代碼(演算法)參考

1
#include "stdio.h"

#define MAXSIZE 32

typedef char datatype;

typedef struct _Node{
datatype data;
_Node *lchild, *rchild;
}Node, *PNode, *PBitTree;

void visit(PNode node){
printf("%c\t", node->data);
}

(1)
void PreOrder(PBitTree root){
if(!root) return;
visit(root);
PreOrder(root->lchild);
PreOrder(root->rchild);
}

void InOrder(PBitTree root){
if(!root) return;
InOrder(root->lchild);
visit(root);
InOrder(root->rchild);
}

void PostOrder(PBitTree root){
if(!root) return;
PostOrder(root->lchild);
PostOrder(root->rchild);
visit(root);
}

(2)
void Display(PBitTree root){
PNode nodeQueue[MAXSIZE]; /*結點隊列*/
int levelQueue[MAXSIZE]; /*用於保存結點層次信息的隊列*/
int front=0, rear=0, curlevel = 0, bline = 0; /*bline用於判斷是否需要換行*/
PNode p = root;
if(p) { /*根結點入隊*/
nodeQueue[rear] = p;
levelQueue[rear] = 0;
rear = (rear + 1) % MAXSIZE;
}
while(front != rear) { /*如果隊列不空*/
p = nodeQueue[front]; /*獲取隊首元素*/
bline = levelQueue[front] != curlevel ? 1 : 0; /*判斷是否需要換行*/
curlevel = levelQueue[front];
front = (front + 1) % MAXSIZE; /*出隊*/
if(bline) printf("\n"); /*如果需要換行,則輸出換行符*/
visit(p);
if(p->lchild) { /*如果左孩子不空,將左孩子入隊*/
nodeQueue[rear] = p->lchild;
levelQueue[rear] = curlevel + 1;
rear = (rear + 1) % MAXSIZE;
if(p->rchild) { /*如果右孩子不空,將右孩子入隊*/
nodeQueue[rear] = p->rchild;
levelQueue[rear] = curlevel + 1;
rear = (rear + 1) % MAXSIZE;
}
}
}

(3)
void Exchange(PBitTree root) {
if(!root) return;
PNode tmp;
Exchange(root->lchild);
Exchange(root->rchild);
tmp = root->lchild;
root->lchild = root->rchild;
root->rchild = tmp;
}

2
typedef char datatype;

typedef struct _listNode{
datatype data;
_listNode *next;
}RQueueNode, *PRQueueNode, *PRQueue;

PRQueue rear;

void InitRQueue( ) { /*初始化隊列*/
rear = (PRQueue) malloc(sizeof(RQueueNode)); /*生成附加頭結點*/
rear->next = rear; /*設置為循環的空隊*/
}

void EnRQueue(datatype x) { /*入隊*/
PRQueueNode *newNode;
newNode = (PRQueue) malloc(sizeof(RQueueNode)); //生成新結點
newNode->data = x;
newNode->next = rear->next; /*將新結點鏈接到隊尾*/
rear->next = newNode;
rear = newNode; /*將新結點作為新的隊尾*/
}

datatype DeRQueue( ) { /*出隊*/
PRQueueNode *front; /*隊首指針*/
datatype tmp; /*用於返回隊首元素數據*/
assert(rear->next != rear); /*斷言隊列不空*/
front = rear->next->next;
rear->next->next = front->next;
if(rear == front) rear = rear->next; /*如果出隊的是隊列中的最後一個結點*/
tmp = front->data;
free(front);
return tmp;
}

㈨ (數據結構與演算法)請編寫一個代碼段,彈出隊列中所有結點,用STL中得隊列實現

#include<stdio.h>
#include<queue>
using namespace std;
int main(void)
{
queue<int>Q;
while(!Q.empty())Q.pop();

int i;
for(i=0;i<10;i++)
{
Q.push(i);
}
while(!Q.empty())
{
printf("%d ",Q.front());
Q.pop();
}
puts("");
return 0;
}

閱讀全文

與數據結構與演算法實現代碼相關的資料

熱點內容
拆二代程序員 瀏覽:396
河北壓縮空氣冷干機生產廠家 瀏覽:578
圖論與java 瀏覽:575
程序員寫代碼告白初音 瀏覽:738
sshpdf 瀏覽:539
windows調用linux 瀏覽:594
如何查找本地伺服器名稱 瀏覽:819
linux文件只讀屬性 瀏覽:585
VNAS技術加密 瀏覽:131
python編程電話費計算話費 瀏覽:463
c編譯文件怎麼改名 瀏覽:626
pdf轉格式軟體 瀏覽:875
單片機原理及應用第二版第八章答案 瀏覽:536
伺服器一百個節點相當於什麼 瀏覽:344
綏化電氣編程培訓 瀏覽:374
輕量應用伺服器怎麼添加軟體上去 瀏覽:813
資產管理pdf 瀏覽:170
製冷壓縮機熱負荷過低 瀏覽:364
伺服器出現兩個IPV4地址 瀏覽:848
宜興雲存儲伺服器 瀏覽:221