數據結構c語言代碼
1. 數據結構關於數據查找的代碼(用c語言)
(1)創建圖的鄰接矩陣和鄰接表 
(2)驗證圖的深度優先、廣度優先遍歷演算法 
(3)驗證最短路徑問題
問題太多了,每個小問題,都可以寫不少代碼
下面是問題1的代碼,其他的問題,網上也很容易找到
//     鄰接矩陣表示   :   
    
  #include   <iostream.h>   
  #include   <stdlib.h>   
    
  #define   INFINITY   0   
    
  #define   MAX_VERTEX_NUM   10         //最大頂點數   
    
  #define   MAX_EDGE_NUM   40       //最大邊數   
    
  typedef   enum   Graphkind;   
typedef   char   VertexType;           //頂點數據類型     
    
  typedef   struct   ArcCell   
  {     
  int   adj;             //無權圖,1或0表示相鄰否;帶權圖則是權值。   
  //int   *info;   
  }ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];   
typedef   struct     
  {     
  VertexType   vexs[MAX_VERTEX_NUM];           //頂點向量   
  AdjMatrix   arcs;       //鄰接矩陣   
  int   vexnum,arcnum;                         //圖的當前頂點數和弧數。   
  Graphkind   kind;   
  }MGraph;   
    
  int   LocateVex(MGraph   G,VertexType   v1)   
  {   
  int   i;   
  for(i=0;i<G.vexnum;i++)   
  if(G.vexs[i]==v1)   
  return   i;   
  return   -1;   
  }   
    
  int   CreatUDN(MGraph   &G)   
  //   採用數組表示法,構造無向網   G   
  {   
  VertexType   v1,v2;   
  int   w,j;   
  cout<<"輸入圖的頂點數"<<endl;   
  cin>>G.vexnum;   
  cout<<"輸入圖的弧數"<<endl;   
  cin>>G.arcnum;   
  for(int   i=0;i<G.vexnum;i++)   
  {   
  cout<<"輸入頂點向量"<<endl;   
  cin>>G.vexs[i];   
  }   
  for(i=0;i<G.vexnum;i++)   
  for(j=0;j<G.vexnum;j++)   
  {   
  G.arcs[i][j].adj=INFINITY;   
  }   
  for(int   k=0;k<G.arcnum;++k)                 //構造鄰接矩陣   
  {   
  cout<<"輸入邊依附的兩個頂點"<<endl;   
  cin>>v1>>v2;   
  cout<<"輸入此邊的權值"<<endl;   
  cin>>w;   
  i=LocateVex(G,v1);   
  j=LocateVex(G,v2);   
  G.arcs[i][j].adj=w;   
  G.arcs[j][i].adj=G.arcs[i][j].adj;   
  }   
  return   1;   
  }   
    
  void   dispMGraph(MGraph   G)   
  {   
  cout<<"圖的鄰接矩陣圖是:"<<endl;   
  for(int   i=0;i<G.vexnum;i++)   
  {   
  for(int   j=0;j<G.vexnum;j++)   
  cout<<" "<<G.arcs[i][j].adj;   
  cout<<endl;   
  }   
  }   
        
  void   main()   
  {   
  MGraph   G;   
  CreatUDN(G);   
  dispMGraph(G);       
  }
