當前位置:首頁 » 編程語言 » 數據結構c語言版查找

數據結構c語言版查找

發布時間: 2023-01-26 20:49:50

① 數據結構關於數據查找的代碼(用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的啊

② C語言數據結構「查找」問題

#include <stdio.h>

int find(int* array, int size, int value)
{
int up = 0;
int left = 0;
int right = size - 1;
int mid,times = 0;
if(array[size-1] >= array[0]) up = 1;
while(left<=right)
{
mid = (left+right)>>1;
times ++;
if(array[mid] == value) break;
else if(array[mid] > value)
{
if(up)
right = mid - 1;
else left = mid + 1;
}
else if(up)
left = mid + 1;
else right = mid - 1;
}
printf("共比較%d次\n",times);
if(left <= right)
return mid;
else
return -1;
}

int main()
{
int array[20] = {100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5};
int q, answer;
while(scanf("%d",&q)==1)
{
answer = find(array,20,q);
if(answer > 0)
printf("位置: %d\n", answer);
else
printf("找不到\n");
}
}

③ 《數據結構(C語言版)》pdf下載在線閱讀,求百度網盤雲資源

《數據結構(C語言版)》(嚴蔚敏)電子書網盤下載免費在線閱讀

資源鏈接:

鏈接:https://pan..com/s/1BmtD5k3mLtJZO36Xw_Hq3w

密碼:5dfz

書名:數據結構(C語言版)

作者:嚴蔚敏

豆瓣評分:6.1

出版社:清華大學出版社

出版年份:2012-5

頁數:335

內容簡介:

《數據結構》(C語言版)是為「數據結構」課程編寫的教材,也可作為學習數據結構及其演算法的C程序設計的參數教材。

本書的前半部分從抽象數據類型的角度討論各種基本類型的數據結構及其應用;後半部分主要討論查找和排序的各種實現方法及其綜合分析比較。其內容和章節編排1992年4月出版的《數據結構》(第二版)基本一致,但在本書中更突出了抽象數據類型的概念。全書採用類C語言作為數據結構和演算法的描述語言。

本書概念表述嚴謹,邏輯推理嚴密,語言精煉,用詞達意,並有配套出版的《數據結構題集》(C語言版),便於教學,又便於自學。

本書後附有光碟。光碟內容可在DOS環境下運行的以類C語言描述的「數據結構演算法動態模擬輔助教學軟體,以及在Windows環境下運行的以類PASCAL或類C兩種語言描述的「數據結構演算法動態模擬輔助教學軟體」。

本書可作為計算機類專業或信息類相關專業的本科或專科教材,也可供從事計算機工程與應用工作的科技工作者參考。

作者簡介:

嚴蔚敏 清華大學計算機系教授,長期從事數據結構教學和教材建設,和吳偉民合作編著的《數據結構》曾獲「第二屆普通高等學校優秀教材全國特等獎」和「1996年度國家科學技術進步獎三等獎」。

吳偉民 廣東工業大學計算機學院副教授,碩士生導師。廣東省計算機學會圖像圖形分會秘書長。長期從事數據結構教學和系列教材建設。主要研究領域:數據結構和演算法、可是計算、編譯和虛擬機技術、智能系統等。和嚴蔚敏合作編著的《數據結構》曾獲「第二屆普通高等學校優秀教材全國特等獎」和「1996年度國家科學技術進步獎三等獎」。

④ 2020-07-17/(C語言)數據結構按值查找表結點值地址

//按值查找表結點值地址

typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *LinkList;
LinkList List_TailInsert(LinkList L)
{
int x; //設元素類型為整型
L = (LinkList)malloc(sizeof(LNode));
LNode *s, *r = L; //r為表尾指針
printf("請輸入表元素(以999結尾):");
scanf("%d", &x); //輸入結點的值
while (x != 999) //輸入999表示結束
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s; //r指向新的表尾結點
scanf("%d", &x);
}
r->next = NULL; //尾結點指針置空
return L;
}
void print(LinkList L)
{
while (L->next != NULL)
{
L = L->next;
printf("%d ", L->data);
}
}
LNode *LocateElem(LinkList L, int e)
{
int i = 1;
LNode *p = L->next; //頭結點賦給p
while (p != NULL && p->data != e) //從第1個結點開始找data值為e的結點
{
p = p->next;
i++;
}
return p; //找到後返回該結點指針,否則返回NULL
}
int main()
{
LinkList L, A;
LNode *p;
int e, i;
A = (LinkList)malloc(sizeof(LNode));
A = List_TailInsert(L);
printf("尾插法建立的單鏈表:");
print(A);
printf("\n");
printf("請輸入要查找的值:");
scanf("%d", &e);
p = LocateElem(A, e);
printf("要查找的值%d的地址為%p.", e, p);
printf("\n");
return 0;
}

