程序比較復雜,應該是大實驗吧
B. 數據結構,求源代碼,c/c++
已發送,請查收。[email protected]
C. 用到數據結構的程序源代碼
#include "stdio.h"
#include "malloc.h"
struct Bitree {
char nodes;
struct Bitree *lchild;
struct Bitree *rchild;
};
struct node {
char this;
struct node *next;
};
build( )
{
struct Bitree *b,*bn; /*b指向二叉樹,bn為操作數
*/
struct node *c,*cn; /*c放置節點,作為一個小的隊列.操作cn*/
int i,j,k;
char no;
i=1; /*每層有i個節點*/
printf("now we'll build the binary tree,,in the process,if the node is not exist ,enter '#'.first enter a boot\n"); /*先輸入根節點*/
c=(struct node *)malloc(sizeof(struct node));
b=(struct Bitree *)malloc(sizeof(struct Bitree));
scanf("%c",&no);
c->this=no;
b->nodes=no;
bn=b;
do
{
j=2*i; /*i的下一層有2i個節點,*/
for(k=1;k<=i;k++)
{
bn=c->this;
printf("the left child of %c:",c->this);
scanf("%c",&no);
bn->lchild=(struct Bitree *)malloc(sizeof(struct Bitree));
bn->lchild->nodes=no;
if (no=="#")
{
j=j-1; /*如果節點的孩子為空,節點數-1*/
}
else
{
cn->next=(struct Bitree *)malloc(sizeof(struct Bitree));
cn->next->this=no;
cn=cn->next;
};
printf("the right child of %c:",c->this);
scanf("%c",&no);
bn->rchild=(struct Bitree *)malloc(sizeof(struct Bitree));
bn->rchild->nodes=no;
if(no=="#")
{
j=j-1;
}
else
{
cn->next=(struct Bitree *)malloc(sizeof(struct Bitree));
cn->next->this=no;
cn=cn->next;
};
c=c->next;
};
i=j;
}while(i!=0);
}
firstorder(struct Bitree *n)
{
printf("%c",n->nodes);
if(n->nodes=="#")
;
else
firstorder(n->lchild);
firstorder(n->rchild);
}
main()
{
struct Bitree *o;
build(o);
firstorder(o);
}
D. 數據結構問題,希望有完整源代碼
先看題目,順序表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
E. 數據結構(C語言版)10.3快速排序源代碼
void quiksort(int a[],int low,int high)
{
int i = low;
int j = high;
int temp = a[i];
if( low < high)
{
while(i < j)
{
while((a[j] >= temp) && (i < j))
{
j--;
}
a[i] = a[j];
while((a[i] <= temp) && (i < j))
{
i++;
}
a[j]= a[i];
}
a[i] = temp;
quiksort(a,low,i-1);
quiksort(a,j+1,high);
}
else
{
return;
}
}
void main()
{
int arry[5] = {23,1,21,4,19};
quiksort(arry,0,4);
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
這是源碼另外博客裡面還有詳細解釋你可以進去看看
F. 數據結構C語言。求源代碼
這里L是函數輸入鏈表的頭結點,L->next指向鏈表的第一個元素。
進入Reverse函數,先將p、q這兩個臨時指針指向鏈表的第一個元素。此時將L->next賦為空值,即L的next不再指向鏈表第一個元素了(想想此函數的目的是:要將L->next從鏈表的一頭指向另一頭。就回明白這里的含義了)
進入循環,這時p、q都指向了鏈表的第一個元素,循環結束條件是q等於空,即while(q)表示如果q不為空NULL,則進行循環。
q=q->next; //既然q不等於空,q就再往後走走,看看還有沒有結點。
p->next=L->next;
L->next=p;
p=q;
//這里L->next是上一次循環時指向的p,而後p走到下一個節點,那麼這里p->next=L->next就是將p當前節點的next指到它前一個節點去。從而達到逆序的效果。
這里可能你會覺得最開始L->next=NULL,很奇怪~
其實不會,第一個節點的next將指向L->next即為空,因為第一個節點正是反向後的尾巴,最後一個節點,它的next應該為空才對。
循環進行第二輪時,你就應該明白,此時q會先跑到第三個節點去,當前的p指向第二個節點,L->next在第一輪循環中指向了第一個節點,然後將第二個節點的next指向了第一個節點。L->next又指向當前的p,即第二個節點,這次循環的最後p又追上q,指向了第三個節點,如此循環,直到q等於原鏈表的最後一個節點的next,這應該是空NULL,結束循環。
G. 為什麼有些人的數據結構學的那麼好,我感覺怎麼理解偽代碼與源代碼比較難
數據結構學習的是一種計算機抽象思想,通過介紹一個個經典數據結構的原理,來鍛煉提高自身的計算機抽象能力。當然,在這個過程中,一定要不斷的實踐,在實踐過程中理解這些思想,並積累相應的經驗。針對,理解代碼的困難,建議首先鞏固編程語言基礎,然後在理解代碼之前一定要先理解結構的基本思想,至少要先在邏輯上理解這些結構和其相應的基本操作,這之後再去編寫代碼(或參考),這樣才能取得不錯的效果。希望能夠對你有所幫助!~
H. 求數據結構(c語言版)程序源代碼
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #define MAX_POS_NUM 100
6 #define MAX_STR_LEN 1024
7
8
9 //1. get all position of str_z in str_x
10 int get_sub_str_pos(const char * str_x, const char * str_z, int sub_str_pos[])
11 {
12 if (NULL == str_x || NULL == str_z)
13 {
14 printf("in error!\n");
15 return -1;
16 }
17
18 const char * pos_ptr = NULL;
19
20 pos_ptr = strstr(str_x,str_z);
21
22 int i=0;
23 while(pos_ptr)
24 {
25 printf("substring positon:%d\n",pos_ptr-str_x+1);
26 sub_str_pos[i] = pos_ptr - str_x + 1;
27 pos_ptr = strstr(pos_ptr+strlen(str_z),str_z);
28 i++;
29 }
30
31 return 0;
32 }
33
34 //2. get max length common string of str_x and str_y
35 char * get_max_com_str(const char * str_x, const char * str_y)
36 {
37 int x_len = strlen(str_x);
38 int y_len = strlen(str_y);
39
40 char * tmp_str = new char[y_len+1];
41
42 for(int i=y_len; i>0; i--) // i is substring length
43 {
44 if (i>x_len)
45 continue;
46 for(int j=0;j<=y_len-i; j++) // j is substring start postion
47 {
48 snprintf(tmp_str,i+1,"%s",str_y);
49 if (strstr(str_x,tmp_str))
50 {
51 printf("%s\n",tmp_str);
52 printf("max common substring length:%d\n",i);
53 return tmp_str;
54 }
55 }
56 }
57
58 return NULL;
59 }
60
61 //3. replace all substring in question 1
62 char * replace_sub_str(const char * str_x, char * max_com_str, int sub_str_pos[], int sub_str_len)
63 {
64 char * replaced_str = new char[MAX_STR_LEN];
65
66 int sub_pos = sub_str_pos[0];
67 int l=0; // l is sub_str_pos index
68 int i=0,j=0; //i is str_x pos, j is replaced_str pos
69
70 while(*str_x)
71 {
72 if (i==sub_pos-1) // replace from this position
73 {
74 // printf ("i:%d,\n",i);
75 for (int k=0; k<strlen(max_com_str); k++)
76 {
77 *(replaced_str + j) = * (max_com_str + k);
78 j++;
79 }
80 i += sub_str_len;
81 str_x += sub_str_len;
82 l++;
83 sub_pos = sub_str_pos[l];
84 continue;
85 }
86 *(replaced_str+j) = *str_x++;
87 i++;
88 j++;
89 }
90
91 * (replaced_str + j) = '\0';
92
93 return replaced_str;
94 }
95
96 int main()
97 {
98 const char * str_x = "abcabcabc";
99 const char * str_y = "cabcd";
100 const char * str_z = "abc";
101
102 int sub_str_pos [MAX_POS_NUM] = {0};
103
104 char * max_com_str = NULL;
105
106 char * replaced_str = NULL;
107
108 get_sub_str_pos(str_x,str_z,sub_str_pos);
109 max_com_str = get_max_com_str(str_x,str_y);
110
111 printf("max common str: %s\n",max_com_str);
112
113 replaced_str = replace_sub_str(str_x, max_com_str, sub_str_pos, strlen(str_z));
114 printf("repalced str: %s\n",replaced_str);
115
116 return 0;
117 }
I. 數據結構各種演算法或源代碼從哪找
可以去書店找一下,會有裡面是數據機構整個程序實現的參考書的