//     鄰接表   表示:   
    
  #include   <iostream.h>   
  #include   <stdlib.h>   
    
  #define   MAX_VERTEX_NUM   20         //最大頂點數   
    
  #define   MAX_EDGE_NUM   40       //最大邊數   
    
  int   visited[   MAX_VERTEX_NUM];   
    
  typedef   int   VertexType   ;           //頂點數據類型     
    
  typedef   struct   ArcNode   
  {     
  int   adjvex;   
  int   weight;   
  struct   ArcNode   *nextarc;   
  }ArcNode;   
    
  typedef   struct   VNode   
  {     
  VertexType   data;   
  ArcNode   *firstarc;   
  }VNode,AdjList[MAX_VERTEX_NUM];   
    
  typedef   struct     
  {     
  AdjList   vertices;   
  int   vexnum,arcnum;   
  int   kind;   
  }ALGraph;   
    
  void   CreateDG(ALGraph   &G)   
  {   
  int   i,j,k;   
  ArcNode   *p;   
  cout<<"創建一個圖:"<<endl;   
  cout<<"頂點數:";   cin>>G.vexnum;cout<<endl;   
  cout<<"邊數:";     cin>>G.arcnum;   cout<<endl;   
  for(i=0;i<G.vexnum;i++)   
  {   
  G.vertices[i].data=i;   
  G.vertices[i].firstarc=NULL;   
  }   
  for(k=0;k<G.arcnum;k++)   
  {   
  cout<<"請輸入第"<<k+1<<"條邊:";   
  cin>>i>>j;   
  p=(ArcNode*)malloc(sizeof(ArcNode));   
  p->adjvex=j;   
  p->nextarc=G.vertices[i].firstarc;   
  G.vertices[i].firstarc=p;   
  }   
  }   
    
  void   Disp(ALGraph   G)   
  {   
  int   i,j;   
  ArcNode   *p;   
  cout<<"輸出圖為:"<<endl;   
  for(i=0;i<G.vexnum;i++)   
  {   
  p=G.vertices[i].firstarc;   
  j=0;   
  while(p!=NULL)   
  {   
  cout<<"("<<i<<","<<p->adjvex<<")";   
  p=p->nextarc;   
  j=1;   
  }   
  if(j==1)   
  cout<<endl;   
  }   
  }   
    
  void   dfs(ALGraph   G,int   v)   //深度優先遍歷   
  {   
  ArcNode   *p;   
  cout<<v<<"   ";   
  visited[v]=1;   
  p=G.vertices[v].firstarc;   
  while(p!=NULL)   
  { if(!visited[p->adjvex])   
  dfs(G,p->adjvex);   
  p=p->nextarc;   
  }   
  return   ;   
  }   
void   dfs1(ALGraph   G)   
  {   
  int   i;   
  for(i=0;i<G.vexnum;i++)   
  if(visited[i]==0)   
  dfs(G,i);   
  }   
void   main()   
  {     
  ALGraph   G;   
  CreateDG(G);   
  int   v;   
  Disp(G);   
  cout<<"輸入頂點:";   
  cin>>v;   
  cout<<"深度優先序列:";   
  dfs1(G);   
  cout<<endl;   
  }