⑤ C語言編寫數據結構查找演算法

實驗五 查找的實現
一、 實驗目的
1.通過實驗掌握查找的基本概念;
2.掌握順序查找演算法與實現;
3.掌握折半查找演算法與實現。
二、 實驗要求
1. 認真閱讀和掌握本實驗的參考程序。
2. 保存程序的運行結果,並結合程序進行分析。
三、 實驗內容
1、建立一個線性表,對表中數據元素存放的先後次序沒有任何要求。輸入待查數據元素的關鍵字進行查找。為了簡化演算法,數據元素只含一個整型關鍵字欄位,數據元素的其餘數據部分忽略不考慮。建議採用前哨的作用,以提高查找效率。
2、查找表的存儲結構為有序表,輸入待查數據元素的關鍵字利用折半查找方法進行查找。此程序中要求對整型量關鍵字數據的輸入按從小到大排序輸入。
一、順序查找
順序查找代碼:
#include"stdio.h"
#include"stdlib.h"
typedef struct node{
intkey;
}keynode;
typedef struct Node{
keynoder[50];
intlength;
}list,*sqlist;
int Createsqlist(sqlist s)
{
inti;
printf("請輸入您要輸入的數據的個數:\n");
scanf("%d",&(s->length));
printf("請輸入您想輸入的%d個數據;\n\n",s->length);
for(i=0;i<s->length;i++)
scanf("%d",&(s->r[i].key));
printf("\n");
printf("您所輸入的數據為:\n\n");
for(i=0;i<s->length;i++)
printf("%-5d",s->r[i].key);
printf("\n\n");
return1;
}
int searchsqlist(sqlist s,int k)
{
inti=0;
s->r[s->length].key=k;
while(s->r[i].key!=k)
{

i++;
}
if(i==s->length)
{
printf("該表中沒有您要查找的數據!\n");
return-1;
}
else
returni+1;
}
sqlist Initlist(void)
{
sqlistp;
p=(sqlist)malloc(sizeof(list));
if(p)
returnp;
else
returnNULL;
}
main()
{
intkeyplace,keynum;//
sqlistT;//
T=Initlist();
Createsqlist(T);
printf("請輸入您想要查找的數據的關鍵字:\n\n");
scanf("%d",&keynum);
printf("\n");
keyplace=searchsqlist(T,keynum);
printf("您要查找的數據的位置為:\n\n%d\n\n",keyplace);
return2;
}
順序查找的運行結果:
二、折半查找
折半查找代碼:
#include"stdio.h"
#include"stdlib.h"
typedef struct node{
intkey;
}keynode;
typedef struct Node{
keynoder[50];
intlength;
}list,*sqlist;
int Createsqlist(sqlist s)
{
inti;
printf("請輸入您要輸入的數據的個數:\n");
scanf("%d",&(s->length));
printf("請由大到小輸入%d個您想輸入的個數據;\n\n",s->length);
for(i=0;i<s->length;i++)
scanf("%d",&(s->r[i].key));
printf("\n");
printf("您所輸入的數據為:\n\n");
for(i=0;i<s->length;i++)
printf("%-5d",s->r[i].key);
printf("\n\n");
return1;
}
int searchsqlist(sqlist s,int k)
{
intlow,mid,high;
low=0;
high=s->length-1;
while(low<=high)
{
mid=(low+high)/2;
if(s->r[mid].key==k)
returnmid+1;
elseif(s->r[mid].key>k)
high=mid-1;
else
low=mid+1;
}
printf("該表中沒有您要查找的數據!\n");
return-1;
}
sqlist Initlist(void)
{
sqlistp;
p=(sqlist)malloc(sizeof(list));
if(p)
returnp;
else
returnNULL;
}
main()
{
intkeyplace,keynum;//
sqlistT;//
T=Initlist();
Createsqlist(T);
printf("請輸入您想要查找的數據的關鍵字:\n\n");
scanf("%d",&keynum);
printf("\n");
keyplace=searchsqlist(T,keynum);
printf("您要查找的數據的位置為:\n\n%d\n\n",keyplace);
return2;
}
折半查找運行結果:
三、實驗總結:
該實驗使用了兩種查找數據的方法(順序查找和折半查找),這兩種方法的不同之處在於查找方式和過程不同,線性表的創建完全相同,程序較短,結果也一目瞭然。

⑥ 數據結構 c語言版 ——順序表的查找、插入與刪除

#include<stdio.h>
#include<stdlib.h>
#define N 10 //順序表的最大容量
int length=0; //順序表的當前元素個數

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100//線性表存儲的空間初始化分配量
#define LISTINCREAMENT 10 //線性表存儲空間的分配增量

typedef struct LNode//線性單鏈表存儲結構
{
int data;
struct LNode *next;
}LNode,*LinkList;

int CreatList_L(LinkList&L)//創建一個線性鏈表
{
L=(LinkList)malloc(sizeof(LNode));//分配一個空間給鏈表,作為頭結點

if(!L) exit(OVERFLOW);
L->next=NULL;
return OK;
}
int DestroyList_L(LinkList &L)//銷毀鏈表
{
if(L) free(L);
return OK;
}
int ListInsert_L(LinkList&L,int i,int e)//再練表的第i個元素前插入一個元素e
{
LinkList p=L;//p指針定位於i-1
LNode *s;
int j=0;
while(p&&j<i-1) {p=p->next;j++;}//定位
if(!p||j>i-1) return ERROR;//如果i<1或大於鏈表元素個數+1
s=(LNode*)malloc(sizeof(LNode));
if(!s) exit(OVERFLOW);
s->data=e; //完成插入操作
s->next=p->next;
p->next=s;
return OK;
}

int ListDelet_L(LinkList&L,int i,int&e)//刪除鏈表L中的第i個元素,並返回給e;
{
LinkList p=L;
LNode* q;
int j=0;
while(!p&&j<i-1) {p=p->next;j++;}//p指針定位於i-1;
if(!p->next||j>i-1) return ERROR;

e=p->next->data; //完成刪除操作
q=p->next;
p->next=p->next->next;
free(q);
return OK;

}

int ListTraverse_L(LinkList L,int n)//鏈表的遍歷
{
int i=0;
if(!L)return ERROR;
L=L->next;
while(L)
{
if(L->data==n)return i;
L=L->next;
i++;
}

return FALSE;
}

int InverseSingleList_L(LinkList &L)
{
if(!L->next||!L->next->next)//如果鏈表少於2個Node那麼鏈表不需要改變順序
return OK;
LNode *p,*q;
p=L->next; //第一次因為p是最後一個連接所以把p->next設為空
q=p->next;
p->next=NULL;
p=q;
while(p)
{
q=p->next; //用q去保留p後面一個Node;
p->next=L->next;
L->next=p;
p=q;
}
return OK;
}

int main()
{
int List[N];
LinkList L;

int ch,exit='N';
do
{
system("CLS");
printf("\t\t********************************************\n");
printf("\t\t* 1.創建一個順序表 .........(1) *\n");
printf("\t\t* 2.在順序表中查找元表.........(2) *\n");
printf("\t\t* 3.在順序表中插入元表.........(3) *\n");
printf("\t\t* 4.在順序表中刪除元表.........(4) *\n");
printf("\t\t* 5.退出 .........(5) *\n");
printf("\t\t********************************************\n");
printf("\n請選擇操作代碼:");
ch=getchar();

switch(ch)
{
case '1':
printf("\n請輸入十個元素");
CreatList_L(L);
for(length=0;length<N;length++)
{
scanf("%d",&List[length]);
getchar();
ListInsert_L(L,length+1,List[length]);
}
printf("\n創建成功!");
getchar();
break;
case '2':
scanf("%d",&List[0]);
if(ListTraverse_L(L,List[0]))printf("該元素存在該年表的第%d個位置",ListTraverse_L(L,List[0]));
else printf("不存在該元素");
getchar();
break;
case '3':
scanf("%d%d",&length,&List[0]);
ListInsert_L(L,length,List[0]);
system("pause");
break;
case '4':
scanf("%d",&length);
ListDelet_L(L,length,List[0]);
system("pause");
break;
case '5':
printf("\n您是否真的要退出程序(Y/N):");
getchar();
exit=getchar();
break;
default:
getchar();
printf("\n無效輸入,請重新選擇...:");
getchar();
break;

}

}while(exit!='y'&&exit!='Y');

}

⑦ C語言——數據結構(排序-查找)

#include<stdio.h>
#include<stdlib.h>
#define N 10
void shellpass(int a[], int n, int d)
{
int i,j,temp;
for(i=d;i<n;i++)
{
if(a[i]<a[i-d])
{
temp=a[i];
for(j=i-d;j>=0&&temp<a[j];j-=d)
a[j+d]=a[j];
a[j+d]=temp;
}
}
}
void shellsort(int a[], int n, int delta[], int t)
{
int i, j;
for(i=0; i<t; i++)
{
shellpass(a, n, delta[i]);

printf("第%d趟希爾排序,增量為%d,排序之後的結果\n",i+1,delta[i]);
for(j=0; j<n; j++)
{
printf("%d\t",a[j]);
}
printf("\n");
}
}
void main()
{
int n=N, a[N];
int b[3] = {5,3,1};
int i;
printf("輸入%d個數\n", n);
for(i=0;i<n;i++)
{
printf("第%d個數:\t",i+1);
scanf("%d",&a[i]);
}

shellsort(a, n, b, 3);
printf("最終希爾排序之後的結果\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:583
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:877
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:572
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:758
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:674
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1001
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:245
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:104
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:796
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:702