補充:
c和c++本來就差不了多少
只需要把#include <iostream.h>換成#include <stdio.h>
把cout換成printf,把cin換成scanf
就能把上述c++的代碼變化成c的啊
2. 數據結構二叉樹的程序,用c語言怎麼實現
您好,想要實現一個二叉樹,需要用到結構體來存儲每個節點的信息,並使用指針來存儲每個節點的左右子節點的地址。具體的實現方法可以參考下面的代碼示例:
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* createNode(int val) {
struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
void insertNode(struct TreeNode* root, int val) {
if (root == NULL) {
return;
}
if (val < root->val) {
if (root->left == NULL) {
root->left = createNode(val);
} else {
insertNode(root->left, val);
}
} else {
if (root->right == NULL) {
root->right = createNode(val);
} else {
insertNode(root->right, val);
}
}
}
void printTree(struct TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
printTree(root->left);
printTree(root->right);
}
int main() {
struct TreeNode* root = createNode(5);
insertNode(root, 3);
insertNode(root, 2);
insertNode(root, 4);
insertNode(root, 7);
insertNode(root, 6);
insertNode(root, 8);
printTree(root);
return 0;
}
在這段代碼中,我們定義了一個結構體 TreeNode 來表示二叉樹的每個節點,結構體中包含了一個節點的數值 val,以及指向左子節點和右子節點的指針 left 和 right。
3. 求數據結構(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 }
4. c語言數據結構
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct student{ int age; char name[10]; struct student *next; } ;
void main() { struct student *p,*p1,*p2;
    p1=(struct student *)malloc(sizeof(struct student)); if ( p1==NULL ) goto errorexit0;
    strcpy(p1->name,"張三"); p1->age=30; p1->next=NULL;
    p2=(struct student *)malloc(sizeof(struct student)); if ( p2==NULL ) goto errorexit1;
    strcpy(p1->name,"李四"); p2->age=40; p2->next=NULL;
    p=p1; p->next=p2;
    printf("%s %s\n",p->name,(p->next)->name);
    free(p1); free(p2); return;
errorexit1: free(p1); printf("申請節點2出錯。\n");
errorexit0: printf("申請節點1出錯。\n");
}
5. 數據結構 圖的基本操作要C語言的完整代碼!!
#include<stdio.h>
#define n 6
#define e 8
void CREATGRAPH();
 
typedef char vextype;
typedef float adjtype;
typedef struct{
    vextype  vexs[n];
    adjtype arcs[n][n];
}graph;
int main()
{
    
    CREATGRAPH();
    printf("創建成功!\n");
    
}
void CREATGRAPH()
{
    graph *ga;
    int i,j,k;
    float w;
    for(i=0;i<n;i++)
    ga->vexs[i]=getchar();
    for(i=0;i<n;i++)
     for(j=0;j<n;j++)
     ga->arcs[i][j]=0;
     for(k=0;k<e;k++)
     {
        scanf("%d%d%f",&i,&j,&w);
        ga->arcs[i][j]=w;
        ga->arcs[j][i]=w;
     
    }
        printf("創建成功!\n");
    
}
沒寫完,,自己加加吧!
6. 數據結構c語言
把scanf("%d ",&q->name);改成scanf("%s",q->name);。
把scanf("%d ",&q->score);改成scanf("%d",&q->score);。
函數studlist *CreateStudent()應該有一個返回值。若不需要返回值,請改成voidCreateStudent()。
if(p->Next->score<q->score)中p->Next->score並未賦值,怎麼能與q->score比較?這里就會跳出運行。
char name[3];中3太小隻能放下一個漢字或兩個字元。
適當的地方應該有釋放所申請的內存的語句。
7. c語言數據結構這幾行代碼什麼意思,可以分別解釋一下么新手小白求教
typedef struct{int i,j,int di;}Box; //定義一個自定義類型: 結構Box
typedef struct{Box data[MaxSize];int top;}StackType; //定義結構類型,其中有Box數組
StackType st; //st具備StackType結構, 應該是堆棧
st.top++; //頂層加1,這裡面應當先初始化st.top為棧底值,比如0
st.data[st.top].i=X; //相當於入棧操作,棧頂元素的i和j進行賦值
st.data[st.top].j=Y;
st.data[st.top].di=-1;

8. 數據結構C語言編程
#include"stdio.h"
#include<stdlib.h>
#include"time.h"
intmain(intargv,char*argc[]){
doublex[10]={0.0,};
inti;
srand((unsigned)time(NULL));
while(rand()%10000!=0){//
for(i=0;i<9;x[i++]=x[i+1]);
x[9]=rand()/32767.0*100000;//模擬採集數據
}
for(i=0;i<10;printf("%10.3f ",x[i++]));//輸出最後10個數
return0;
}
運行樣例:

9. c語言數據結構 最短路徑問題代碼
直接看代碼:
#include<stdlib.h>
#defineMAXVEX10
typedefstructgraph{
intn,e;//頂點數、邊數
charvexs[MAXVEX];//頂點數組
intarcs[MAXVEX][MAXVEX];//鄰接矩陣
intkind;//類型:0有向圖;1無向圖;2有向網;3無向網
}MGraph;
voidPrintPath(MGraphG,int*p,inti){
if(p[i]>=0){
PrintPath(G,p,p[i]);//先輸出前驅頂點
}
printf("%c",G.vexs[i]);//輸出本頂點
}
voidDijkstra(MGraphG,intv){
//用Dijkstra演算法求有向網G中序號為v的頂點到
//其餘各頂點的最短路徑
int*s,*d,*p,i,j,k,min;
if(v<0||v>=G.n){//頂點編號參數錯誤
printf("DijkstraparameterERROR!v<0Orv>=%d",G.n);
return;
}
s=(int*)malloc(sizeof(int)*G.n);
d=(int*)malloc(sizeof(int)*G.n);
p=(int*)malloc(sizeof(int)*G.n);
for(i=0;i<G.n;i++){//初始化輔助數組,置0
s[i]=0;d[i]=G.arcs[v][i];
if(d[i]!=0)p[i]=v;//v是vi的直接前驅
elsep[i]=-1;//-1表示無直接前驅
}
s[v]=1;d[v]=0;//確定源點自身的最短路徑長度
printf("Dijkstra:(Theshortestpathfrom%c:) ",G.vexs[v]);
for(i=0;i<G.n-1;i++){
//確定v到其餘n-1個頂點的最短路徑
min=32767;k=-1;
for(j=0;j<G.n;j++){
//找出路徑長度最小的頂點k
if(!s[j]&&d[j]!=0&&d[j]<min){
k=j;min=d[k];
}
}
if(k<0){//有未能到達的頂點,把它們輸出
for(j=0;j<G.n;++j){
if(j==v)continue;
if(s[j]==0){
printf("%c->%c:Nopath. ",G.vexs[v],G.vexs[j]);
}
}
free(s);free(d);free(p);
return;//已完成或出現不連通的頂點
}
s[k]=1;
printf("%c->%c:d=%-5d,p=",G.vexs[v],G.vexs[k],d[k]);
PrintPath(G,p,k);//輸出v到i的路徑(正序)
printf(" ");
for(j=0;j<G.n;j++){
//更新其餘頂點的最短路徑及前驅
if(!s[j]&&G.arcs[k][j]!=0&&(d[j]==0||d[j]>d[k]+G.arcs[k][j])){
d[j]=d[k]+G.arcs[k][j];p[j]=k;
}
}
}
free(s);free(d);free(p);
}
這是單源的最短路徑演算法。
10. 數據結構創建一棵樹的c語言代碼怎麼寫
剛剛回答了一個類似的問題,以下代碼供參考:
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char TElemType;
typedef int Status;
typedef struct BiTNode { // 結點結構
 TElemType      data;
 struct BiTNode  *lchild, *rchild; 
 // 左右孩子指針
} BiTNode, *BiTree;
//以下是建立二叉樹存儲結構,空節點輸入作為#結束標識
Status CreateBiTree(BiTree &T) {
 //請將該演算法補充完整,參見第6章課件演算法或課本
 char ch;
 scanf("%c",&ch);
 if(ch=='#') T=NULL;
 else{
  if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
   exit(OVERFLOW);
  T->data=ch;
  CreateBiTree(T->lchild);
  CreateBiTree(T->rchild);
 }
 return OK;
} // CreateBiTree
void Preorder(BiTree T)
{
 if(T)
 {
  printf("%c",T->data);
  Preorder(T->lchild);
  Preorder(T->rchild);
 }     
} 
void Inorder(BiTree T)
{ // 中序遍歷二叉樹 
 //請將該演算法補充完整,參見第6章課件演算法
 if(T)
 {
  Inorder(T->lchild); 
  printf("%c",T->data);
  Inorder(T->rchild);
 }
}
void Postorder(BiTree T)
{ // 後序遍歷二叉樹 
 //請將該演算法補充完整,參見第6章課件演算法
 if(T)
 {
  Postorder(T->lchild);
  Postorder(T->rchild);
  printf("%c",T->data);
 }
}
//以下是求葉子結點數
void CountLeaf(BiTree T,int& count){ 
 //請將該演算法補充完整,參見第6章課件演算法
 if(T){
  if((!T->lchild)&&(!T->rchild))
   count++;
  CountLeaf(T->lchild,count);
  CountLeaf(T->rchild,count);
 }
} 
//以下是求二叉樹的深度
int Depth(BiTree T ){
 //請將該演算法補充完整,參見第6章課件演算法
 int depthval,depthLeft,depthRight;
 if(!T)  depthval=0;
 else{
  depthLeft = Depth(T->lchild);
  depthRight = Depth(T->rchild);
  if(depthLeft>depthRight)depthval = 1+depthLeft;
  else depthval = 1+depthRight;
 }
 return depthval;
}
void main(){
 BiTree T;
 int s=0,d;
 printf("\n creat of the bitree:\n"); 
 CreateBiTree(T);
 printf("\n output result of Preorder:\n"); 
 Preorder(T); 
 CountLeaf(T,s);
 d=Depth(T);
 printf("\n leaves=%d\n",s);
 printf("\n depth=%d\n",d);
}
